Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | n/a |
0 / 0 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
|||
| ProvidesApieFacade | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | n/a |
0 / 0 |
|||
| givenAnApieFacade | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| 1 | <?php |
| 2 | namespace Apie\Common\Tests\Concerns; |
| 3 | |
| 4 | use Apie\Common\ApieFacade; |
| 5 | use Apie\Common\Enums\UrlPrefix; |
| 6 | use Apie\Common\Interfaces\ApieFacadeInterface; |
| 7 | use Apie\Common\Interfaces\HasActionDefinition; |
| 8 | use Apie\Common\Interfaces\HasRouteDefinition; |
| 9 | use Apie\Common\Interfaces\RouteDefinitionProviderInterface; |
| 10 | use Apie\Common\Lists\UrlPrefixList; |
| 11 | use Apie\Common\RouteDefinitions\ActionHashmap; |
| 12 | use Apie\Core\Actions\ActionInterface; |
| 13 | use Apie\Core\BoundedContext\BoundedContext; |
| 14 | use Apie\Core\BoundedContext\BoundedContextHashmap; |
| 15 | use Apie\Core\BoundedContext\BoundedContextId; |
| 16 | use Apie\Core\Context\ApieContext; |
| 17 | use Apie\Core\Enums\RequestMethod; |
| 18 | use Apie\Core\ValueObjects\UrlRouteDefinition; |
| 19 | use Apie\Fixtures\BoundedContextFactory; |
| 20 | use Apie\Fixtures\TestHelpers\TestWithInMemoryDatalayer; |
| 21 | use Apie\Serializer\Serializer; |
| 22 | use LogicException; |
| 23 | |
| 24 | /** |
| 25 | * Creates an Apie Facade for testing actions. |
| 26 | * |
| 27 | * In tests you would write it often like this: |
| 28 | * ```php |
| 29 | * $apieFacade = $this->givenAnApieFacade(ActionClass::class); |
| 30 | * $context = new ApieContext(); |
| 31 | * $action = $apieFacade->getAction('default', 'test', $context); |
| 32 | * ``` |
| 33 | * |
| 34 | * @codeCoverageIgnore |
| 35 | */ |
| 36 | trait ProvidesApieFacade |
| 37 | { |
| 38 | use TestWithInMemoryDatalayer; |
| 39 | |
| 40 | /** @param class-string<ActionInterface> $apieFacadeActionClass */ |
| 41 | public function givenAnApieFacade(string $apieFacadeActionClass, ?BoundedContextHashmap $boundedContextHashmap = null): ApieFacadeInterface |
| 42 | { |
| 43 | $routeDefinitionProvider = new class($apieFacadeActionClass) implements RouteDefinitionProviderInterface { |
| 44 | /** @param class-string<ActionInterface> $apieFacadeActionClass */ |
| 45 | public function __construct(private readonly string $apieFacadeActionClass) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | public function getActionsForBoundedContext(BoundedContext $boundedContext, ApieContext $apieContext): ActionHashmap |
| 50 | { |
| 51 | $routeDefinition = new class($this->apieFacadeActionClass) implements HasRouteDefinition, HasActionDefinition { |
| 52 | /** @param class-string<ActionInterface> $apieFacadeActionClass */ |
| 53 | public function __construct(private readonly string $apieFacadeActionClass) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | public function getMethod(): RequestMethod |
| 58 | { |
| 59 | return RequestMethod::GET; |
| 60 | } |
| 61 | |
| 62 | public function getUrl(): UrlRouteDefinition |
| 63 | { |
| 64 | return new UrlRouteDefinition('/test'); |
| 65 | } |
| 66 | |
| 67 | public function getController(): string |
| 68 | { |
| 69 | throw new LogicException('not implemented'); |
| 70 | } |
| 71 | public function getRouteAttributes(): array |
| 72 | { |
| 73 | return []; |
| 74 | } |
| 75 | |
| 76 | public function getAction(): string |
| 77 | { |
| 78 | return $this->apieFacadeActionClass; |
| 79 | } |
| 80 | |
| 81 | public function getOperationId(): string |
| 82 | { |
| 83 | return 'test'; |
| 84 | } |
| 85 | |
| 86 | public function getUrlPrefixes(): UrlPrefixList |
| 87 | { |
| 88 | return new UrlPrefixList([UrlPrefix::CMS]); |
| 89 | } |
| 90 | }; |
| 91 | return new ActionHashmap( |
| 92 | [ |
| 93 | 'test' => $routeDefinition |
| 94 | ] |
| 95 | ); |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | return new ApieFacade( |
| 100 | $routeDefinitionProvider, |
| 101 | $boundedContextHashmap ?? BoundedContextFactory::createHashmap(), |
| 102 | Serializer::create(), |
| 103 | $this->givenAnInMemoryDataLayer(new BoundedContextId('default')) |
| 104 | ); |
| 105 | } |
| 106 | } |