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
2namespace Apie\Common\Tests\Concerns;
3
4use Apie\Common\ApieFacade;
5use Apie\Common\Enums\UrlPrefix;
6use Apie\Common\Interfaces\ApieFacadeInterface;
7use Apie\Common\Interfaces\HasActionDefinition;
8use Apie\Common\Interfaces\HasRouteDefinition;
9use Apie\Common\Interfaces\RouteDefinitionProviderInterface;
10use Apie\Common\Lists\UrlPrefixList;
11use Apie\Common\RouteDefinitions\ActionHashmap;
12use Apie\Core\Actions\ActionInterface;
13use Apie\Core\BoundedContext\BoundedContext;
14use Apie\Core\BoundedContext\BoundedContextHashmap;
15use Apie\Core\BoundedContext\BoundedContextId;
16use Apie\Core\Context\ApieContext;
17use Apie\Core\Enums\RequestMethod;
18use Apie\Core\ValueObjects\UrlRouteDefinition;
19use Apie\Fixtures\BoundedContextFactory;
20use Apie\Fixtures\TestHelpers\TestWithInMemoryDatalayer;
21use Apie\Serializer\Serializer;
22use 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 */
36trait 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}