1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace TomCizek\SymfonyInteropContainer\Tests\Configurators; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Psr\Container\ContainerExceptionInterface; |
7
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
8
|
|
|
use TomCizek\SymfonyInteropContainer\SymfonyInteropContainer; |
9
|
|
|
use TomCizek\SymfonyInteropContainer\SymfonyInteropContainerBundle; |
10
|
|
|
use TomCizek\SymfonyInteropContainer\Tests\TestBundle\TestBundle; |
11
|
|
|
use TomCizek\SymfonyInteropContainer\Tests\TestBundle\TestService; |
12
|
|
|
|
13
|
|
|
class SymfonyInteropContainerTest extends SymfonyInteropContainerTestCase |
14
|
|
|
{ |
15
|
|
|
const FIXTURE_CONGIGS_DIR = __DIR__ . '/fixtures/'; |
16
|
|
|
|
17
|
|
|
/** @var SymfonyInteropContainer */ |
18
|
|
|
private $interopContainer; |
19
|
|
|
|
20
|
|
|
public function setUp() |
21
|
|
|
{ |
22
|
|
|
parent::setUp(); |
23
|
|
|
|
24
|
|
|
$this->givenInteropContainerFromBootedKernelWith( |
25
|
|
|
[ |
26
|
|
|
SymfonyInteropContainerBundle::class, |
27
|
|
|
TestBundle::class, |
28
|
|
|
], |
29
|
|
|
[ |
30
|
|
|
ConfigFile::record(self::FIXTURE_CONGIGS_DIR . 'TestExtensionConfig.yml', 'yml'), |
31
|
|
|
] |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testHas_WithBadType_ShouldThrowContainerException() |
36
|
|
|
{ |
37
|
|
|
$this->willThrowException(ContainerExceptionInterface::class); |
38
|
|
|
|
39
|
|
|
$this->whenAskInteropContainerWhetherHasKey(new DateTime()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testHas_NotExistingService_ShouldReturnFalse() |
43
|
|
|
{ |
44
|
|
|
$hasConfig = $this->whenAskInteropContainerWhetherHasKey('not_existing_service'); |
45
|
|
|
|
46
|
|
|
$this->thenResultIsFalse($hasConfig); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testHas_ConfigKey_ShouldReturnTrue() |
50
|
|
|
{ |
51
|
|
|
$hasConfig = $this->whenAskInteropContainerWhetherHasKey('config'); |
52
|
|
|
|
53
|
|
|
$this->thenResultIsTrue($hasConfig); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testHas_ExistingService_ShouldReturnTrue() |
57
|
|
|
{ |
58
|
|
|
$hasConfig = $this->whenAskInteropContainerWhetherHasKey('test_service'); |
59
|
|
|
|
60
|
|
|
$this->thenResultIsTrue($hasConfig); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testGet_WithBadType_ShouldThrowContainerException() |
64
|
|
|
{ |
65
|
|
|
$this->willThrowException(ContainerExceptionInterface::class); |
66
|
|
|
|
67
|
|
|
$this->whenGetFromInteropContainerByKey(new DateTime()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testGet_NotExistingService_ShouldThrowNotFoundException() |
71
|
|
|
{ |
72
|
|
|
$this->willThrowException(NotFoundExceptionInterface::class); |
73
|
|
|
|
74
|
|
|
$this->whenGetFromInteropContainerByKey('not_existing_service'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testGet_ExistingTestServiceFromConfig_ShouldBeInstanceOfTestService() |
78
|
|
|
{ |
79
|
|
|
$testService = $this->whenGetFromInteropContainerByKey('test_service'); |
80
|
|
|
|
81
|
|
|
$this->thenIsInstanceOf(TestService::class, $testService); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testGet_ConfigKey_ShouldContainTestExtensionConfig() |
85
|
|
|
{ |
86
|
|
|
$config = $this->whenGetFromInteropContainerByKey('config'); |
87
|
|
|
|
88
|
|
|
$this->thenIsexpecteedConfig($config); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string[] $bundles |
93
|
|
|
* @param ConfigFile[] $configFiles |
94
|
|
|
* |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
|
private function givenInteropContainerFromBootedKernelWith(array $bundles = [], array $configFiles = []): void |
98
|
|
|
{ |
99
|
|
|
$container = $this->getContainerFromBootedKernelWith($bundles, $configFiles); |
100
|
|
|
$this->interopContainer = $container->get(SymfonyInteropContainer::class); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function willThrowException($class): void |
104
|
|
|
{ |
105
|
|
|
$this->expectException($class); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private function whenAskInteropContainerWhetherHasKey($key) |
109
|
|
|
{ |
110
|
|
|
return $this->interopContainer->has($key); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function thenResultIsTrue($has): void |
114
|
|
|
{ |
115
|
|
|
self::assertTrue($has); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private function thenResultIsFalse($has): void |
119
|
|
|
{ |
120
|
|
|
self::assertFalse($has); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
private function whenGetFromInteropContainerByKey($key) |
124
|
|
|
{ |
125
|
|
|
return $this->interopContainer->get($key); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private function thenIsInstanceOf($expectedClass, $instance): void |
129
|
|
|
{ |
130
|
|
|
self::assertInstanceOf($expectedClass, $instance); |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
private function thenIsexpecteedConfig(array $config): void |
135
|
|
|
{ |
136
|
|
|
$expectedConfig = [ |
137
|
|
|
'test' => [ |
138
|
|
|
'foo' => 'bar', |
139
|
|
|
], |
140
|
|
|
]; |
141
|
|
|
self::assertEquals($expectedConfig, $config); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|