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()); } }