You've already forked Traefik-Redis-Api
Is there an award for this?
This commit is contained in:
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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user