diff --git a/module/Rest/test/Action/HealthActionFactoryTest.php b/module/Rest/test/Action/HealthActionFactoryTest.php new file mode 100644 index 00000000..e5130d62 --- /dev/null +++ b/module/Rest/test/Action/HealthActionFactoryTest.php @@ -0,0 +1,45 @@ +factory = new Action\HealthActionFactory(); + } + + /** + * @test + */ + public function serviceIsCreatedExtractingConnectionFromEntityManager() + { + $em = $this->prophesize(EntityManager::class); + $conn = $this->prophesize(Connection::class); + + $getConnection = $em->getConnection()->willReturn($conn->reveal()); + + $sm = new ServiceManager(['services' => [ + 'Logger_Shlink' => $this->prophesize(LoggerInterface::class)->reveal(), + AppOptions::class => $this->prophesize(AppOptions::class)->reveal(), + EntityManager::class => $em->reveal(), + ]]); + + $instance = ($this->factory)($sm, ''); + + $this->assertInstanceOf(Action\HealthAction::class, $instance); + $getConnection->shouldHaveBeenCalledOnce(); + } +} diff --git a/module/Rest/test/Action/HealthActionTest.php b/module/Rest/test/Action/HealthActionTest.php new file mode 100644 index 00000000..93371876 --- /dev/null +++ b/module/Rest/test/Action/HealthActionTest.php @@ -0,0 +1,70 @@ +conn = $this->prophesize(Connection::class); + $this->action = new HealthAction($this->conn->reveal(), new AppOptions(['version' => '1.2.3'])); + } + + /** + * @test + */ + public function passResponseIsReturnedWhenConnectionSucceeds() + { + $ping = $this->conn->ping()->willReturn(true); + + /** @var JsonResponse $resp */ + $resp = $this->action->handle(new ServerRequest()); + $payload = $resp->getPayload(); + + $this->assertEquals(200, $resp->getStatusCode()); + $this->assertEquals('pass', $payload['status']); + $this->assertEquals('1.2.3', $payload['version']); + $this->assertEquals([ + 'about' => 'https://shlink.io', + 'project' => 'https://github.com/shlinkio/shlink', + ], $payload['links']); + $this->assertEquals('application/health+json', $resp->getHeaderLine('Content-type')); + $ping->shouldHaveBeenCalledOnce(); + } + + /** + * @test + */ + public function failResponseIsReturnedWhenConnectionFails() + { + $ping = $this->conn->ping()->willReturn(false); + + /** @var JsonResponse $resp */ + $resp = $this->action->handle(new ServerRequest()); + $payload = $resp->getPayload(); + + $this->assertEquals(503, $resp->getStatusCode()); + $this->assertEquals('fail', $payload['status']); + $this->assertEquals('1.2.3', $payload['version']); + $this->assertEquals([ + 'about' => 'https://shlink.io', + 'project' => 'https://github.com/shlinkio/shlink', + ], $payload['links']); + $this->assertEquals('application/health+json', $resp->getHeaderLine('Content-type')); + $ping->shouldHaveBeenCalledOnce(); + } +}