Completed
Push — 1.2-symfony-4.1 ( 9574a4...b0b494 )
by Kamil
17:41
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
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 22 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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