-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start to add ApplicationDeployment database class
- Loading branch information
1 parent
32c080d
commit cf3d4db
Showing
5 changed files
with
188 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace App\Domain\Application; | ||
|
||
class ApplicationDeployment | ||
{ | ||
private string $name; | ||
private string $environment; | ||
private int $deployment_id; | ||
|
||
public function __construct(string $name, string $environment, int $deployment_id) | ||
{ | ||
$this->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, | ||
]; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Domain/Application/ApplicationDeploymentRepository.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace App\Domain\Application; | ||
|
||
use App\Infrastructure\Persistence\Application\ApplicationNotFoundException; | ||
|
||
interface ApplicationDeploymentRepository | ||
{ | ||
/** | ||
* @param ApplicationDeployment $applicationDeployment | ||
* @return ApplicationDeployment | ||
*/ | ||
public function create(ApplicationDeployment $applicationDeployment): ApplicationDeployment; | ||
|
||
/** | ||
* @param ApplicationDeployment $applicationDeployment | ||
* @return ApplicationDeployment | ||
*/ | ||
public function update(ApplicationDeployment $applicationDeployment): ApplicationDeployment; | ||
|
||
/** | ||
* @param string $name | ||
* @param string $environment | ||
* @throws ApplicationNotFoundException | ||
* @return ApplicationDeployment | ||
*/ | ||
public function findApplicationDeployment(string $name, string $environment): ApplicationDeployment; | ||
Check failure on line 27 in src/Domain/Application/ApplicationDeploymentRepository.php GitHub Actions / Tests PHP 8.1
|
||
} |
60 changes: 60 additions & 0 deletions
60
src/Infrastructure/Persistence/Application/PDOApplicationDeploymentRepository.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace App\Infrastructure\Persistence\Application; | ||
|
||
use App\Domain\Application\ApplicationDeployment; | ||
use App\Domain\Application\ApplicationDeploymentRepository; | ||
use PDO; | ||
|
||
class PDOApplicationDeploymentRepository implements ApplicationDeploymentRepository | ||
{ | ||
private PDO $connection; | ||
|
||
public function __construct(PDO $connection) | ||
{ | ||
$this->connection = $connection; | ||
} | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public function create(ApplicationDeployment $applicationDeployment): ApplicationDeployment | ||
{ | ||
// TODO: Implement create() method. | ||
Check failure on line 22 in src/Infrastructure/Persistence/Application/PDOApplicationDeploymentRepository.php GitHub Actions / Tests PHP 8.1
|
||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function update(ApplicationDeployment $applicationDeployment): ApplicationDeployment | ||
{ | ||
// TODO: Implement update() method. | ||
Check failure on line 30 in src/Infrastructure/Persistence/Application/PDOApplicationDeploymentRepository.php GitHub Actions / Tests PHP 8.1
|
||
} | ||
|
||
/** | ||
* @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"); | ||
Check failure on line 43 in src/Infrastructure/Persistence/Application/PDOApplicationDeploymentRepository.php GitHub Actions / Tests PHP 8.1
|
||
} | ||
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'] | ||
); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
tests/Infrastructure/Persistence/Application/PDOApplicationDeploymentRepositoryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace Tests\Infrastructure\Persistence\Application; | ||
|
||
use App\Domain\Application\ApplicationDeployment; | ||
use App\Infrastructure\Persistence\Application\ApplicationNotFoundException; | ||
use App\Infrastructure\Persistence\Application\PDOApplicationDeploymentRepository; | ||
use PDO; | ||
use Prophecy\Argument; | ||
use Tests\TestCase; | ||
|
||
use function PHPUnit\Framework\assertEquals; | ||
|
||
class PDOApplicationDeploymentRepositoryTest extends TestCase | ||
{ | ||
public function testUpdate() | ||
{ | ||
self::assertNull(null); | ||
} | ||
|
||
/** | ||
* @throws ApplicationNotFoundException | ||
*/ | ||
public function testFindApplicationDeployment() | ||
{ | ||
//Given | ||
$applicationDeployment = new ApplicationDeployment('frontend', 'prod', 2); | ||
$pdoStatementProphecy = $this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters