Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
89.33% |
67 / 75 |
|
58.33% |
7 / 12 |
CRAP | |
0.00% |
0 / 1 |
| SymfonyTestApplication | |
89.33% |
67 / 75 |
|
58.33% |
7 / 12 |
18.39 | |
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.30% |
26 / 27 |
|
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 | |
69.23% |
9 / 13 |
|
0.00% |
0 / 1 |
3.26 | |||
| httpRequest | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
| handleResponse | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| 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\Cookie; |
| 21 | use Symfony\Component\HttpFoundation\Request; |
| 22 | use Symfony\Component\HttpFoundation\Response; |
| 23 | use Symfony\Component\HttpFoundation\ResponseHeaderBag; |
| 24 | |
| 25 | class SymfonyTestApplication implements TestApplicationInterface |
| 26 | { |
| 27 | use ItRunsApplications; |
| 28 | |
| 29 | private ?SymfonyTestingKernel $kernel = null; |
| 30 | |
| 31 | private ?string $authenticationCookie = null; |
| 32 | |
| 33 | public function __construct( |
| 34 | private readonly ApplicationConfig $applicationConfig, |
| 35 | private readonly BoundedContextConfig $boundedContextConfig |
| 36 | ) { |
| 37 | } |
| 38 | |
| 39 | public function getApplicationConfig(): ApplicationConfig |
| 40 | { |
| 41 | return $this->applicationConfig; |
| 42 | } |
| 43 | |
| 44 | public function getConsoleApplication(): Application |
| 45 | { |
| 46 | $application = new ConsoleApplication($this->kernel); |
| 47 | $application->setAutoExit(false); |
| 48 | return $application; |
| 49 | } |
| 50 | |
| 51 | public function bootApplication(): void |
| 52 | { |
| 53 | IntegrationTestLogger::resetLoggedException(); |
| 54 | if ($this->kernel) { |
| 55 | return; |
| 56 | } |
| 57 | $boundedContexts = $this->boundedContextConfig->toArray(); |
| 58 | $this->authenticationCookie = null; |
| 59 | $this->kernel = new SymfonyTestingKernel( |
| 60 | [ |
| 61 | 'bounded_contexts' => $boundedContexts, |
| 62 | 'datalayers' => [ |
| 63 | 'default_datalayer' => $this->applicationConfig->getDatalayerImplementation()->name, |
| 64 | ], |
| 65 | 'encryption_key' => 'test', |
| 66 | 'enable_mcp_server' => true, |
| 67 | 'remote_mcp_path' => '/mcp', |
| 68 | 'enable_doctrine_bundle_connection' => true, |
| 69 | 'enable_security' => $this->applicationConfig->doesIncludeSecurity(), |
| 70 | 'doctrine' => [ |
| 71 | 'run_migrations' => true, |
| 72 | 'connection_params' => [ |
| 73 | 'driver' => 'pdo_sqlite' |
| 74 | ] |
| 75 | ], |
| 76 | ], |
| 77 | $this->applicationConfig->doesIncludeTemplating(), |
| 78 | $this->applicationConfig->doesIncludeSecurity(), |
| 79 | ); |
| 80 | $this->kernel->boot(); |
| 81 | } |
| 82 | |
| 83 | public function getServiceContainer(): ContainerInterface |
| 84 | { |
| 85 | return $this->kernel->getContainer(); |
| 86 | } |
| 87 | |
| 88 | public function cleanApplication(): void |
| 89 | { |
| 90 | $this->kernel = null; |
| 91 | } |
| 92 | |
| 93 | public function httpRequestGet(string $uri): ResponseInterface |
| 94 | { |
| 95 | $parameters = []; |
| 96 | $parameterRaw = parse_url($uri, PHP_URL_QUERY); |
| 97 | if ($parameterRaw) { |
| 98 | parse_str($parameterRaw, $parameters); |
| 99 | } |
| 100 | $uri = parse_url($uri, PHP_URL_PATH); |
| 101 | $sfRequest = Request::create($uri, parameters: $parameters); |
| 102 | if ($this->authenticationCookie) { |
| 103 | $sfRequest->cookies->set( |
| 104 | AddAuthenticationCookie::COOKIE_NAME, |
| 105 | $this->authenticationCookie |
| 106 | ); |
| 107 | } |
| 108 | $sfResponse = $this->kernel->handle($sfRequest); |
| 109 | return $this->handleResponse($sfResponse); |
| 110 | } |
| 111 | |
| 112 | public function httpRequest(TestRequestInterface $testRequest): ResponseInterface |
| 113 | { |
| 114 | $psrRequest = $testRequest->getRequest(); |
| 115 | $factory = new HttpFoundationFactory(); |
| 116 | $sfRequest = $factory->createRequest($psrRequest); |
| 117 | if ($this->authenticationCookie) { |
| 118 | $sfRequest->cookies->set( |
| 119 | AddAuthenticationCookie::COOKIE_NAME, |
| 120 | $this->authenticationCookie |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | $sfResponse = $this->kernel->handle($sfRequest); |
| 125 | return $this->handleResponse($sfResponse); |
| 126 | } |
| 127 | |
| 128 | private function handleResponse(Response $sfResponse): ResponseInterface |
| 129 | { |
| 130 | $cookie = $sfResponse->headers->getCookies( |
| 131 | ResponseHeaderBag::COOKIES_ARRAY |
| 132 | )[""]["/"][AddAuthenticationCookie::COOKIE_NAME] ?? null; |
| 133 | if ($cookie !== null) { |
| 134 | $cookie = Cookie::fromString($cookie)->getValue(); |
| 135 | } |
| 136 | $this->authenticationCookie = $cookie; |
| 137 | |
| 138 | $psrFactory = new NyholmPsr17Factory(); |
| 139 | $factory = new PsrHttpFactory($psrFactory, $psrFactory, $psrFactory, $psrFactory); |
| 140 | return $factory->createResponse($sfResponse); |
| 141 | } |
| 142 | |
| 143 | public function loginAs(DecryptedAuthenticatedUser $user): void |
| 144 | { |
| 145 | $textEncrypter = new TextEncrypter('test'); |
| 146 | $this->authenticationCookie = $textEncrypter->encrypt($user->toNative()); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Forget that you are logged in. |
| 151 | */ |
| 152 | public function logout(): void |
| 153 | { |
| 154 | $this->authenticationCookie = null; |
| 155 | } |
| 156 | |
| 157 | public function getLoggedInAs(): ?DecryptedAuthenticatedUser |
| 158 | { |
| 159 | if (empty($this->authenticationCookie)) { |
| 160 | return null; |
| 161 | } |
| 162 | $textEncrypter = new TextEncrypter('test'); |
| 163 | return DecryptedAuthenticatedUser::fromNative( |
| 164 | $textEncrypter->decrypt($this->authenticationCookie), |
| 165 | ); |
| 166 | } |
| 167 | } |