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