|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Zalas\BundleTest\PHPUnit; |
|
5
|
|
|
|
|
6
|
|
|
use Symfony\Component\DependencyInjection\ResettableContainerInterface; |
|
7
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
|
8
|
|
|
use Zalas\BundleTest\HttpKernel\KernelBuilder; |
|
9
|
|
|
use Zalas\BundleTest\HttpKernel\TestKernel as TheTestKernel; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Reproduces the behaviour of `Symfony\Bundle\FrameworkBundle\Test\KernelTestCase` for the `TestKernel`. |
|
13
|
|
|
* |
|
14
|
|
|
* Compared to its inspiration this trait offers a more dynamic way of booting the Symfony kernel. |
|
15
|
|
|
* Cache will be generated for each variation of the kernel configuration. |
|
16
|
|
|
*/ |
|
17
|
|
|
trait TestKernel |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var KernelInterface|null |
|
21
|
|
|
*/ |
|
22
|
|
|
protected static $kernel; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @before |
|
26
|
|
|
*/ |
|
27
|
|
|
protected function resetKernel() |
|
28
|
|
|
{ |
|
29
|
|
|
static::$kernel = null; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
protected static function getKernelClass(): string |
|
33
|
|
|
{ |
|
34
|
|
|
return $_ENV['KERNEL_CLASS'] ?? $_SERVER['KERNEL_CLASS'] ?? TheTestKernel::class; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected static function bootKernel(array $options = []): KernelInterface |
|
38
|
|
|
{ |
|
39
|
|
|
self::ensureKernelShutdown(); |
|
40
|
|
|
|
|
41
|
|
|
return static::$kernel = static::createKernelBuilder($options)->bootKernel(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Creates a test kernel. |
|
46
|
|
|
* |
|
47
|
|
|
* Compared to the original `KernelTestCase` this method offers an additional `kernel_class` option due to a more |
|
48
|
|
|
* dynamic nature of this trait. |
|
49
|
|
|
* |
|
50
|
|
|
* Options: |
|
51
|
|
|
* |
|
52
|
|
|
* * environment |
|
53
|
|
|
* * debug |
|
54
|
|
|
* * kernel_class |
|
55
|
|
|
*/ |
|
56
|
|
|
protected static function createKernel(array $options = []): KernelInterface |
|
57
|
|
|
{ |
|
58
|
|
|
return self::createKernelBuilder($options)->createKernel(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected static function createKernelBuilder(array $options): KernelBuilder |
|
62
|
|
|
{ |
|
63
|
|
|
$builder = new KernelBuilder(); |
|
64
|
|
|
$builder->withKernelClass($options['kernel_class'] ?? static::getKernelClass()); |
|
65
|
|
|
$builder->withEnvironment($options['environment'] ?? $_ENV['APP_ENV'] ?? $_SERVER['APP_ENV'] ?? 'test'); |
|
66
|
|
|
$builder->withDebug((bool) ($options['debug'] ?? $_ENV['APP_DEBUG'] ?? $_SERVER['APP_DEBUG'] ?? true)); |
|
67
|
|
|
|
|
68
|
|
|
return $builder; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @after |
|
73
|
|
|
*/ |
|
74
|
|
|
protected static function ensureKernelShutdown(): void |
|
75
|
|
|
{ |
|
76
|
|
|
if (null !== static::$kernel) { |
|
77
|
|
|
$container = static::$kernel->getContainer(); |
|
78
|
|
|
static::$kernel->shutdown(); |
|
79
|
|
|
if ($container instanceof ResettableContainerInterface) { |
|
80
|
|
|
$container->reset(); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|