From cf3d4dbbc7f19bee6f2632311ca29bf3850f080e Mon Sep 17 00:00:00 2001 From: Neil Millard Date: Sun, 31 Mar 2024 23:31:25 +0100 Subject: [PATCH] Start to add ApplicationDeployment database class --- .../Application/ApplicationDeployment.php | 41 +++++++++++++ .../ApplicationDeploymentRepository.php | 28 +++++++++ .../PDOApplicationDeploymentRepository.php | 60 +++++++++++++++++++ ...PDOApplicationDeploymentRepositoryTest.php | 59 ++++++++++++++++++ .../SqLiteDeploymentRepositoryTest.php | 1 - 5 files changed, 188 insertions(+), 1 deletion(-) create mode 100644 src/Domain/Application/ApplicationDeployment.php create mode 100644 src/Domain/Application/ApplicationDeploymentRepository.php create mode 100644 src/Infrastructure/Persistence/Application/PDOApplicationDeploymentRepository.php create mode 100644 tests/Infrastructure/Persistence/Application/PDOApplicationDeploymentRepositoryTest.php diff --git a/src/Domain/Application/ApplicationDeployment.php b/src/Domain/Application/ApplicationDeployment.php new file mode 100644 index 0000000..e927af8 --- /dev/null +++ b/src/Domain/Application/ApplicationDeployment.php @@ -0,0 +1,41 @@ +name = strtolower($name); + $this->environment = strtolower($environment); + $this->deployment_id = $deployment_id; + } + + public function getName(): string + { + return $this->name; + } + + public function getEnvironment(): string + { + return $this->environment; + } + + public function getDeployment(): int + { + return $this->deployment_id; + } + + public function jsonSerialize(): array + { + return [ + 'name' => $this->name, + 'environment' => $this->environment, + 'deployment_id' => $this->deployment_id, + ]; + } +} diff --git a/src/Domain/Application/ApplicationDeploymentRepository.php b/src/Domain/Application/ApplicationDeploymentRepository.php new file mode 100644 index 0000000..fb7365a --- /dev/null +++ b/src/Domain/Application/ApplicationDeploymentRepository.php @@ -0,0 +1,28 @@ +connection = $connection; + } + /** + * @inheritDoc + */ + public function create(ApplicationDeployment $applicationDeployment): ApplicationDeployment + { + // TODO: Implement create() method. + } + + /** + * @inheritDoc + */ + public function update(ApplicationDeployment $applicationDeployment): ApplicationDeployment + { + // TODO: Implement update() method. + } + + /** + * @inheritDoc + */ + public function findApplicationDeployment(string $name, string $environment): ApplicationDeployment + { + $query = $this->connection + ->prepare("SELECT * FROM applications WHERE name=:name AND environment=:environment"); + $query->execute(['name' => $name, 'environment' => $environment]); + $result = $query->fetch(PDO::FETCH_ASSOC); + if (!$result) { + throw new ApplicationNotFoundException("Application $name in $environment not found"); + } + return $this->getApplicationDeploymentFromResult($result); + } + + /** + * @param mixed $result + * @return ApplicationDeployment + */ + private function getApplicationDeploymentFromResult(mixed $result): ApplicationDeployment + { + return new ApplicationDeployment( + $result['name'], + $result['environment'], + $result['deployment_id'] + ); + } +} diff --git a/tests/Infrastructure/Persistence/Application/PDOApplicationDeploymentRepositoryTest.php b/tests/Infrastructure/Persistence/Application/PDOApplicationDeploymentRepositoryTest.php new file mode 100644 index 0000000..c364bcf --- /dev/null +++ b/tests/Infrastructure/Persistence/Application/PDOApplicationDeploymentRepositoryTest.php @@ -0,0 +1,59 @@ +prophesize(\PDOStatement::class); + $pdoStatementProphecy + ->execute(['name' => 'frontend', 'environment' => 'prod']) + ->willReturn(true) + ->shouldBeCalledOnce(); + $pdoStatementProphecy + ->fetch(PDO::FETCH_ASSOC) + ->willReturn($applicationDeployment->jsonSerialize()) + ->shouldBeCalledOnce(); + + $pdoStatementObject = $pdoStatementProphecy->reveal(); + + $databaseProphecy = $this->prophesize(PDO::class); + $databaseProphecy + ->prepare(Argument::any()) + ->willReturn($pdoStatementObject) + ->shouldBeCalledOnce(); + //When + $deploymentRepository = new PDOApplicationDeploymentRepository($databaseProphecy->reveal()); + $result = $deploymentRepository->findApplicationDeployment( + $applicationDeployment->getName(), + $applicationDeployment->getEnvironment() + ); + assertEquals($applicationDeployment->getName(), $result->getName()); + assertEquals($applicationDeployment->getDeployment(), $result->getDeployment()); + } + + public function testCreate() + { + self::assertNull(null); + } +} diff --git a/tests/Infrastructure/Persistence/Deployment/SqLiteDeploymentRepositoryTest.php b/tests/Infrastructure/Persistence/Deployment/SqLiteDeploymentRepositoryTest.php index c9571f8..164a57f 100644 --- a/tests/Infrastructure/Persistence/Deployment/SqLiteDeploymentRepositoryTest.php +++ b/tests/Infrastructure/Persistence/Deployment/SqLiteDeploymentRepositoryTest.php @@ -4,7 +4,6 @@ use App\Domain\Deployment\Deployment; use App\Infrastructure\Persistence\Deployment\SqLiteDeploymentRepository; -use DI\Container; use PDO; use Prophecy\Argument; use Tests\TestCase;