From 0d8047eeaaedaa11d8d38829840f545baf633345 Mon Sep 17 00:00:00 2001 From: Neil Millard Date: Sat, 30 Mar 2024 15:08:58 +0000 Subject: [PATCH] Add GET applications endpoint --- app/routes.php | 5 +++ .../Actions/Application/ApplicationAction.php | 18 ++++++++ .../Application/ListApplicationAction.php | 19 +++++++++ .../Deployment/DeploymentRepository.php | 2 + .../Deployment/SqLiteDeploymentRepository.php | 8 ++++ .../Application/ListApplicationActionTest.php | 41 +++++++++++++++++++ .../SqLiteDeploymentRepositoryTest.php | 27 ++++++++++++ 7 files changed, 120 insertions(+) create mode 100644 src/Application/Actions/Application/ApplicationAction.php create mode 100644 src/Application/Actions/Application/ListApplicationAction.php create mode 100644 tests/Application/Actions/Application/ListApplicationActionTest.php diff --git a/app/routes.php b/app/routes.php index 5dd50a6..0fbd29d 100644 --- a/app/routes.php +++ b/app/routes.php @@ -2,6 +2,7 @@ declare(strict_types=1); +use App\Application\Actions\Application\ListApplicationAction; use App\Application\Actions\Deployment\CreateDeploymentAction; use App\Application\Actions\Deployment\DisplayDeploymentFormAction; use App\Application\Actions\Deployment\ListDeploymentAction; @@ -29,6 +30,10 @@ $group->get('/{id}', ViewDeploymentAction::class); }); + $app->group('/applications', function ($group) { + $group->get('', ListApplicationAction::class); + }); + $app->group('/users', function (Group $group) { $group->get('', ListUsersAction::class); $group->get('/{id}', ViewUserAction::class); diff --git a/src/Application/Actions/Application/ApplicationAction.php b/src/Application/Actions/Application/ApplicationAction.php new file mode 100644 index 0000000..9cd0b11 --- /dev/null +++ b/src/Application/Actions/Application/ApplicationAction.php @@ -0,0 +1,18 @@ +deploymentRepository = $deploymentRepository; + } +} diff --git a/src/Application/Actions/Application/ListApplicationAction.php b/src/Application/Actions/Application/ListApplicationAction.php new file mode 100644 index 0000000..79414cb --- /dev/null +++ b/src/Application/Actions/Application/ListApplicationAction.php @@ -0,0 +1,19 @@ +deploymentRepository->findApplications(); + $this->logger->info("Applications list was viewed."); + return $this->respondWithData($deployments); + } +} diff --git a/src/Domain/Deployment/DeploymentRepository.php b/src/Domain/Deployment/DeploymentRepository.php index 70bd776..09d72ad 100644 --- a/src/Domain/Deployment/DeploymentRepository.php +++ b/src/Domain/Deployment/DeploymentRepository.php @@ -27,4 +27,6 @@ public function findDeploymentwithApplication(string $application): array; * @return Deployment */ public function create(Deployment $deployment): Deployment; + + public function findApplications(): array; } diff --git a/src/Infrastructure/Persistence/Deployment/SqLiteDeploymentRepository.php b/src/Infrastructure/Persistence/Deployment/SqLiteDeploymentRepository.php index a687d3b..4c3925a 100644 --- a/src/Infrastructure/Persistence/Deployment/SqLiteDeploymentRepository.php +++ b/src/Infrastructure/Persistence/Deployment/SqLiteDeploymentRepository.php @@ -86,4 +86,12 @@ public function findDeploymentWithApplication(string $application): array $query->execute(); return $query->fetchAll(); } + + public function findApplications(): array + { + $query = $this->connection + ->prepare("SELECT DISTINCT application FROM deployments ORDER BY deployments.application"); + $query->execute(); + return $query->fetchAll(PDO::FETCH_COLUMN, 0); + } } diff --git a/tests/Application/Actions/Application/ListApplicationActionTest.php b/tests/Application/Actions/Application/ListApplicationActionTest.php new file mode 100644 index 0000000..a0607d7 --- /dev/null +++ b/tests/Application/Actions/Application/ListApplicationActionTest.php @@ -0,0 +1,41 @@ +getAppInstance(); + + /** @var Container $container */ + $container = $app->getContainer(); + $deployment = new Deployment(1, 'bill.gates', 'Frontend', '1.0.0', 'prod', 1); + + $deploymentRepositoryProphecy = $this->prophesize(DeploymentRepository::class); + $deploymentRepositoryProphecy + ->findApplications() + ->willReturn([$deployment->getApplication()]) + ->shouldBeCalledOnce(); + + $container->set(DeploymentRepository::class, $deploymentRepositoryProphecy->reveal()); + + //When + $request = $this->createRequest('GET', '/applications'); + $response = $app->handle($request); + + $payload = (string)$response->getBody(); + $expectedPayload = new ActionPayload(200, [$deployment->getApplication()]); + $serializedPayload = json_encode($expectedPayload, JSON_PRETTY_PRINT); + + //Then + $this->assertEquals($serializedPayload, $payload); + } +} diff --git a/tests/Infrastructure/Persistence/Deployment/SqLiteDeploymentRepositoryTest.php b/tests/Infrastructure/Persistence/Deployment/SqLiteDeploymentRepositoryTest.php index 8286bd0..c9571f8 100644 --- a/tests/Infrastructure/Persistence/Deployment/SqLiteDeploymentRepositoryTest.php +++ b/tests/Infrastructure/Persistence/Deployment/SqLiteDeploymentRepositoryTest.php @@ -122,4 +122,31 @@ public function testGetDeploymentResult() $result = $deploymentRepository->getDeploymentResult($deployment->jsonSerialize()); assertEquals($deployment->jsonSerialize(), $result->jsonSerialize()); } + + public function testFindApplications() + { + //Given + $deployment = new Deployment(1, 'bill.gates', 'Frontend', '1.0.0', 'prod', 2); + $pdoStatementProphecy = $this->prophesize(\PDOStatement::class); + $pdoStatementProphecy + ->execute(Argument::any()) + ->willReturn(true) + ->shouldBeCalledOnce(); + $pdoStatementProphecy + ->fetchAll(PDO::FETCH_COLUMN, 0) + ->willReturn([$deployment->getApplication()]) + ->shouldBeCalledOnce(); + + $pdoStatementObject = $pdoStatementProphecy->reveal(); + + $databaseProphecy = $this->prophesize(PDO::class); + $databaseProphecy + ->prepare(Argument::any()) + ->willReturn($pdoStatementObject) + ->shouldBeCalledOnce(); + //When + $deploymentRepository = new SqLiteDeploymentRepository($databaseProphecy->reveal()); + $result = $deploymentRepository->findApplications(); + assertEquals($deployment->getApplication(), $result[0]); + } }