Initial project structure with Slim skeleton

This commit is contained in:
Gregory Ballantine
2022-07-09 12:25:26 -04:00
commit 14735bd9a5
43 changed files with 4878 additions and 0 deletions

View File

@ -0,0 +1,78 @@
<?php
declare(strict_types=1);
namespace Tests\Application\Actions;
use App\Application\Actions\Action;
use App\Application\Actions\ActionPayload;
use DateTimeImmutable;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Log\LoggerInterface;
use Tests\TestCase;
class ActionTest extends TestCase
{
public function testActionSetsHttpCodeInRespond()
{
$app = $this->getAppInstance();
$container = $app->getContainer();
$logger = $container->get(LoggerInterface::class);
$testAction = new class($logger) extends Action {
public function __construct(
LoggerInterface $loggerInterface
) {
parent::__construct($loggerInterface);
}
public function action(): Response
{
return $this->respond(
new ActionPayload(
202,
[
'willBeDoneAt' => (new DateTimeImmutable())->format(DateTimeImmutable::ATOM)
]
)
);
}
};
$app->get('/test-action-response-code', $testAction);
$request = $this->createRequest('GET', '/test-action-response-code');
$response = $app->handle($request);
$this->assertEquals(202, $response->getStatusCode());
}
public function testActionSetsHttpCodeRespondData()
{
$app = $this->getAppInstance();
$container = $app->getContainer();
$logger = $container->get(LoggerInterface::class);
$testAction = new class($logger) extends Action {
public function __construct(
LoggerInterface $loggerInterface
) {
parent::__construct($loggerInterface);
}
public function action(): Response
{
return $this->respondWithData(
[
'willBeDoneAt' => (new DateTimeImmutable())->format(DateTimeImmutable::ATOM)
],
202
);
}
};
$app->get('/test-action-response-code', $testAction);
$request = $this->createRequest('GET', '/test-action-response-code');
$response = $app->handle($request);
$this->assertEquals(202, $response->getStatusCode());
}
}

View File

@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace Tests\Application\Actions\User;
use App\Application\Actions\ActionPayload;
use App\Domain\User\UserRepository;
use App\Domain\User\User;
use DI\Container;
use Tests\TestCase;
class ListUserActionTest extends TestCase
{
public function testAction()
{
$app = $this->getAppInstance();
/** @var Container $container */
$container = $app->getContainer();
$user = new User(1, 'bill.gates', 'Bill', 'Gates');
$userRepositoryProphecy = $this->prophesize(UserRepository::class);
$userRepositoryProphecy
->findAll()
->willReturn([$user])
->shouldBeCalledOnce();
$container->set(UserRepository::class, $userRepositoryProphecy->reveal());
$request = $this->createRequest('GET', '/users');
$response = $app->handle($request);
$payload = (string) $response->getBody();
$expectedPayload = new ActionPayload(200, [$user]);
$serializedPayload = json_encode($expectedPayload, JSON_PRETTY_PRINT);
$this->assertEquals($serializedPayload, $payload);
}
}

View File

@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace Tests\Application\Actions\User;
use App\Application\Actions\ActionError;
use App\Application\Actions\ActionPayload;
use App\Application\Handlers\HttpErrorHandler;
use App\Domain\User\User;
use App\Domain\User\UserNotFoundException;
use App\Domain\User\UserRepository;
use DI\Container;
use Slim\Middleware\ErrorMiddleware;
use Tests\TestCase;
class ViewUserActionTest extends TestCase
{
public function testAction()
{
$app = $this->getAppInstance();
/** @var Container $container */
$container = $app->getContainer();
$user = new User(1, 'bill.gates', 'Bill', 'Gates');
$userRepositoryProphecy = $this->prophesize(UserRepository::class);
$userRepositoryProphecy
->findUserOfId(1)
->willReturn($user)
->shouldBeCalledOnce();
$container->set(UserRepository::class, $userRepositoryProphecy->reveal());
$request = $this->createRequest('GET', '/users/1');
$response = $app->handle($request);
$payload = (string) $response->getBody();
$expectedPayload = new ActionPayload(200, $user);
$serializedPayload = json_encode($expectedPayload, JSON_PRETTY_PRINT);
$this->assertEquals($serializedPayload, $payload);
}
public function testActionThrowsUserNotFoundException()
{
$app = $this->getAppInstance();
$callableResolver = $app->getCallableResolver();
$responseFactory = $app->getResponseFactory();
$errorHandler = new HttpErrorHandler($callableResolver, $responseFactory);
$errorMiddleware = new ErrorMiddleware($callableResolver, $responseFactory, true, false, false);
$errorMiddleware->setDefaultErrorHandler($errorHandler);
$app->add($errorMiddleware);
/** @var Container $container */
$container = $app->getContainer();
$userRepositoryProphecy = $this->prophesize(UserRepository::class);
$userRepositoryProphecy
->findUserOfId(1)
->willThrow(new UserNotFoundException())
->shouldBeCalledOnce();
$container->set(UserRepository::class, $userRepositoryProphecy->reveal());
$request = $this->createRequest('GET', '/users/1');
$response = $app->handle($request);
$payload = (string) $response->getBody();
$expectedError = new ActionError(ActionError::RESOURCE_NOT_FOUND, 'The user you requested does not exist.');
$expectedPayload = new ActionPayload(404, null, $expectedError);
$serializedPayload = json_encode($expectedPayload, JSON_PRETTY_PRINT);
$this->assertEquals($serializedPayload, $payload);
}
}