ConfigurableKernel   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 49
dl 0
loc 148
rs 10
c 0
b 0
f 0
wmc 20

14 Methods

Rating   Name   Duplication   Size   Complexity  
A ensureKernelNotBooted() 0 4 2
A getTestNamespace() 0 3 1
A givenDebugIsEnabled() 0 7 1
A givenPublicServiceId() 0 7 1
A givenEnabledBundle() 0 7 1
A givenEnvironment() 0 7 1
A givenTempDir() 0 7 1
A initializeKernelBuilder() 0 11 3
A givenDebugIsDisabled() 0 7 1
A createKernelBuilder() 0 15 4
A givenEnabledBundles() 0 7 1
A givenKernel() 0 7 1
A givenBundleConfiguration() 0 7 1
A givenPublicServiceIds() 0 7 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Zalas\BundleTest\PHPUnit;
5
6
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
7
use Zalas\BundleTest\HttpKernel\KernelBuilder;
8
9
/**
10
 * Builds on top of `TestKernel` trait to provide more convenient ways of setting up the kernel with given* methods.
11
 */
12
trait ConfigurableKernel
13
{
14
    use TestKernel;
15
16
    /**
17
     * @var KernelBuilder
18
     */
19
    protected static $kernelBuilder;
20
21
    /**
22
     * @before
23
     */
24
    protected function initializeKernelBuilder(): void
25
    {
26
        static::$kernelBuilder = new KernelBuilder($this->getTestNamespace());
27
        static::$kernelBuilder->withKernelClass(static::getKernelClass());
28
29
        if (null !== $environment = $_ENV['APP_ENV'] ?? $_SERVER['APP_ENV'] ?? null) {
30
            static::$kernelBuilder->withEnvironment((string) $environment);
31
        }
32
33
        if (null !== $debug = $_ENV['APP_DEBUG'] ?? $_SERVER['APP_DEBUG'] ?? null) {
34
            static::$kernelBuilder->withDebug((bool) $debug);
35
        }
36
    }
37
38
    protected static function createKernelBuilder(array $options): KernelBuilder
39
    {
40
        if (null !== $environment = $options['environment'] ?? null) {
41
            static::$kernelBuilder->withEnvironment((string) $environment);
42
        }
43
44
        if (null !== $isDebug = $options['debug'] ?? null) {
45
            static::$kernelBuilder->withDebug((bool) $isDebug);
46
        }
47
48
        if (null !== $kernelClass = $options['kernel_class'] ?? null) {
49
            static::$kernelBuilder->withKernelClass((string) $kernelClass);
50
        }
51
52
        return static::$kernelBuilder;
53
    }
54
55
    /**
56
     * Scopes the current test case in the working directory (temp directory).
57
     */
58
    protected function getTestNamespace(): string
59
    {
60
        return \str_replace('\\', '', __CLASS__);
61
    }
62
63
    protected function givenEnvironment(string $environment): self
64
    {
65
        $this->ensureKernelNotBooted();
66
67
        static::$kernelBuilder->withEnvironment($environment);
68
69
        return $this;
70
    }
71
72
    protected function givenDebugIsEnabled(): self
73
    {
74
        $this->ensureKernelNotBooted();
75
76
        static::$kernelBuilder->withDebug(true);
77
78
        return $this;
79
    }
80
81
    protected function givenDebugIsDisabled(): self
82
    {
83
        $this->ensureKernelNotBooted();
84
85
        static::$kernelBuilder->withDebug(false);
86
87
        return $this;
88
    }
89
90
    protected function givenKernel(string $kernelClass): self
91
    {
92
        $this->ensureKernelNotBooted();
93
94
        static::$kernelBuilder->withKernelClass($kernelClass);
95
96
        return $this;
97
    }
98
99
    protected function givenBundleConfiguration($extensionName, array $configuration): self
100
    {
101
        $this->ensureKernelNotBooted();
102
103
        static::$kernelBuilder->withBundleConfiguration($extensionName, $configuration);
104
105
        return $this;
106
    }
107
108
    protected function givenPublicServiceId(string $serviceId): self
109
    {
110
        $this->ensureKernelNotBooted();
111
112
        static::$kernelBuilder->withPublicService($serviceId);
113
114
        return $this;
115
    }
116
117
    protected function givenPublicServiceIds(array $serviceIds): self
118
    {
119
        $this->ensureKernelNotBooted();
120
121
        static::$kernelBuilder->withPublicServices($serviceIds);
122
123
        return $this;
124
    }
125
126
    /**
127
     * @param BundleInterface[] $bundles
128
     */
129
    protected function givenEnabledBundles(array $bundles): self
130
    {
131
        $this->ensureKernelNotBooted();
132
133
        static::$kernelBuilder->withBundles($bundles);
134
135
        return $this;
136
    }
137
138
    protected function givenEnabledBundle(BundleInterface $bundle): self
139
    {
140
        $this->ensureKernelNotBooted();
141
142
        static::$kernelBuilder->withBundle($bundle);
143
144
        return $this;
145
    }
146
147
    protected function givenTempDir(string $tempDir): self
148
    {
149
        $this->ensureKernelNotBooted();
150
151
        static::$kernelBuilder->withTempDir($tempDir);
152
153
        return $this;
154
    }
155
156
    private function ensureKernelNotBooted(): void
157
    {
158
        if (null !== static::$kernel) {
159
            throw new \LogicException('Configuration cannot be changed once kernel is booted.');
160
        }
161
    }
162
}
163