Files

94 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
namespace TorstenHettstedt\OptLogin\Tests\unit\Application\Actions;
use DateTimeImmutable;
use DateTimeInterface;
use Exception;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Log\LoggerInterface;
use TorstenHettstedt\OptLogin\Application\Actions\Action;
use TorstenHettstedt\OptLogin\Application\Actions\ActionPayload;
use TorstenHettstedt\OptLogin\Tests\unit\TestCase;
class ActionTest extends TestCase
{
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
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(DateTimeInterface::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());
}
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
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(DateTimeInterface::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());
}
}