conn = $this->createMock(Connection::class); $dbPlatform = $this->createMock(AbstractPlatform::class); $dbPlatform->method('getDummySelectSQL')->willReturn(''); $this->conn->method('getDatabasePlatform')->willReturn($dbPlatform); $em = $this->createMock(EntityManagerInterface::class); $em->method('getConnection')->willReturn($this->conn); $this->action = new HealthAction($em, new AppOptions(version: '1.2.3')); } /** @test */ public function passResponseIsReturnedWhenDummyQuerySucceeds(): void { $this->conn->expects($this->once())->method('executeQuery')->willReturn($this->createMock(Result::class)); /** @var JsonResponse $resp */ $resp = $this->action->handle(new ServerRequest()); $payload = $resp->getPayload(); self::assertEquals(200, $resp->getStatusCode()); self::assertEquals('pass', $payload['status']); self::assertEquals('1.2.3', $payload['version']); self::assertEquals([ 'about' => 'https://shlink.io', 'project' => 'https://github.com/shlinkio/shlink', ], $payload['links']); self::assertEquals('application/health+json', $resp->getHeaderLine('Content-type')); } /** @test */ public function failResponseIsReturnedWhenDummyQueryThrowsException(): void { $this->conn->expects($this->once())->method('executeQuery')->willThrowException(new Exception()); /** @var JsonResponse $resp */ $resp = $this->action->handle(new ServerRequest()); $payload = $resp->getPayload(); self::assertEquals(503, $resp->getStatusCode()); self::assertEquals('fail', $payload['status']); self::assertEquals('1.2.3', $payload['version']); self::assertEquals([ 'about' => 'https://shlink.io', 'project' => 'https://github.com/shlinkio/shlink', ], $payload['links']); self::assertEquals('application/health+json', $resp->getHeaderLine('Content-type')); } }