Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
87.50% |
21 / 24 |
|
57.14% |
4 / 7 |
CRAP | |
0.00% |
0 / 1 |
| CmsFormSubmitRequest | |
87.50% |
21 / 24 |
|
57.14% |
4 / 7 |
10.20 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getExpectedTargetUrl | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| shouldDoRequestValidation | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| shouldDoResponseValidation | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| bootstrap | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getRequest | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| verifyValidResponse | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | namespace Apie\IntegrationTests\Requests; |
| 3 | |
| 4 | use Apie\Common\Interfaces\ApieFacadeInterface; |
| 5 | use Apie\Core\BoundedContext\BoundedContextId; |
| 6 | use Apie\Core\Entities\EntityInterface; |
| 7 | use Apie\Core\Identifiers\IdentifierInterface; |
| 8 | use Apie\Core\IdentifierUtils; |
| 9 | use Apie\Faker\Datalayers\FakerDatalayer; |
| 10 | use Apie\IntegrationTests\Apie\TypeDemo\Resources\User; |
| 11 | use Apie\IntegrationTests\Interfaces\TestApplicationInterface; |
| 12 | use Nyholm\Psr7\ServerRequest; |
| 13 | use PHPUnit\Framework\TestCase; |
| 14 | use Psr\Http\Message\ResponseInterface; |
| 15 | use Psr\Http\Message\ServerRequestInterface; |
| 16 | use ReflectionClass; |
| 17 | |
| 18 | class CmsFormSubmitRequest implements TestRequestInterface, BootstrapRequestInterface |
| 19 | { |
| 20 | private bool $faked = false; |
| 21 | private ApieFacadeInterface $apieFacade; |
| 22 | |
| 23 | /** |
| 24 | * @param class-string<EntityInterface> $resourceName |
| 25 | * @param array<int, EntityInterface> $entities |
| 26 | * @param array<string, mixed> $formData |
| 27 | */ |
| 28 | public function __construct( |
| 29 | private readonly BoundedContextId $boundedContextId, |
| 30 | private readonly string $resourceName, |
| 31 | private readonly string $id, |
| 32 | private readonly string $methodName, |
| 33 | private readonly array $entities, |
| 34 | private readonly array $formData, |
| 35 | private readonly ?string $expectedTargetUrl = null |
| 36 | ) { |
| 37 | } |
| 38 | |
| 39 | public function getExpectedTargetUrl(): ?string |
| 40 | { |
| 41 | return $this->expectedTargetUrl; |
| 42 | } |
| 43 | |
| 44 | public function shouldDoRequestValidation(): bool |
| 45 | { |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | public function shouldDoResponseValidation(): bool |
| 50 | { |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | public function bootstrap(TestApplicationInterface $testApplication): void |
| 55 | { |
| 56 | $this->apieFacade = $testApplication->getServiceContainer()->get('apie'); |
| 57 | foreach ($this->entities as $entity) { |
| 58 | $this->apieFacade->persistNew($entity, $this->boundedContextId); |
| 59 | } |
| 60 | $this->faked = $testApplication->getApplicationConfig()->getDatalayerImplementation()->name === FakerDatalayer::class; |
| 61 | } |
| 62 | |
| 63 | public function getRequest(): ServerRequestInterface |
| 64 | { |
| 65 | $url = 'http://localhost/cms/' . $this->boundedContextId . '/resource/action/' . (new ReflectionClass($this->resourceName))->getShortName() . '/' . $this->id . '/' . $this->methodName; |
| 66 | return new ServerRequest( |
| 67 | 'POST', |
| 68 | $url, |
| 69 | [ |
| 70 | 'content-type' => 'application/x-www-form-urlencoded', |
| 71 | ], |
| 72 | http_build_query($this->formData) |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | public function verifyValidResponse(ResponseInterface $response): void |
| 77 | { |
| 78 | TestCase::assertEquals(301, $response->getStatusCode()); |
| 79 | if (!$this->faked) { |
| 80 | /** @var IdentifierInterface<EntityInterface> $identifier */ |
| 81 | $identifier = IdentifierUtils::entityClassToIdentifier(new ReflectionClass($this->resourceName)) |
| 82 | ->getMethod('fromNative')->invoke(null, $this->id); |
| 83 | $entity = $this->apieFacade->find($identifier, $this->boundedContextId); |
| 84 | if ($entity instanceof User) { |
| 85 | TestCase::assertTrue($entity->isBlocked()); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |