You've already forked Traefik-Redis-Api
Is there an award for this?
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
* @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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class FooController extends Controller
|
||||
{
|
||||
|
||||
}
|
||||
146
tests/Controllers/MiddlewareControllerTest.php
Normal file
146
tests/Controllers/MiddlewareControllerTest.php
Normal file
@@ -0,0 +1,146 @@
|
||||
<?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());
|
||||
}
|
||||
}
|
||||
160
tests/Controllers/RoutesControllerTest.php
Normal file
160
tests/Controllers/RoutesControllerTest.php
Normal file
@@ -0,0 +1,160 @@
|
||||
<?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());
|
||||
}
|
||||
}
|
||||
163
tests/Controllers/ServicesControllerTest.php
Normal file
163
tests/Controllers/ServicesControllerTest.php
Normal file
@@ -0,0 +1,163 @@
|
||||
<?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']);
|
||||
|
||||
@@ -44,7 +44,7 @@ class RedisClientRoutersTest extends Unit
|
||||
->once()
|
||||
->andReturn(true);
|
||||
|
||||
RedisClient::createOrReplace('foo', [
|
||||
new RedisClient()->createOrReplace('foo', [
|
||||
'rule' => 'Host(`foo.localhost`)',
|
||||
'service' => 'foo',
|
||||
], EntityEnum::ROUTER, $protocol);
|
||||
@@ -108,7 +108,7 @@ class RedisClientRoutersTest extends Unit
|
||||
->with('traefik/' . $protocolEnum->getValue() . '/routers/foo/service')
|
||||
->andReturn('foo');
|
||||
|
||||
$routers = RedisClient::getRouter('foo', $protocolEnum);
|
||||
$routers = new RedisClient()->getRouter('foo', $protocolEnum);
|
||||
|
||||
$this->assertIsArray($routers);
|
||||
|
||||
@@ -138,7 +138,7 @@ class RedisClientRoutersTest extends Unit
|
||||
'traefik/' . $protocol->getValue() . '/routers/bar/url/0',
|
||||
]]);
|
||||
|
||||
$routers = RedisClient::getAllRouters($protocol);
|
||||
$routers = new RedisClient()->getAllRouters($protocol);
|
||||
|
||||
$this->assertIsArray($routers);
|
||||
$this->assertCount(2, $routers);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user