|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2016 Inneair |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
* |
|
18
|
|
|
* @license http://www.apache.org/licenses/LICENSE-2.0.html Apache-2.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace Inneair\TransactionBundle\Test\DependencyInjection; |
|
22
|
|
|
|
|
23
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
24
|
|
|
use Exception; |
|
25
|
|
|
use InvalidArgumentException; |
|
26
|
|
|
use PHPUnit_Framework_MockObject_MockObject; |
|
27
|
|
|
use Psr\Log\LoggerInterface; |
|
28
|
|
|
use Inneair\TransactionBundle\Annotation\Transactional; |
|
29
|
|
|
use Inneair\TransactionBundle\DependencyInjection\Configuration; |
|
30
|
|
|
use Inneair\TransactionBundle\DependencyInjection\InneairTransactionExtension; |
|
31
|
|
|
use Inneair\TransactionBundle\Test\AbstractTest; |
|
32
|
|
|
use Inneair\TransactionBundle\Test\DependencyInjection\Fixture\InvalidConfiguration; |
|
33
|
|
|
use Symfony\Bridge\Doctrine\RegistryInterface; |
|
34
|
|
|
use Symfony\Component\Config\FileLocator; |
|
35
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
36
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
37
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
|
38
|
|
|
|
|
39
|
|
|
class InneairTransactionExtensionTest extends AbstractTest |
|
40
|
|
|
{ |
|
41
|
|
|
/** |
|
42
|
|
|
* The extension. |
|
43
|
|
|
* @var InneairTransactionExtension |
|
44
|
|
|
*/ |
|
45
|
|
|
private $extension; |
|
46
|
|
|
/** |
|
47
|
|
|
* Container builder. |
|
48
|
|
|
* @var ContainerBuilder |
|
49
|
|
|
*/ |
|
50
|
|
|
private $container; |
|
51
|
|
|
/** |
|
52
|
|
|
* Mocked annotation reader. |
|
53
|
|
|
* @var PHPUnit_Framework_MockObject_MockObject |
|
54
|
|
|
*/ |
|
55
|
|
|
private $annotationReader; |
|
56
|
|
|
/** |
|
57
|
|
|
* Mocked logger. |
|
58
|
|
|
* @var PHPUnit_Framework_MockObject_MockObject |
|
59
|
|
|
*/ |
|
60
|
|
|
private $logger; |
|
61
|
|
|
/** |
|
62
|
|
|
* Mocked entity manager registry. |
|
63
|
|
|
* @var PHPUnit_Framework_MockObject_MockObject |
|
64
|
|
|
*/ |
|
65
|
|
|
private $entityManagerRegistry; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritDoc} |
|
69
|
|
|
*/ |
|
70
|
|
|
public function setUp() |
|
71
|
|
|
{ |
|
72
|
|
|
$this->annotationReader = $this->getMock(Reader::class); |
|
73
|
|
|
$this->logger = $this->getMock(LoggerInterface::class); |
|
74
|
|
|
$this->entityManagerRegistry = $this->getMock(RegistryInterface::class); |
|
75
|
|
|
$this->extension = new InneairTransactionExtension(); |
|
76
|
|
|
|
|
77
|
|
|
$this->container = new ContainerBuilder(); |
|
78
|
|
|
$this->container->set('annotation_reader', $this->annotationReader); |
|
79
|
|
|
$this->container->set('logger', $this->logger); |
|
80
|
|
|
$this->container->set('doctrine', $this->entityManagerRegistry); |
|
81
|
|
|
$this->container->registerExtension($this->extension); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function testDefaultConfiguration() |
|
85
|
|
|
{ |
|
86
|
|
|
$this->extension->load(array(), $this->container); |
|
87
|
|
|
|
|
88
|
|
|
$this->assertTrue( |
|
89
|
|
|
$this->container->hasParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::STRICT_MODE) |
|
90
|
|
|
); |
|
91
|
|
|
$this->assertFalse( |
|
92
|
|
|
$this->container->getParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::STRICT_MODE) |
|
93
|
|
|
); |
|
94
|
|
|
$this->assertTrue( |
|
95
|
|
|
$this->container->hasParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::DEFAULT_POLICY) |
|
96
|
|
|
); |
|
97
|
|
|
$this->assertSame( |
|
98
|
|
|
Transactional::REQUIRED, |
|
99
|
|
|
$this->container->getParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::DEFAULT_POLICY) |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function testConfigurationWithStrictMode() |
|
104
|
|
|
{ |
|
105
|
|
|
$this->extension->load(array(array(Configuration::STRICT_MODE => true)), $this->container); |
|
106
|
|
|
|
|
107
|
|
|
$this->assertTrue( |
|
108
|
|
|
$this->container->hasParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::STRICT_MODE) |
|
109
|
|
|
); |
|
110
|
|
|
$this->assertTrue( |
|
111
|
|
|
$this->container->getParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::STRICT_MODE) |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function testConfigurationWithoutStrictMode() |
|
116
|
|
|
{ |
|
117
|
|
|
$this->extension->load(array(array(Configuration::STRICT_MODE => false)), $this->container); |
|
118
|
|
|
|
|
119
|
|
|
$this->assertTrue( |
|
120
|
|
|
$this->container->hasParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::STRICT_MODE) |
|
121
|
|
|
); |
|
122
|
|
|
$this->assertFalse( |
|
123
|
|
|
$this->container->getParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::STRICT_MODE) |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function testConfigurationWithRequiredPolicy() |
|
128
|
|
|
{ |
|
129
|
|
|
$this->extension->load( |
|
130
|
|
|
array(array(Configuration::DEFAULT_POLICY => Configuration::POLICY_REQUIRED)), |
|
131
|
|
|
$this->container |
|
132
|
|
|
); |
|
133
|
|
|
|
|
134
|
|
|
$this->assertTrue( |
|
135
|
|
|
$this->container->hasParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::DEFAULT_POLICY) |
|
136
|
|
|
); |
|
137
|
|
|
$this->assertSame( |
|
138
|
|
|
Transactional::REQUIRED, |
|
139
|
|
|
$this->container->getParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::DEFAULT_POLICY) |
|
140
|
|
|
); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function testConfigurationWithNotRequiredPolicy() |
|
144
|
|
|
{ |
|
145
|
|
|
$this->extension->load( |
|
146
|
|
|
array(array(Configuration::DEFAULT_POLICY => Configuration::POLICY_NOT_REQUIRED)), |
|
147
|
|
|
$this->container |
|
148
|
|
|
); |
|
149
|
|
|
|
|
150
|
|
|
$this->assertTrue( |
|
151
|
|
|
$this->container->hasParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::DEFAULT_POLICY) |
|
152
|
|
|
); |
|
153
|
|
|
$this->assertSame( |
|
154
|
|
|
Transactional::NOT_REQUIRED, |
|
155
|
|
|
$this->container->getParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::DEFAULT_POLICY) |
|
156
|
|
|
); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
public function testConfigurationWithNestedPolicy() |
|
160
|
|
|
{ |
|
161
|
|
|
$this->extension->load( |
|
162
|
|
|
array(array(Configuration::DEFAULT_POLICY => Configuration::POLICY_NESTED)), |
|
163
|
|
|
$this->container); |
|
164
|
|
|
|
|
165
|
|
|
$this->assertTrue( |
|
166
|
|
|
$this->container->hasParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::DEFAULT_POLICY) |
|
167
|
|
|
); |
|
168
|
|
|
$this->assertSame( |
|
169
|
|
|
Transactional::NESTED, |
|
170
|
|
|
$this->container->getParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::DEFAULT_POLICY) |
|
171
|
|
|
); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
public function testConfigurationWithUnsupportedPolicy() |
|
175
|
|
|
{ |
|
176
|
|
|
$extension = $this->getMock(InneairTransactionExtension::class, array('getConfiguration')); |
|
177
|
|
|
$extension->expects(static::once())->method('getConfiguration')->willReturn(new InvalidConfiguration()); |
|
178
|
|
|
$hasException = false; |
|
179
|
|
|
try { |
|
180
|
|
|
$extension->load(array(array(Configuration::DEFAULT_POLICY => null)), $this->container); |
|
181
|
|
|
} catch (InvalidArgumentException $e) { |
|
182
|
|
|
$hasException = true; |
|
183
|
|
|
} |
|
184
|
|
|
$this->assertTrue($hasException); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
public function testYamlFullConfiguration() |
|
188
|
|
|
{ |
|
189
|
|
|
$this->loadConfigurationFile('config-full.yml'); |
|
190
|
|
|
|
|
191
|
|
|
$this->assertTrue( |
|
192
|
|
|
$this->container->hasParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::STRICT_MODE) |
|
193
|
|
|
); |
|
194
|
|
|
$this->assertTrue( |
|
195
|
|
|
$this->container->getParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::STRICT_MODE) |
|
196
|
|
|
); |
|
197
|
|
|
$this->assertTrue( |
|
198
|
|
|
$this->container->hasParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::DEFAULT_POLICY) |
|
199
|
|
|
); |
|
200
|
|
|
$this->assertSame( |
|
201
|
|
|
Transactional::NESTED, |
|
202
|
|
|
$this->container->getParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::DEFAULT_POLICY) |
|
203
|
|
|
); |
|
204
|
|
|
$this->assertTrue( |
|
205
|
|
|
$this->container->hasParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::NO_ROLLBACK_EXCEPTIONS) |
|
206
|
|
|
); |
|
207
|
|
|
$noRollbackExceptions = $this->container->getParameter( |
|
208
|
|
|
Configuration::ROOT_NODE_NAME . '.' . Configuration::NO_ROLLBACK_EXCEPTIONS |
|
209
|
|
|
); |
|
210
|
|
|
$this->assertCount(1, $noRollbackExceptions); |
|
211
|
|
|
$this->assertSame('Exception', current($noRollbackExceptions)); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
public function testYamlConfigurationWithUnknownExceptionClass() |
|
215
|
|
|
{ |
|
216
|
|
|
$hasException = false; |
|
217
|
|
|
try { |
|
218
|
|
|
$this->loadConfigurationFile('config-with-unknown-exception-class.yml'); |
|
219
|
|
|
} catch (InvalidArgumentException $e) { |
|
220
|
|
|
$hasException = true; |
|
221
|
|
|
} |
|
222
|
|
|
$this->assertTrue($hasException); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
public function testYamlConfigurationWithInvalidExceptionClass() |
|
226
|
|
|
{ |
|
227
|
|
|
$hasException = false; |
|
228
|
|
|
try { |
|
229
|
|
|
$this->loadConfigurationFile('config-with-invalid-exception-class.yml'); |
|
230
|
|
|
} catch (InvalidArgumentException $e) { |
|
231
|
|
|
$hasException = true; |
|
232
|
|
|
} |
|
233
|
|
|
$this->assertTrue($hasException); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
public function testXmlFullConfiguration() |
|
237
|
|
|
{ |
|
238
|
|
|
$this->loadConfigurationFile('config-full.xml'); |
|
239
|
|
|
|
|
240
|
|
|
$this->assertTrue( |
|
241
|
|
|
$this->container->hasParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::STRICT_MODE) |
|
242
|
|
|
); |
|
243
|
|
|
$this->assertTrue( |
|
244
|
|
|
$this->container->getParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::STRICT_MODE) |
|
245
|
|
|
); |
|
246
|
|
|
$this->assertTrue( |
|
247
|
|
|
$this->container->hasParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::DEFAULT_POLICY) |
|
248
|
|
|
); |
|
249
|
|
|
$this->assertSame( |
|
250
|
|
|
Transactional::NESTED, |
|
251
|
|
|
$this->container->getParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::DEFAULT_POLICY) |
|
252
|
|
|
); |
|
253
|
|
|
$this->assertTrue( |
|
254
|
|
|
$this->container->hasParameter(Configuration::ROOT_NODE_NAME . '.' . Configuration::NO_ROLLBACK_EXCEPTIONS) |
|
255
|
|
|
); |
|
256
|
|
|
$noRollbackExceptions = $this->container->getParameter( |
|
257
|
|
|
Configuration::ROOT_NODE_NAME . '.' . Configuration::NO_ROLLBACK_EXCEPTIONS |
|
258
|
|
|
); |
|
259
|
|
|
$this->assertCount(1, $noRollbackExceptions); |
|
260
|
|
|
$this->assertSame('Exception', current($noRollbackExceptions)); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
public function testXmlConfigurationWithUnknownExceptionClass() |
|
264
|
|
|
{ |
|
265
|
|
|
$hasException = false; |
|
266
|
|
|
try { |
|
267
|
|
|
$this->loadConfigurationFile('config-with-unknown-exception-class.xml'); |
|
268
|
|
|
} catch (InvalidArgumentException $e) { |
|
269
|
|
|
$hasException = true; |
|
270
|
|
|
} |
|
271
|
|
|
$this->assertTrue($hasException); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
public function testXmlConfigurationWithInvalidExceptionClass() |
|
275
|
|
|
{ |
|
276
|
|
|
$hasException = false; |
|
277
|
|
|
try { |
|
278
|
|
|
$this->loadConfigurationFile('config-with-invalid-exception-class.xml'); |
|
279
|
|
|
} catch (InvalidArgumentException $e) { |
|
280
|
|
|
$hasException = true; |
|
281
|
|
|
} |
|
282
|
|
|
$this->assertTrue($hasException); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* Loads a configuration file. |
|
287
|
|
|
* |
|
288
|
|
|
* @param string $configFileName Name of a configuration file. |
|
289
|
|
|
* @throws InvalidArgumentException If the configuration contains a class name that does not exist, or is not an |
|
290
|
|
|
* exception. |
|
291
|
|
|
* @throws Exception If the configuration file is not supported. |
|
292
|
|
|
*/ |
|
293
|
|
|
private function loadConfigurationFile($configFileName) |
|
294
|
|
|
{ |
|
295
|
|
|
$xmlExtension = '.xml'; |
|
296
|
|
|
$ymlExtension = '.yml'; |
|
297
|
|
|
$fileLocator = new FileLocator(__DIR__ . '/Fixture'); |
|
298
|
|
|
if (mb_strrpos($configFileName, $ymlExtension) === (mb_strlen($configFileName) - mb_strlen($ymlExtension))) { |
|
299
|
|
|
$loader = new YamlFileLoader($this->container, $fileLocator); |
|
300
|
|
|
} elseif ( |
|
301
|
|
|
mb_strrpos($configFileName, $xmlExtension) === (mb_strlen($configFileName) - mb_strlen($xmlExtension)) |
|
302
|
|
|
) { |
|
303
|
|
|
$loader = new XmlFileLoader($this->container, $fileLocator); |
|
304
|
|
|
} else { |
|
305
|
|
|
throw Exception('Configuration file not supported: ' . $configFileName); |
|
306
|
|
|
} |
|
307
|
|
|
$loader->load($configFileName); |
|
308
|
|
|
|
|
309
|
|
|
$this->container->compile(); |
|
310
|
|
|
} |
|
311
|
|
|
} |
|
312
|
|
|
|