Completed
Push — master ( 9a4347...97ef27 )
by Kamil
26:10 queued 10s
created

TestAppKernel::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
require_once __DIR__.'/AppKernel.php';
15
16
use ProxyManager\Proxy\VirtualProxyInterface;
17
use PSS\SymfonyMockerContainer\DependencyInjection\MockerContainer;
18
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
19
use Symfony\Component\DependencyInjection\ContainerBuilder;
20
use Symfony\Component\DependencyInjection\ContainerInterface;
21
22
class TestAppKernel extends AppKernel implements CompilerPassInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function shutdown(): void
28
    {
29
        if (false === $this->booted) {
30
            return;
31
        }
32
33
        if (!in_array($this->getEnvironment(), ['test', 'test_cached'], true)) {
34
            parent::shutdown();
35
36
            return;
37
        }
38
39
        $container = $this->getContainer();
40
        parent::shutdown();
41
        $this->cleanupContainer($container);
42
    }
43
44
    /**
45
     * Hotfix for https://github.com/symfony/symfony/issues/27494
46
     *
47
     * @internal
48
     */
49
    public function process(ContainerBuilder $container): void
50
    {
51
        $clientDefinition = $container->findDefinition('test.client');
52
53
        if (count($clientDefinition->getArguments()) >= 5) {
54
            $clientDefinition->replaceArgument(4, null);
55
        }
56
    }
57
58
    /**
59
     * Remove all container references from all loaded services
60
     *
61
     * @param ContainerInterface $container
62
     */
63
    protected function cleanupContainer(ContainerInterface $container): void
64
    {
65
        $containerReflection = new \ReflectionObject($container);
66
        $containerServicesPropertyReflection = $containerReflection->getProperty('services');
67
        $containerServicesPropertyReflection->setAccessible(true);
68
69
        $services = $containerServicesPropertyReflection->getValue($container) ?: [];
70
        foreach ($services as $serviceId => $service) {
71
            if (null === $service) {
72
                continue;
73
            }
74
75
            if (in_array($serviceId, $this->getServicesToIgnoreDuringContainerCleanup(), true)) {
76
                continue;
77
            }
78
79
            $serviceReflection = new \ReflectionObject($service);
80
81
            if ($serviceReflection->implementsInterface(VirtualProxyInterface::class)) {
82
                continue;
83
            }
84
85
            $servicePropertiesReflections = $serviceReflection->getProperties();
86
            $servicePropertiesDefaultValues = $serviceReflection->getDefaultProperties();
87
            foreach ($servicePropertiesReflections as $servicePropertyReflection) {
88
                $defaultPropertyValue = null;
89
                if (isset($servicePropertiesDefaultValues[$servicePropertyReflection->getName()])) {
90
                    $defaultPropertyValue = $servicePropertiesDefaultValues[$servicePropertyReflection->getName()];
91
                }
92
93
                $servicePropertyReflection->setAccessible(true);
94
                $servicePropertyReflection->setValue($service, $defaultPropertyValue);
95
            }
96
        }
97
98
        $containerServicesPropertyReflection->setValue($container, null);
99
    }
100
101
    protected function getContainerBaseClass(): string
102
    {
103
        return MockerContainer::class;
104
    }
105
106
    protected function getServicesToIgnoreDuringContainerCleanup(): array
107
    {
108
        return [
109
            'kernel',
110
            'http_kernel',
111
            'liip_imagine.mime_type_guesser',
112
            'liip_imagine.extension_guesser',
113
        ];
114
    }
115
}
116