You've already forked Traefik-Redis-Api
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
5587dffeeb
|
|||
|
9c1445972d
|
|||
|
28dcc6a20d
|
|||
|
7f7af2f3b4
|
|||
|
b9f4b4500d
|
|||
|
6e340e898f
|
|||
|
3e232ab198
|
|||
|
f74d7f3895
|
|||
|
303a10feba
|
|||
|
d8dff9e833
|
|||
|
88eb6d4d05
|
@@ -2,4 +2,5 @@ include:
|
||||
- local: .gitlab/ci/stages.yml
|
||||
- local: .gitlab/ci/tests.yml
|
||||
- local: .gitlab/ci/libraries.yml
|
||||
- local: .gitlab/ci/build.yml
|
||||
|
||||
|
||||
14
.gitlab/ci/build.yml
Normal file
14
.gitlab/ci/build.yml
Normal file
@@ -0,0 +1,14 @@
|
||||
include:
|
||||
- component: $CI_SERVER_FQDN/shared/blueprints/docker-build@v1.2.0
|
||||
rules:
|
||||
- if: '$CI_COMMIT_TAG'
|
||||
inputs:
|
||||
registry: scr.siteworxpro.com
|
||||
registryUser: $CI_REGISTRY_USER
|
||||
dockerHubUser: $CI_DOCKER_HUB_USER
|
||||
dockerHubPat: $CI_DOCKER_HUB_PAT
|
||||
registryPassword: $CI_REGISTRY_PASSWORD
|
||||
platform: "linux/amd64,linux/arm64"
|
||||
stage: build
|
||||
repo: scr.siteworxpro.com/traefik-api
|
||||
tag: "${CI_COMMIT_TAG}"
|
||||
@@ -1,3 +1,4 @@
|
||||
stages:
|
||||
- libraries
|
||||
- tests
|
||||
- build
|
||||
@@ -14,8 +14,11 @@ Unit Tests:
|
||||
script: |
|
||||
echo "Running unit tests..."
|
||||
composer run tests:unit:coverage
|
||||
coverage: '/^\s*Lines:\s*\d+.\d+\%/'
|
||||
artifacts:
|
||||
expire_in: 1 day
|
||||
reports:
|
||||
junit: tests/reports/junit.xml
|
||||
paths:
|
||||
- tests/reports/
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ FROM php:8.4.6-alpine AS php
|
||||
# Move the production PHP configuration file to the default location
|
||||
RUN mv /usr/local/etc/php/php.ini-production /usr/local/etc/php/php.ini \
|
||||
&& apk add libpq-dev linux-headers --no-cache \
|
||||
&& docker-php-ext-install pdo_pgsql sockets \
|
||||
&& docker-php-ext-install sockets \
|
||||
&& rm -rf /var/cache/apk/*
|
||||
|
||||
# Set the working directory to /app
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
|
||||
[](https://git.siteworxpro.com/rrise/traefik-redis-provider-api/-/commits/master)
|
||||
[](https://git.siteworxpro.com/rrise/traefik-redis-provider-api/-/commits/master)
|
||||
[](https://git.siteworxpro.com/rrise/traefik-redis-provider-api/-/releases)
|
||||
|
||||
[Traefik](https://traefik.io/traefik/) is a modern HTTP reverse proxy and load balancer that makes deploying microservices easy. This project provides
|
||||
|
||||
@@ -36,15 +36,15 @@
|
||||
"phpunit --colors=always --display-deprecations tests"
|
||||
],
|
||||
"tests:unit:coverage": [
|
||||
"phpunit --colors=always --display-deprecations --coverage-html tests/reports/html tests "
|
||||
"phpunit --coverage-text --colors=never --display-deprecations --log-junit tests/reports/junit.xml --coverage-html tests/reports/html tests "
|
||||
],
|
||||
"tests:lint": [
|
||||
"phpcs ./src --standard=PSR12 --colors -v",
|
||||
"phpcs ./tests --standard=PSR12 --colors -v"
|
||||
"phpcs ./src/**/*.php --standard=PSR12 --colors -v",
|
||||
"phpcs ./tests/**/*.php --standard=PSR12 --colors -v"
|
||||
],
|
||||
"tests:lint:fix": [
|
||||
"phpcbf ./src --standard=PSR12 --colors -v",
|
||||
"phpcbf ./tests --standard=PSR12 --colors -v"
|
||||
"phpcbf ./src/**/*.php --standard=PSR12 --colors -v",
|
||||
"phpcbf ./tests/**/*.php --standard=PSR12 --colors -v"
|
||||
],
|
||||
"tests:license": [
|
||||
"composer-license-checker"
|
||||
|
||||
@@ -2,15 +2,15 @@ volumes:
|
||||
redisdata: {}
|
||||
|
||||
services:
|
||||
|
||||
composer-runtime:
|
||||
ports:
|
||||
- "9501:9501"
|
||||
volumes:
|
||||
- .:/app
|
||||
image: siteworxpro/composer
|
||||
entrypoint: "/bin/sh -c 'while true; do sleep 30; done;'"
|
||||
environment:
|
||||
PHP_IDE_CONFIG: serverName=localhost
|
||||
|
||||
dev-runtime:
|
||||
ports:
|
||||
- "9501:9501"
|
||||
|
||||
@@ -4,10 +4,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Controllers;
|
||||
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use League\Route\Http\Exception\NotFoundException;
|
||||
use Nyholm\Psr7\ServerRequest;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Siteworxpro\App\Facades\Logger;
|
||||
use Siteworxpro\App\Traefik\ProtocolEnum;
|
||||
|
||||
abstract class Controller implements ControllerInterface
|
||||
@@ -19,7 +19,7 @@ abstract class Controller implements ControllerInterface
|
||||
try {
|
||||
return ProtocolEnum::fromString($protocol);
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
Log::error($e->getMessage());
|
||||
Logger::error($e->getMessage());
|
||||
|
||||
return ProtocolEnum::HTTP;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ use Nyholm\Psr7\ServerRequest;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Siteworxpro\App\Http\JsonResponseFactory;
|
||||
use Siteworxpro\App\Traefik\EntityEnum;
|
||||
use Siteworxpro\App\Traefik\RedisClient;
|
||||
use Siteworxpro\App\Facades\RedisClient;
|
||||
|
||||
class MiddlewaresController extends Controller
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@ use Nyholm\Psr7\ServerRequest;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Siteworxpro\App\Http\JsonResponseFactory;
|
||||
use Siteworxpro\App\Traefik\EntityEnum;
|
||||
use Siteworxpro\App\Traefik\RedisClient;
|
||||
use Siteworxpro\App\Facades\RedisClient;
|
||||
|
||||
class RoutesController extends Controller
|
||||
{
|
||||
|
||||
@@ -8,7 +8,7 @@ use Nyholm\Psr7\ServerRequest;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Siteworxpro\App\Http\JsonResponseFactory;
|
||||
use Siteworxpro\App\Traefik\EntityEnum;
|
||||
use Siteworxpro\App\Traefik\RedisClient;
|
||||
use Siteworxpro\App\Facades\RedisClient;
|
||||
|
||||
class ServicesController extends Controller
|
||||
{
|
||||
|
||||
45
src/Facades/RedisClient.php
Normal file
45
src/Facades/RedisClient.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\App\Facades;
|
||||
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
use Siteworxpro\App\Traefik\EntityEnum;
|
||||
use Siteworxpro\App\Traefik\ProtocolEnum;
|
||||
use Siteworxpro\App\Traefik\RedisClient as RedisClientConcrete;
|
||||
|
||||
/**
|
||||
* Facade for RedisClient.
|
||||
*
|
||||
* This class provides a static interface to the RedisClientConcrete class.
|
||||
*
|
||||
* @method static void createOrReplace(string $name, array $data, EntityEnum $entity, ProtocolEnum $type = ProtocolEnum::HTTP) // @codingStandardsIgnoreLine
|
||||
* @method static bool deleteAllKeys(string $name, EntityEnum $entity, ProtocolEnum $protocol = ProtocolEnum::HTTP)
|
||||
* @method static array getAllMiddlewares(ProtocolEnum $protocol = ProtocolEnum::HTTP)
|
||||
* @method static array getMiddleware(string $name, ProtocolEnum $protocol = ProtocolEnum::HTTP)
|
||||
* @method static array getAllRouters(ProtocolEnum $protocol = ProtocolEnum::HTTP)
|
||||
* @method static array getRouter(string $name, ProtocolEnum $protocol = ProtocolEnum::HTTP)
|
||||
* @method static array getAllServices(ProtocolEnum $protocol = ProtocolEnum::HTTP)
|
||||
* @method static array getService(string $name, ProtocolEnum $protocol = ProtocolEnum::HTTP)
|
||||
*/
|
||||
class RedisClient extends Facade
|
||||
{
|
||||
public static function getFacadeRoot(): RedisClientConcrete
|
||||
{
|
||||
if (self::$resolvedInstance !== null) {
|
||||
$client = self::resolveFacadeInstance(self::getFacadeAccessor());
|
||||
|
||||
if ($client instanceof RedisClientConcrete) {
|
||||
return $client;
|
||||
}
|
||||
}
|
||||
|
||||
return new RedisClientConcrete();
|
||||
}
|
||||
|
||||
protected static function getFacadeAccessor(): string
|
||||
{
|
||||
return RedisClientConcrete::class;
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ class RedisClient
|
||||
* @param ProtocolEnum $type
|
||||
* @return void
|
||||
*/
|
||||
public static function createOrReplace(
|
||||
public function createOrReplace(
|
||||
string $name,
|
||||
array $data,
|
||||
EntityEnum $entity,
|
||||
@@ -47,7 +47,7 @@ class RedisClient
|
||||
* @param ProtocolEnum $protocol
|
||||
* @return bool
|
||||
*/
|
||||
public static function deleteAllKeys(
|
||||
public function deleteAllKeys(
|
||||
string $name,
|
||||
EntityEnum $entity,
|
||||
ProtocolEnum $protocol = ProtocolEnum::HTTP
|
||||
@@ -66,33 +66,33 @@ class RedisClient
|
||||
* @param ProtocolEnum $type
|
||||
* @return array
|
||||
*/
|
||||
public static function getMiddleware(string $name, ProtocolEnum $type = ProtocolEnum::HTTP): array
|
||||
public function getMiddleware(string $name, ProtocolEnum $type = ProtocolEnum::HTTP): array
|
||||
{
|
||||
$pattern = 'traefik/' . $type->getValue() . '/' . EntityEnum::MIDDLEWARE->getValue() . "/$name/*";
|
||||
|
||||
return self::fetchValuesToArray($pattern);
|
||||
return $this->fetchValuesToArray($pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ProtocolEnum $type
|
||||
* @return array
|
||||
*/
|
||||
public static function getAllMiddlewares(ProtocolEnum $type = ProtocolEnum::HTTP): array
|
||||
public function getAllMiddlewares(ProtocolEnum $type = ProtocolEnum::HTTP): array
|
||||
{
|
||||
$pattern = 'traefik/' . $type->getValue() . '/' . EntityEnum::MIDDLEWARE->getValue() . '/*';
|
||||
|
||||
return self::getUniqueKeys($pattern, 3);
|
||||
return $this->getUniqueKeys($pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ProtocolEnum $type
|
||||
* @return array
|
||||
*/
|
||||
public static function getAllServices(ProtocolEnum $type = ProtocolEnum::HTTP): array
|
||||
public function getAllServices(ProtocolEnum $type = ProtocolEnum::HTTP): array
|
||||
{
|
||||
$pattern = 'traefik/' . $type->getValue() . '/' . EntityEnum::SERVICE->getValue() . '/*';
|
||||
|
||||
return self::getUniqueKeys($pattern, 3);
|
||||
return $this->getUniqueKeys($pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -100,22 +100,22 @@ class RedisClient
|
||||
* @param ProtocolEnum $type
|
||||
* @return array
|
||||
*/
|
||||
public static function getService(string $serviceName, ProtocolEnum $type = ProtocolEnum::HTTP): array
|
||||
public function getService(string $serviceName, ProtocolEnum $type = ProtocolEnum::HTTP): array
|
||||
{
|
||||
$pattern = 'traefik/' . $type->getValue() . '/' . EntityEnum::SERVICE->getValue() . "/$serviceName/*";
|
||||
|
||||
return self::fetchValuesToArray($pattern);
|
||||
return $this->fetchValuesToArray($pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ProtocolEnum $type
|
||||
* @return array
|
||||
*/
|
||||
public static function getAllRouters(ProtocolEnum $type = ProtocolEnum::HTTP): array
|
||||
public function getAllRouters(ProtocolEnum $type = ProtocolEnum::HTTP): array
|
||||
{
|
||||
$pattern = 'traefik/' . $type->getValue() . '/' . EntityEnum::ROUTER->getValue() . '/*';
|
||||
|
||||
return self::getUniqueKeys($pattern, 3);
|
||||
return $this->getUniqueKeys($pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -123,27 +123,26 @@ class RedisClient
|
||||
* @param ProtocolEnum $type
|
||||
* @return array
|
||||
*/
|
||||
public static function getRouter(string $name, ProtocolEnum $type = ProtocolEnum::HTTP): array
|
||||
public function getRouter(string $name, ProtocolEnum $type = ProtocolEnum::HTTP): array
|
||||
{
|
||||
$pattern = 'traefik/' . $type->getValue() . '/' . EntityEnum::ROUTER->getValue() . "/$name/*";
|
||||
|
||||
return self::fetchValuesToArray($pattern);
|
||||
return $this->fetchValuesToArray($pattern);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $pattern
|
||||
* @param int $position
|
||||
* @return array
|
||||
*/
|
||||
private static function getUniqueKeys(string $pattern, int $position): array
|
||||
private function getUniqueKeys(string $pattern): array
|
||||
{
|
||||
$values = new Collection();
|
||||
$redis = Redis::getFacadeRoot();
|
||||
|
||||
foreach (new Keyspace($redis, $pattern) as $key) {
|
||||
$parts = explode('/', $key);
|
||||
if (isset($parts[$position])) {
|
||||
$values->push($parts[$position]);
|
||||
if (isset($parts[3])) {
|
||||
$values->push($parts[3]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,7 +153,7 @@ class RedisClient
|
||||
* @param string $pattern
|
||||
* @return array
|
||||
*/
|
||||
private static function fetchValuesToArray(string $pattern): array
|
||||
private function fetchValuesToArray(string $pattern): array
|
||||
{
|
||||
$redis = Redis::getFacadeRoot();
|
||||
|
||||
@@ -174,7 +173,7 @@ class RedisClient
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
public static function flattenToDotArray(array $data): array
|
||||
public function flattenToDotArray(array $data): array
|
||||
{
|
||||
$collection = new Collection($data);
|
||||
|
||||
|
||||
75
tests/Controllers/ControllerTest.php
Normal file
75
tests/Controllers/ControllerTest.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Controllers;
|
||||
|
||||
use League\Route\Http\Exception\NotFoundException;
|
||||
use Nyholm\Psr7\ServerRequest;
|
||||
use PHPUnit\Framework\MockObject\Exception;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Siteworxpro\App\Controllers\Controller;
|
||||
use Siteworxpro\App\Facades\Logger;
|
||||
use Siteworxpro\App\Traefik\ProtocolEnum;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
class ControllerTest extends Unit
|
||||
{
|
||||
/**
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function testGet(): void
|
||||
{
|
||||
$fooController = new FooController();
|
||||
$request = new ServerRequest('GET', '/foo');
|
||||
|
||||
$this->expectException(NotFoundException::class);
|
||||
|
||||
$fooController->get($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function testPost(): void
|
||||
{
|
||||
$fooController = new FooController();
|
||||
$request = new ServerRequest('GET', '/foo');
|
||||
|
||||
$this->expectException(NotFoundException::class);
|
||||
|
||||
$fooController->post($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function testDelete(): void
|
||||
{
|
||||
$fooController = new FooController();
|
||||
$request = new ServerRequest('GET', '/foo');
|
||||
|
||||
$this->expectException(NotFoundException::class);
|
||||
|
||||
$fooController->delete($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotFoundException
|
||||
*/
|
||||
public function testPatch(): void
|
||||
{
|
||||
$fooController = new FooController();
|
||||
$request = new ServerRequest('GET', '/foo');
|
||||
|
||||
$this->expectException(NotFoundException::class);
|
||||
|
||||
$fooController->patch($request);
|
||||
}
|
||||
}
|
||||
|
||||
// This is a dummy controller for testing purposes
|
||||
// @codingStandardsIgnoreLine
|
||||
class FooController extends Controller
|
||||
{
|
||||
}
|
||||
145
tests/Controllers/MiddlewareControllerTest.php
Normal file
145
tests/Controllers/MiddlewareControllerTest.php
Normal file
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Controllers;
|
||||
|
||||
use Nyholm\Psr7\ServerRequest;
|
||||
use Siteworxpro\App\Controllers\MiddlewaresController;
|
||||
use Siteworxpro\App\Facades\RedisClient;
|
||||
use Siteworxpro\App\Traefik\EntityEnum;
|
||||
use Siteworxpro\App\Traefik\ProtocolEnum;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
class MiddlewareControllerTest extends Unit
|
||||
{
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testGet(): void
|
||||
{
|
||||
$request = new ServerRequest('GET', '/middlewares');
|
||||
|
||||
$request = $request->withAttribute('protocol', 'http');
|
||||
|
||||
RedisClient::expects('getAllMiddlewares')
|
||||
->with(ProtocolEnum::HTTP)
|
||||
->andReturn(['middleware1', 'middleware2']);
|
||||
|
||||
$controller = new MiddlewaresController();
|
||||
|
||||
$response = $controller->get($request);
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertEquals('["middleware1","middleware2"]', (string)$response->getBody());
|
||||
$this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testGetInd(): void
|
||||
{
|
||||
$request = new ServerRequest('GET', '/middlewares/foo');
|
||||
|
||||
$request = $request
|
||||
->withAttribute('protocol', 'http')
|
||||
->withAttribute('id', 'foo');
|
||||
|
||||
RedisClient::expects('getMiddleware')
|
||||
->with('foo', ProtocolEnum::HTTP)
|
||||
->andReturn([
|
||||
'traefik/http/middlewares/foo' => 'bar',
|
||||
]);
|
||||
|
||||
$controller = new MiddlewaresController();
|
||||
|
||||
$response = $controller->get($request);
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertEquals('{"traefik\/http\/middlewares\/foo":"bar"}', (string)$response->getBody());
|
||||
$this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testPost(): void
|
||||
{
|
||||
|
||||
$request = new ServerRequest('POST', '/middlewares');
|
||||
|
||||
$request = $request
|
||||
->withAttribute('protocol', 'http')
|
||||
->withAttribute('id', 'foo')
|
||||
->withParsedBody(['foo' => 'bar']);
|
||||
|
||||
RedisClient::expects('createOrReplace')
|
||||
->with('foo', ['foo' => 'bar'], EntityEnum::MIDDLEWARE, ProtocolEnum::HTTP)
|
||||
->andReturn(true);
|
||||
|
||||
$controller = new MiddlewaresController();
|
||||
$response = $controller->post($request);
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertEquals('{"message":"Middleware added successfully"}', (string)$response->getBody());
|
||||
}
|
||||
|
||||
public function testPostValidationFailure(): void
|
||||
{
|
||||
$request = new ServerRequest('POST', '/middlewares');
|
||||
|
||||
$request = $request
|
||||
->withAttribute('protocol', 'http')
|
||||
->withAttribute('id', 'foo')
|
||||
->withParsedBody([]);
|
||||
|
||||
RedisClient::expects('createOrReplace')
|
||||
->never();
|
||||
|
||||
$controller = new MiddlewaresController();
|
||||
$response = $controller->post($request);
|
||||
$this->assertEquals(400, $response->getStatusCode());
|
||||
$this->assertEquals('{"error":"Middleware is invalid"}', (string)$response->getBody());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testDelete(): void
|
||||
{
|
||||
$request = new ServerRequest('DELETE', '/middlewares/foo');
|
||||
|
||||
$request = $request
|
||||
->withAttribute('protocol', 'http')
|
||||
->withAttribute('id', 'foo');
|
||||
|
||||
RedisClient::expects('deleteAllKeys')
|
||||
->with('foo', EntityEnum::MIDDLEWARE, ProtocolEnum::HTTP)
|
||||
->andReturn(true);
|
||||
|
||||
$controller = new MiddlewaresController();
|
||||
$response = $controller->delete($request);
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertEquals('{"message":"Middleware deleted successfully"}', (string)$response->getBody());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testDeleteValidationFailure(): void
|
||||
{
|
||||
$request = new ServerRequest('DELETE', '/middlewares');
|
||||
|
||||
$request = $request
|
||||
->withAttribute('protocol', 'http')
|
||||
->withAttribute('id', '');
|
||||
|
||||
RedisClient::expects('deleteAllKeys')
|
||||
->never();
|
||||
|
||||
$controller = new MiddlewaresController();
|
||||
$response = $controller->delete($request);
|
||||
$this->assertEquals(400, $response->getStatusCode());
|
||||
$this->assertEquals('{"error":"Middleware is invalid"}', (string)$response->getBody());
|
||||
}
|
||||
}
|
||||
159
tests/Controllers/RoutesControllerTest.php
Normal file
159
tests/Controllers/RoutesControllerTest.php
Normal file
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Controllers;
|
||||
|
||||
use Nyholm\Psr7\ServerRequest;
|
||||
use Siteworxpro\App\Controllers\RoutesController;
|
||||
use Siteworxpro\App\Facades\RedisClient;
|
||||
use Siteworxpro\App\Traefik\EntityEnum;
|
||||
use Siteworxpro\App\Traefik\ProtocolEnum;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
class RoutesControllerTest extends Unit
|
||||
{
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testGet(): void
|
||||
{
|
||||
$request = new ServerRequest('GET', '/routes');
|
||||
|
||||
$request = $request->withAttribute('protocol', 'http');
|
||||
|
||||
RedisClient::expects('getAllRouters')
|
||||
->with(ProtocolEnum::HTTP)
|
||||
->andReturn(['route1', 'route2']);
|
||||
|
||||
$controller = new RoutesController();
|
||||
$response = $controller->get($request);
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertEquals('["route1","route2"]', (string)$response->getBody());
|
||||
$this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testGetInd(): void
|
||||
{
|
||||
$request = new ServerRequest('GET', '/routes/foo');
|
||||
|
||||
$request = $request
|
||||
->withAttribute('protocol', 'http')
|
||||
->withAttribute('id', 'foo');
|
||||
|
||||
RedisClient::expects('getRouter')
|
||||
->with('foo', ProtocolEnum::HTTP)
|
||||
->andReturn([
|
||||
'traefik/http/routers/foo' => 'bar',
|
||||
]);
|
||||
|
||||
$controller = new RoutesController();
|
||||
$response = $controller->get($request);
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertEquals('{"traefik\/http\/routers\/foo":"bar"}', (string)$response->getBody());
|
||||
$this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testDelete(): void
|
||||
{
|
||||
$request = new ServerRequest('DELETE', '/routes/foo');
|
||||
|
||||
$request = $request
|
||||
->withAttribute('protocol', 'http')
|
||||
->withAttribute('id', 'foo');
|
||||
|
||||
RedisClient::expects('deleteAllKeys')
|
||||
->with('foo', EntityEnum::ROUTER, ProtocolEnum::HTTP)
|
||||
->andReturn(true);
|
||||
|
||||
$controller = new RoutesController();
|
||||
$response = $controller->delete($request);
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertEquals('{"message":"Router deleted successfully"}', (string)$response->getBody());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testPost(): void
|
||||
{
|
||||
$request = new ServerRequest('POST', '/routes/foo');
|
||||
|
||||
$request = $request
|
||||
->withAttribute('protocol', 'http')
|
||||
->withAttribute('id', 'foo')
|
||||
->withParsedBody([
|
||||
'service' => 'my-service',
|
||||
'rule' => 'Host(`example.com`)',
|
||||
]);
|
||||
|
||||
RedisClient::expects('createOrReplace')
|
||||
->with(
|
||||
'foo',
|
||||
[
|
||||
'service' => 'my-service',
|
||||
'rule' => 'Host(`example.com`)',
|
||||
],
|
||||
EntityEnum::ROUTER,
|
||||
ProtocolEnum::HTTP
|
||||
)
|
||||
->andReturn(true);
|
||||
|
||||
$controller = new RoutesController();
|
||||
$response = $controller->post($request);
|
||||
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertEquals('{"message":"Router created successfully"}', (string)$response->getBody());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testPostWithoutService(): void
|
||||
{
|
||||
$request = new ServerRequest('POST', '/routes/foo');
|
||||
|
||||
$request = $request
|
||||
->withAttribute('protocol', 'http')
|
||||
->withAttribute('id', 'foo')
|
||||
->withParsedBody([
|
||||
'rule' => 'Host(`example.com`)',
|
||||
]);
|
||||
|
||||
$controller = new RoutesController();
|
||||
$response = $controller->post($request);
|
||||
|
||||
$this->assertEquals(400, $response->getStatusCode());
|
||||
$this->assertEquals('{"error":"Service is required"}', (string)$response->getBody());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testPostWithoutRule(): void
|
||||
{
|
||||
$request = new ServerRequest('POST', '/routes/foo');
|
||||
|
||||
$request = $request
|
||||
->withAttribute('protocol', 'http')
|
||||
->withAttribute('id', 'foo')
|
||||
->withParsedBody([
|
||||
'service' => 'my-service',
|
||||
]);
|
||||
|
||||
$controller = new RoutesController();
|
||||
$response = $controller->post($request);
|
||||
|
||||
$this->assertEquals(400, $response->getStatusCode());
|
||||
$this->assertEquals('{"error":"Rule is required"}', (string)$response->getBody());
|
||||
}
|
||||
}
|
||||
162
tests/Controllers/ServicesControllerTest.php
Normal file
162
tests/Controllers/ServicesControllerTest.php
Normal file
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Siteworxpro\Tests\Controllers;
|
||||
|
||||
use Nyholm\Psr7\ServerRequest;
|
||||
use Siteworxpro\App\Controllers\ServicesController;
|
||||
use Siteworxpro\App\Facades\RedisClient;
|
||||
use Siteworxpro\App\Traefik\EntityEnum;
|
||||
use Siteworxpro\App\Traefik\ProtocolEnum;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
class ServicesControllerTest extends Unit
|
||||
{
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testGet(): void
|
||||
{
|
||||
$request = new ServerRequest('GET', '/services');
|
||||
$request = $request->withAttribute('protocol', 'http');
|
||||
|
||||
RedisClient::expects('getAllServices')
|
||||
->with(ProtocolEnum::HTTP)
|
||||
->andReturn(['service1', 'service2']);
|
||||
|
||||
$controller = new ServicesController();
|
||||
$response = $controller->get($request);
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertEquals('["service1","service2"]', (string)$response->getBody());
|
||||
$this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testGetInd(): void
|
||||
{
|
||||
$request = new ServerRequest('GET', '/services/foo');
|
||||
$request = $request
|
||||
->withAttribute('protocol', 'http')
|
||||
->withAttribute('id', 'foo');
|
||||
|
||||
RedisClient::expects('getService')
|
||||
->with('foo', ProtocolEnum::HTTP)
|
||||
->andReturn([
|
||||
'traefik/http/services/foo' => 'bar',
|
||||
]);
|
||||
|
||||
$controller = new ServicesController();
|
||||
$response = $controller->get($request);
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertEquals('{"traefik\/http\/services\/foo":"bar"}', (string)$response->getBody());
|
||||
$this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testPost(): void
|
||||
{
|
||||
$request = new ServerRequest('POST', '/services/foo');
|
||||
$request = $request
|
||||
->withAttribute('protocol', 'http')
|
||||
->withAttribute('id', 'foo')
|
||||
->withParsedBody([
|
||||
'loadbalancer' => [
|
||||
'servers' => [
|
||||
['url' => 'https://example.com'],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
RedisClient::expects('createOrReplace')
|
||||
->with(
|
||||
'foo',
|
||||
[
|
||||
'loadbalancer' => [
|
||||
'servers' => [
|
||||
['url' => 'https://example.com'],
|
||||
],
|
||||
],
|
||||
],
|
||||
EntityEnum::SERVICE,
|
||||
ProtocolEnum::HTTP
|
||||
)
|
||||
->andReturn(true);
|
||||
|
||||
$controller = new ServicesController();
|
||||
$response = $controller->post($request);
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertEquals('{"message":"Service updated successfully"}', (string)$response->getBody());
|
||||
$this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testPostValidation(): void
|
||||
{
|
||||
$request = new ServerRequest('POST', '/services/foo');
|
||||
$request = $request
|
||||
->withAttribute('protocol', 'http')
|
||||
->withAttribute('id', 'foo')
|
||||
->withParsedBody([]);
|
||||
|
||||
RedisClient::expects('createOrReplace')
|
||||
->with(
|
||||
'foo',
|
||||
[],
|
||||
EntityEnum::SERVICE,
|
||||
ProtocolEnum::HTTP
|
||||
)
|
||||
->andReturn(true);
|
||||
|
||||
$controller = new ServicesController();
|
||||
$response = $controller->post($request);
|
||||
$this->assertEquals(400, $response->getStatusCode());
|
||||
$this->assertEquals('{"error":"loadbalancer is required"}', (string)$response->getBody());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testDelete(): void
|
||||
{
|
||||
$request = new ServerRequest('DELETE', '/services/foo');
|
||||
$request = $request
|
||||
->withAttribute('protocol', 'http')
|
||||
->withAttribute('id', 'foo');
|
||||
|
||||
RedisClient::expects('deleteAllKeys')
|
||||
->with('foo', EntityEnum::SERVICE, ProtocolEnum::HTTP)
|
||||
->andReturn(true);
|
||||
|
||||
$controller = new ServicesController();
|
||||
$response = $controller->delete($request);
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
$this->assertEquals('{"message":"Service deleted successfully"}', (string)$response->getBody());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \JsonException
|
||||
*/
|
||||
public function testDeleteValidation(): void
|
||||
{
|
||||
$request = new ServerRequest('DELETE', '/services');
|
||||
$request = $request
|
||||
->withAttribute('protocol', 'http')
|
||||
->withAttribute('id', null);
|
||||
|
||||
RedisClient::expects('deleteAllKeys')
|
||||
->with(null, EntityEnum::SERVICE, ProtocolEnum::HTTP)
|
||||
->andReturn(true);
|
||||
|
||||
$controller = new ServicesController();
|
||||
$response = $controller->delete($request);
|
||||
$this->assertEquals(400, $response->getStatusCode());
|
||||
$this->assertEquals('{"error":"Service name is required"}', (string)$response->getBody());
|
||||
}
|
||||
}
|
||||
@@ -48,7 +48,7 @@ class MiddlewareServiceTest extends Unit
|
||||
'/middlewares/foo/headers/customrequestheaders/X-Forwarded-Proto',
|
||||
]]);
|
||||
|
||||
$services = RedisClient::getAllMiddlewares($protocolEnum);
|
||||
$services = new RedisClient()->getAllMiddlewares($protocolEnum);
|
||||
$this->assertCount(1, $services);
|
||||
$this->assertSame('foo', $services[0]);
|
||||
}
|
||||
@@ -101,7 +101,7 @@ class MiddlewareServiceTest extends Unit
|
||||
'/middlewares/foo/headers/customrequestheaders/X-Forwarded-Proto')
|
||||
->andReturn('http');
|
||||
|
||||
$middleware = RedisClient::getMiddleware('foo', $protocolEnum);
|
||||
$middleware = new RedisClient()->getMiddleware('foo', $protocolEnum);
|
||||
|
||||
$this->assertCount(4, $middleware['headers']['customrequestheaders']);
|
||||
$this->assertSame('foo.localhost', $middleware['headers']['customrequestheaders']['Host']);
|
||||
|
||||
@@ -7,19 +7,13 @@ namespace Siteworxpro\Tests\Traefik;
|
||||
use Mockery;
|
||||
use Predis\Command\FactoryInterface;
|
||||
use Siteworxpro\App\Facades\Redis;
|
||||
use Siteworxpro\App\Facades\RedisClient;
|
||||
use Siteworxpro\App\Traefik\EntityEnum;
|
||||
use Siteworxpro\App\Traefik\ProtocolEnum;
|
||||
use Siteworxpro\App\Traefik\RedisClient;
|
||||
use Siteworxpro\Tests\Unit;
|
||||
|
||||
class RedisClientRoutersTest extends Unit
|
||||
{
|
||||
protected function tearDown(): void
|
||||
{
|
||||
parent::tearDown();
|
||||
Mockery::close();
|
||||
}
|
||||
|
||||
private function createTest(ProtocolEnum $protocol): void
|
||||
{
|
||||
$commandFactory = Mockery::mock(FactoryInterface::class)
|
||||
|
||||
@@ -54,7 +54,7 @@ class RedisClientServiceTest extends Unit
|
||||
->once()
|
||||
->andReturn(true);
|
||||
|
||||
RedisClient::createOrReplace('foo', [
|
||||
new RedisClient()->createOrReplace('foo', [
|
||||
'loadbalancer' => [
|
||||
'servers' => [
|
||||
'server1' => [
|
||||
@@ -103,7 +103,7 @@ class RedisClientServiceTest extends Unit
|
||||
->with('traefik/' . $protocol->getValue() . '/services/foo/loadbalancer/servers/server1/url')
|
||||
->andReturn('http://foo.localhost:80');
|
||||
|
||||
$services = RedisClient::getService('foo', $protocol);
|
||||
$services = new RedisClient()->getService('foo', $protocol);
|
||||
|
||||
$this->assertIsArray($services);
|
||||
$this->assertArrayHasKey('loadbalancer', $services);
|
||||
@@ -135,7 +135,7 @@ class RedisClientServiceTest extends Unit
|
||||
'traefik/' . $protocol->getValue() . '/services/bar/loadbalancer/servers/server2/url',
|
||||
]]);
|
||||
|
||||
$services = RedisClient::getAllServices($protocol);
|
||||
$services = new RedisClient()->getAllServices($protocol);
|
||||
|
||||
$this->assertIsArray($services);
|
||||
$this->assertCount(2, $services);
|
||||
|
||||
@@ -6,8 +6,10 @@ namespace Siteworxpro\Tests;
|
||||
|
||||
use Illuminate\Container\Container;
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Siteworxpro\App\Facades\Config;
|
||||
use Siteworxpro\App\Facades\RedisClient;
|
||||
|
||||
abstract class Unit extends TestCase
|
||||
{
|
||||
@@ -19,7 +21,12 @@ abstract class Unit extends TestCase
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
parent::tearDown();
|
||||
|
||||
Config::clearResolvedInstances();
|
||||
Redis::clearResolvedInstances();
|
||||
RedisClient::clearResolvedInstances();
|
||||
|
||||
Facade::setFacadeApplication(null);
|
||||
}
|
||||
}
|
||||
|
||||
1
tests/reports/.gitignore
vendored
1
tests/reports/.gitignore
vendored
@@ -1 +1,2 @@
|
||||
html/
|
||||
junit.xml
|
||||
Reference in New Issue
Block a user