Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
88.00% |
22 / 25 |
|
57.14% |
4 / 7 |
CRAP | |
0.00% |
0 / 1 |
| FormSubmitCall | |
88.00% |
22 / 25 |
|
57.14% |
4 / 7 |
9.14 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| bootstrap | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| isFakeDatalayer | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRequest | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| verifyValidResponse | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
2.01 | |||
| shouldDoRequestValidation | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| shouldDoResponseValidation | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Apie\IntegrationTests\Requests; |
| 4 | |
| 5 | use Apie\Common\Events\AddAuthenticationCookie; |
| 6 | use Apie\Common\IntegrationTestLogger; |
| 7 | use Apie\Core\BoundedContext\BoundedContextId; |
| 8 | use Apie\Core\Entities\EntityInterface; |
| 9 | use Apie\Faker\Datalayers\FakerDatalayer; |
| 10 | use Apie\IntegrationTests\Interfaces\TestApplicationInterface; |
| 11 | use Apie\IntegrationTests\Requests\JsonFields\JsonGetFieldInterface; |
| 12 | use Nyholm\Psr7\ServerRequest; |
| 13 | use PHPUnit\Framework\TestCase; |
| 14 | use Psr\Http\Message\ResponseInterface; |
| 15 | use Psr\Http\Message\ServerRequestInterface; |
| 16 | |
| 17 | class FormSubmitCall implements TestRequestInterface, BootstrapRequestInterface |
| 18 | { |
| 19 | private bool $faked; |
| 20 | |
| 21 | /** |
| 22 | * @param array<int, EntityInterface> $entities |
| 23 | */ |
| 24 | public function __construct( |
| 25 | private readonly string $url, |
| 26 | private readonly BoundedContextId $boundedContextId, |
| 27 | private readonly JsonGetFieldInterface $input, |
| 28 | private readonly string $expectedUrl, |
| 29 | private readonly array $entities = [], |
| 30 | ) { |
| 31 | } |
| 32 | |
| 33 | public function bootstrap(TestApplicationInterface $testApplication): void |
| 34 | { |
| 35 | $apieFacade = $testApplication->getServiceContainer()->get('apie'); |
| 36 | foreach ($this->entities as $entity) { |
| 37 | $apieFacade->persistNew($entity, $this->boundedContextId); |
| 38 | } |
| 39 | $this->faked = $testApplication->getApplicationConfig()->getDatalayerImplementation()->name === FakerDatalayer::class; |
| 40 | } |
| 41 | |
| 42 | public function isFakeDatalayer(): bool |
| 43 | { |
| 44 | return $this->faked; |
| 45 | } |
| 46 | |
| 47 | public function getRequest(): ServerRequestInterface |
| 48 | { |
| 49 | $data = $this->input->getInputValue() ?? []; |
| 50 | return new ServerRequest( |
| 51 | 'POST', |
| 52 | 'http://localhost/cms/' . $this->boundedContextId . '/' . $this->url, |
| 53 | [ |
| 54 | 'content-type' => 'application/x-www-form-urlencoded', |
| 55 | 'accept' => 'text/html' |
| 56 | ], |
| 57 | \http_build_query(['_csrf' => 'string', 'form' => $data]) |
| 58 | ); |
| 59 | } |
| 60 | |
| 61 | public function verifyValidResponse(ResponseInterface $response): void |
| 62 | { |
| 63 | $body = (string) $response->getBody(); |
| 64 | $statusCode = $response->getStatusCode(); |
| 65 | if ($statusCode === 500) { |
| 66 | IntegrationTestLogger::failTestShowError(); |
| 67 | } |
| 68 | TestCase::assertEquals(301, $statusCode, 'Expect status code 301, got: ' . $body); |
| 69 | TestCase::assertEquals($this->expectedUrl, $response->getHeaderLine('Location')); |
| 70 | TestCase::assertStringContainsString(AddAuthenticationCookie::COOKIE_NAME, $response->getHeaderLine('set-cookie')); |
| 71 | } |
| 72 | |
| 73 | public function shouldDoRequestValidation(): bool |
| 74 | { |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | public function shouldDoResponseValidation(): bool |
| 79 | { |
| 80 | return false; |
| 81 | } |
| 82 | } |