Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
98.23% |
111 / 113 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
SymfonyTestingKernel | |
98.23% |
111 / 113 |
|
83.33% |
5 / 6 |
13 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
getCacheDir | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 | |||
registerBundles | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
getDefaultTwigTemplate | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__wakeup | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
registerContainerConfiguration | |
100.00% |
82 / 82 |
|
100.00% |
1 / 1 |
4 |
1 | <?php |
2 | namespace Apie\IntegrationTests\Applications\Symfony; |
3 | |
4 | use Apie\ApieBundle\ApieBundle; |
5 | use Apie\ApieBundle\Security\ApieUserAuthenticator; |
6 | use Apie\ApieBundle\Security\ApieUserProvider; |
7 | use Apie\Core\Other\FileWriterInterface; |
8 | use Apie\Core\Other\MockFileWriter; |
9 | use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; |
10 | use Symfony\Bundle\FrameworkBundle\FrameworkBundle; |
11 | use Symfony\Bundle\MonologBundle\MonologBundle; |
12 | use Symfony\Bundle\SecurityBundle\SecurityBundle; |
13 | use Symfony\Bundle\TwigBundle\TwigBundle; |
14 | use Symfony\Component\Config\Loader\LoaderInterface; |
15 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
16 | use Symfony\Component\DependencyInjection\Definition; |
17 | use Symfony\Component\HttpKernel\Kernel; |
18 | use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; |
19 | |
20 | class SymfonyTestingKernel extends Kernel |
21 | { |
22 | /** |
23 | * @param array<string, mixed> $apieConfig |
24 | */ |
25 | public function __construct( |
26 | private array $apieConfig = [], |
27 | private readonly bool $includeTwigBundle = false, |
28 | private readonly bool $includeSecurityBundle = true |
29 | ) { |
30 | $this->apieConfig['enable_security'] ??= $this->includeSecurityBundle; |
31 | if (!$this->includeTwigBundle) { |
32 | $this->apieConfig['cms'] ??= []; |
33 | $this->apieConfig['cms']['error_template'] = __DIR__ . '/../../../fixtures/symfony/templates/error.html'; |
34 | } |
35 | parent::__construct('test', true); |
36 | } |
37 | |
38 | public function getCacheDir(): string |
39 | { |
40 | return sys_get_temp_dir() |
41 | . '/cache/' |
42 | . md5( |
43 | json_encode( |
44 | [ |
45 | $this->apieConfig, |
46 | $this->includeTwigBundle, |
47 | $this->includeSecurityBundle, |
48 | getenv('PHPUNIT_LOG_INTEGRATION_OUTPUT') ? 1 : 0 |
49 | ] |
50 | ) |
51 | ); |
52 | } |
53 | |
54 | public function registerBundles(): iterable |
55 | { |
56 | $res = [ |
57 | new FrameworkBundle(), // this is needed to have a functional http_kernel service. |
58 | new ApieBundle(), |
59 | new MonologBundle(), // some errors are discarded for response, but still logged. |
60 | new DoctrineBundle(), // maybe make this optional as the bundle also works without |
61 | ]; |
62 | if ($this->includeTwigBundle) { |
63 | $res[] = new TwigBundle(); |
64 | } |
65 | if ($this->includeSecurityBundle) { |
66 | $res[] = new SecurityBundle(); |
67 | } |
68 | return $res; |
69 | } |
70 | |
71 | private function getDefaultTwigTemplate(): string |
72 | { |
73 | return __DIR__ . '/../../../fixtures/symfony/templates'; |
74 | } |
75 | |
76 | public function __wakeup(): void |
77 | { |
78 | $this->__construct($this->apieConfig, $this->includeTwigBundle, $this->includeSecurityBundle); |
79 | parent::__wakeup(); |
80 | } |
81 | |
82 | public function registerContainerConfiguration(LoaderInterface $loader): void |
83 | { |
84 | $loader->load(function (ContainerBuilder $container) { |
85 | $container->addDefinitions([ |
86 | InMemoryPersistentSessionStorageFactory::class => new Definition(InMemoryPersistentSessionStorageFactory::class), |
87 | FileWriterInterface::class => (new Definition(MockFileWriter::class))->setPublic(true), |
88 | ]); |
89 | $container->loadFromExtension('apie', $this->apieConfig); |
90 | $container->loadFromExtension('doctrine', ['orm' => ['auto_mapping' => true], 'dbal' => []]); |
91 | $container->loadFromExtension('monolog', [ |
92 | 'handlers' => [ |
93 | 'file_log' => [ |
94 | 'type' => 'stream', |
95 | 'path' => $this->getCacheDir() . 'log', |
96 | 'level' => getenv('PHPUNIT_LOG_INTEGRATION_OUTPUT') ? 'debug' : 'error', |
97 | ] |
98 | ] |
99 | ]); |
100 | $container->loadFromExtension( |
101 | 'framework', |
102 | [ |
103 | 'http_method_override' => false, |
104 | 'secret' => '123456', |
105 | 'session' => [ |
106 | 'enabled' => true, |
107 | 'storage_factory_id' => InMemoryPersistentSessionStorageFactory::class, |
108 | 'handler_id' => 'session.handler.native_file', |
109 | 'use_cookies' => true, |
110 | 'cookie_secure' => 'auto', |
111 | 'cookie_samesite' => 'lax', |
112 | ], |
113 | 'php_errors' => [ |
114 | 'log' => false, |
115 | ], |
116 | 'router' => [ |
117 | 'resource' => '.', |
118 | 'type' => 'apie' |
119 | ], |
120 | 'uid' => [ |
121 | 'default_uuid_version' => 7, |
122 | 'time_based_uuid_version' => 7, |
123 | ], |
124 | 'csrf_protection' => false, |
125 | 'handle_all_throwables' => true, |
126 | ] |
127 | ); |
128 | if ($this->includeTwigBundle) { |
129 | $container->loadFromExtension( |
130 | 'twig', |
131 | [ |
132 | 'default_path' => $this->getDefaultTwigTemplate(), |
133 | ] |
134 | ); |
135 | } |
136 | if ($this->includeSecurityBundle) { |
137 | $container->loadFromExtension( |
138 | 'security', |
139 | [ |
140 | 'password_hashers' => [ |
141 | PasswordAuthenticatedUserInterface::class => [ |
142 | 'algorithm' => 'auto', |
143 | 'cost' => 4, |
144 | 'time_cost' => 3, |
145 | 'memory_cost' => 10, |
146 | ], |
147 | ], |
148 | 'providers' => [ |
149 | 'apie_user' => [ |
150 | 'id' => ApieUserProvider::class |
151 | ] |
152 | ], |
153 | 'firewalls' => [ |
154 | 'dev' => [ |
155 | 'pattern' => '^/(_(profiler|wdt)|css|images|js)/', |
156 | 'security' => false, |
157 | ], |
158 | 'main' => [ |
159 | 'lazy' => true, |
160 | 'provider' => 'apie_user', |
161 | 'custom_authenticators' => [ApieUserAuthenticator::class] |
162 | ], |
163 | ] |
164 | ] |
165 | ); |
166 | } |
167 | }); |
168 | } |
169 | } |