Skip to content

Commit

Permalink
Add GET applications endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
neilmillard committed Mar 30, 2024
1 parent 205d236 commit 0d8047e
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions src/Application/Actions/Application/ApplicationAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Application\Actions\Application;

use App\Application\Actions\Action;
use App\Domain\Deployment\DeploymentRepository;
use Psr\Log\LoggerInterface;

abstract class ApplicationAction extends Action
{
protected DeploymentRepository $deploymentRepository;

public function __construct(LoggerInterface $logger, DeploymentRepository $deploymentRepository)
{
parent::__construct($logger);
$this->deploymentRepository = $deploymentRepository;
}
}
19 changes: 19 additions & 0 deletions src/Application/Actions/Application/ListApplicationAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Application\Actions\Application;

use App\Application\Actions\Application\ApplicationAction;
use Psr\Http\Message\ResponseInterface as Response;

class ListApplicationAction extends ApplicationAction
{
/**
* @inheritDoc
*/
protected function action(): Response
{
$deployments = $this->deploymentRepository->findApplications();
$this->logger->info("Applications list was viewed.");
return $this->respondWithData($deployments);
}
}
2 changes: 2 additions & 0 deletions src/Domain/Deployment/DeploymentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ public function findDeploymentwithApplication(string $application): array;
* @return Deployment
*/
public function create(Deployment $deployment): Deployment;

public function findApplications(): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Tests\Application\Actions\Application;

use App\Application\Actions\ActionPayload;
use App\Domain\Deployment\Deployment;
use App\Domain\Deployment\DeploymentRepository;
use DI\Container;
use Tests\TestCase;

class ListApplicationActionTest extends TestCase
{
public function testAction()
{
//Given
$app = $this->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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}

0 comments on commit 0d8047e

Please sign in to comment.