Initial project structure with Slim skeleton
This commit is contained in:
78
tests/Application/Actions/ActionTest.php
Normal file
78
tests/Application/Actions/ActionTest.php
Normal 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());
|
||||
}
|
||||
}
|
40
tests/Application/Actions/User/ListUserActionTest.php
Normal file
40
tests/Application/Actions/User/ListUserActionTest.php
Normal 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);
|
||||
}
|
||||
}
|
79
tests/Application/Actions/User/ViewUserActionTest.php
Normal file
79
tests/Application/Actions/User/ViewUserActionTest.php
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user