Skip to content

Commit

Permalink
Start to add ApplicationDeployment database class
Browse files Browse the repository at this point in the history
  • Loading branch information
neilmillard committed Mar 31, 2024
1 parent 32c080d commit cf3d4db
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 1 deletion.
41 changes: 41 additions & 0 deletions src/Domain/Application/ApplicationDeployment.php
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 src/Domain/Application/ApplicationDeploymentRepository.php
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

View workflow job for this annotation

GitHub Actions / Tests PHP 8.1

PHPDoc tag @throws with type App\Infrastructure\Persistence\Application\ApplicationNotFoundException is not subtype of Throwable
}
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

View workflow job for this annotation

GitHub Actions / Tests PHP 8.1

Method App\Infrastructure\Persistence\Application\PDOApplicationDeploymentRepository::create() should return App\Domain\Application\ApplicationDeployment but return statement is missing.
}

/**
* @inheritDoc
*/
public function update(ApplicationDeployment $applicationDeployment): ApplicationDeployment
{
// TODO: Implement update() method.

Check failure on line 30 in src/Infrastructure/Persistence/Application/PDOApplicationDeploymentRepository.php

View workflow job for this annotation

GitHub Actions / Tests PHP 8.1

Method App\Infrastructure\Persistence\Application\PDOApplicationDeploymentRepository::update() should return App\Domain\Application\ApplicationDeployment but return statement is missing.
}

/**
* @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

View workflow job for this annotation

GitHub Actions / Tests PHP 8.1

Instantiated class App\Infrastructure\Persistence\Application\ApplicationNotFoundException not found.

Check failure on line 43 in src/Infrastructure/Persistence/Application/PDOApplicationDeploymentRepository.php

View workflow job for this annotation

GitHub Actions / Tests PHP 8.1

Throwing object of an unknown class App\Infrastructure\Persistence\Application\ApplicationNotFoundException.
}
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']
);
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit cf3d4db

Please sign in to comment.