Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
89.29% |
75 / 84 |
|
50.00% |
6 / 12 |
CRAP | |
0.00% |
0 / 1 |
| SymfonyTestApplication | |
89.29% |
75 / 84 |
|
50.00% |
6 / 12 |
21.54 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getApplicationConfig | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getConsoleApplication | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| bootApplication | |
96.77% |
30 / 31 |
|
0.00% |
0 / 1 |
2 | |||
| getServiceContainer | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| cleanApplication | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| httpRequestGet | |
71.43% |
10 / 14 |
|
0.00% |
0 / 1 |
3.21 | |||
| httpRequest | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
| handleResponse | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
5.01 | |||
| loginAs | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| logout | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLoggedInAs | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
2.02 | |||
| 1 | <?php |
| 2 | namespace Apie\IntegrationTests\Applications\Symfony; |
| 3 | |
| 4 | use Apie\Common\Events\AddAuthenticationCookie; |
| 5 | use Apie\Common\IntegrationTestLogger; |
| 6 | use Apie\Common\ValueObjects\DecryptedAuthenticatedUser; |
| 7 | use Apie\Common\Wrappers\TextEncrypter; |
| 8 | use Apie\IntegrationTests\Concerns\ItRunsApplications; |
| 9 | use Apie\IntegrationTests\Config\ApplicationConfig; |
| 10 | use Apie\IntegrationTests\Config\BoundedContextConfig; |
| 11 | use Apie\IntegrationTests\Interfaces\TestApplicationInterface; |
| 12 | use Apie\IntegrationTests\Requests\TestRequestInterface; |
| 13 | use Nyholm\Psr7\Factory\Psr17Factory as NyholmPsr17Factory; |
| 14 | use Psr\Container\ContainerInterface; |
| 15 | use Psr\Http\Message\ResponseInterface; |
| 16 | use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; |
| 17 | use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory; |
| 18 | use Symfony\Bundle\FrameworkBundle\Console\Application as ConsoleApplication; |
| 19 | use Symfony\Component\Console\Application; |
| 20 | use Symfony\Component\HttpFoundation\Request; |
| 21 | use Symfony\Component\HttpFoundation\Response; |
| 22 | use Symfony\Component\HttpFoundation\ResponseHeaderBag; |
| 23 | |
| 24 | class SymfonyTestApplication implements TestApplicationInterface |
| 25 | { |
| 26 | use ItRunsApplications; |
| 27 | |
| 28 | private ?SymfonyTestingKernel $kernel = null; |
| 29 | |
| 30 | private ?string $authenticationCookie = null; |
| 31 | |
| 32 | /** |
| 33 | * @var array<string, string> |
| 34 | */ |
| 35 | private array $cookies = []; |
| 36 | |
| 37 | public function __construct( |
| 38 | private readonly ApplicationConfig $applicationConfig, |
| 39 | private readonly BoundedContextConfig $boundedContextConfig |
| 40 | ) { |
| 41 | } |
| 42 | |
| 43 | public function getApplicationConfig(): ApplicationConfig |
| 44 | { |
| 45 | return $this->applicationConfig; |
| 46 | } |
| 47 | |
| 48 | public function getConsoleApplication(): Application |
| 49 | { |
| 50 | $application = new ConsoleApplication($this->kernel); |
| 51 | $application->setAutoExit(false); |
| 52 | return $application; |
| 53 | } |
| 54 | |
| 55 | public function bootApplication(): void |
| 56 | { |
| 57 | IntegrationTestLogger::resetLoggedException(); |
| 58 | if ($this->kernel) { |
| 59 | return; |
| 60 | } |
| 61 | $boundedContexts = $this->boundedContextConfig->toArray(); |
| 62 | $this->authenticationCookie = null; |
| 63 | $this->cookies = []; |
| 64 | $this->kernel = new SymfonyTestingKernel( |
| 65 | [ |
| 66 | 'bounded_contexts' => $boundedContexts, |
| 67 | 'datalayers' => [ |
| 68 | 'default_datalayer' => $this->applicationConfig->getDatalayerImplementation()->name, |
| 69 | ], |
| 70 | 'encryption_key' => 'test', |
| 71 | 'enable_mcp_server' => true, |
| 72 | 'remote_mcp_path' => '/mcp', |
| 73 | 'enable_doctrine_bundle_connection' => true, |
| 74 | 'enable_security' => $this->applicationConfig->doesIncludeSecurity(), |
| 75 | 'doctrine' => [ |
| 76 | 'run_migrations' => true, |
| 77 | 'connection_params' => [ |
| 78 | 'driver' => 'pdo_sqlite' |
| 79 | ] |
| 80 | ], |
| 81 | 'open_api' => [ |
| 82 | 'max_enum_size' => 500, |
| 83 | ], |
| 84 | ], |
| 85 | $this->applicationConfig->doesIncludeTemplating(), |
| 86 | $this->applicationConfig->doesIncludeSecurity(), |
| 87 | ); |
| 88 | $this->kernel->boot(); |
| 89 | } |
| 90 | |
| 91 | public function getServiceContainer(): ContainerInterface |
| 92 | { |
| 93 | return $this->kernel->getContainer(); |
| 94 | } |
| 95 | |
| 96 | public function cleanApplication(): void |
| 97 | { |
| 98 | $this->kernel = null; |
| 99 | } |
| 100 | |
| 101 | public function httpRequestGet(string $uri): ResponseInterface |
| 102 | { |
| 103 | $parameters = []; |
| 104 | $parameterRaw = parse_url($uri, PHP_URL_QUERY); |
| 105 | if ($parameterRaw) { |
| 106 | parse_str($parameterRaw, $parameters); |
| 107 | } |
| 108 | $uri = parse_url($uri, PHP_URL_PATH); |
| 109 | $sfRequest = Request::create($uri, parameters: $parameters); |
| 110 | $sfRequest->cookies->add($this->cookies); |
| 111 | if ($this->authenticationCookie) { |
| 112 | $sfRequest->cookies->set( |
| 113 | AddAuthenticationCookie::COOKIE_NAME, |
| 114 | $this->authenticationCookie |
| 115 | ); |
| 116 | } |
| 117 | $sfResponse = $this->kernel->handle($sfRequest); |
| 118 | return $this->handleResponse($sfResponse); |
| 119 | } |
| 120 | |
| 121 | public function httpRequest(TestRequestInterface $testRequest): ResponseInterface |
| 122 | { |
| 123 | $psrRequest = $testRequest->getRequest(); |
| 124 | $factory = new HttpFoundationFactory(); |
| 125 | $sfRequest = $factory->createRequest($psrRequest); |
| 126 | $sfRequest->cookies->add($this->cookies); |
| 127 | if ($this->authenticationCookie) { |
| 128 | $sfRequest->cookies->set( |
| 129 | AddAuthenticationCookie::COOKIE_NAME, |
| 130 | $this->authenticationCookie |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | $sfResponse = $this->kernel->handle($sfRequest); |
| 135 | return $this->handleResponse($sfResponse); |
| 136 | } |
| 137 | |
| 138 | private function handleResponse(Response $sfResponse): ResponseInterface |
| 139 | { |
| 140 | $authenticationCookie = null; |
| 141 | foreach ($sfResponse->headers->getCookies(ResponseHeaderBag::COOKIES_FLAT) as $cookie) { |
| 142 | if ($cookie->getName() === AddAuthenticationCookie::COOKIE_NAME) { |
| 143 | $authenticationCookie = $cookie->isCleared() ? null : $cookie->getValue(); |
| 144 | continue; |
| 145 | } |
| 146 | if ($cookie->isCleared()) { |
| 147 | unset($this->cookies[$cookie->getName()]); |
| 148 | } else { |
| 149 | $this->cookies[$cookie->getName()] = $cookie->getValue(); |
| 150 | } |
| 151 | } |
| 152 | $this->authenticationCookie = $authenticationCookie; |
| 153 | |
| 154 | $psrFactory = new NyholmPsr17Factory(); |
| 155 | $factory = new PsrHttpFactory($psrFactory, $psrFactory, $psrFactory, $psrFactory); |
| 156 | return $factory->createResponse($sfResponse); |
| 157 | } |
| 158 | |
| 159 | public function loginAs(DecryptedAuthenticatedUser $user): void |
| 160 | { |
| 161 | $textEncrypter = new TextEncrypter('test'); |
| 162 | $this->authenticationCookie = $textEncrypter->encrypt($user->toNative()); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Forget that you are logged in. |
| 167 | */ |
| 168 | public function logout(): void |
| 169 | { |
| 170 | $this->authenticationCookie = null; |
| 171 | } |
| 172 | |
| 173 | public function getLoggedInAs(): ?DecryptedAuthenticatedUser |
| 174 | { |
| 175 | if (empty($this->authenticationCookie)) { |
| 176 | return null; |
| 177 | } |
| 178 | $textEncrypter = new TextEncrypter('test'); |
| 179 | return DecryptedAuthenticatedUser::fromNative( |
| 180 | $textEncrypter->decrypt($this->authenticationCookie), |
| 181 | ); |
| 182 | } |
| 183 | } |