/
IntegrationTest.php
366 lines (296 loc) · 14.1 KB
/
IntegrationTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
declare(strict_types=1);
namespace Doctrine\Bundle\FixturesBundle\Tests\IntegrationTest;
use Doctrine\Bundle\FixturesBundle\DependencyInjection\CompilerPass\FixturesCompilerPass;
use Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle;
use Doctrine\Bundle\FixturesBundle\Tests\Fixtures\FooBundle\DataFixtures\DependentOnRequiredConstructorArgsFixtures;
use Doctrine\Bundle\FixturesBundle\Tests\Fixtures\FooBundle\DataFixtures\OtherFixtures;
use Doctrine\Bundle\FixturesBundle\Tests\Fixtures\FooBundle\DataFixtures\RequiredConstructorArgsFixtures;
use Doctrine\Bundle\FixturesBundle\Tests\Fixtures\FooBundle\DataFixtures\WithDependenciesFixtures;
use Doctrine\Bundle\FixturesBundle\Tests\Fixtures\FooBundle\FooBundle;
use Doctrine\Common\DataFixtures\Loader;
use Doctrine\Common\Persistence\ManagerRegistry;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\RouteCollectionBuilder;
use function array_map;
use function get_class;
use function method_exists;
use function rand;
use function sys_get_temp_dir;
class IntegrationTest extends TestCase
{
public function testFixturesLoader() : void
{
$kernel = new IntegrationTestKernel('dev', true);
$kernel->addServices(static function (ContainerBuilder $c) : void {
$c->autowire(OtherFixtures::class)
->addTag(FixturesCompilerPass::FIXTURE_TAG);
$c->autowire(WithDependenciesFixtures::class)
->addTag(FixturesCompilerPass::FIXTURE_TAG);
$c->setAlias('test.doctrine.fixtures.loader', new Alias('doctrine.fixtures.loader', true));
});
$kernel->boot();
$container = $kernel->getContainer();
/** @var ContainerAwareLoader $loader */
$loader = $container->get('test.doctrine.fixtures.loader');
$actualFixtures = $loader->getFixtures();
$this->assertCount(2, $actualFixtures);
$actualFixtureClasses = array_map(static function ($fixture) {
return get_class($fixture);
}, $actualFixtures);
$this->assertSame([
OtherFixtures::class,
WithDependenciesFixtures::class,
], $actualFixtureClasses);
$this->assertInstanceOf(WithDependenciesFixtures::class, $actualFixtures[1]);
}
public function testFixturesLoaderWhenFixtureHasDepdencenyThatIsNotYetLoaded() : void
{
// See https://github.com/doctrine/DoctrineFixturesBundle/issues/215
$kernel = new IntegrationTestKernel('dev', true);
$kernel->addServices(static function (ContainerBuilder $c) : void {
$c->autowire(WithDependenciesFixtures::class)
->addTag(FixturesCompilerPass::FIXTURE_TAG);
$c->autowire(OtherFixtures::class)
->addTag(FixturesCompilerPass::FIXTURE_TAG);
$c->setAlias('test.doctrine.fixtures.loader', new Alias('doctrine.fixtures.loader', true));
});
$kernel->boot();
$container = $kernel->getContainer();
/** @var ContainerAwareLoader $loader */
$loader = $container->get('test.doctrine.fixtures.loader');
$actualFixtures = $loader->getFixtures();
$this->assertCount(2, $actualFixtures);
$actualFixtureClasses = array_map(static function ($fixture) {
return get_class($fixture);
}, $actualFixtures);
$this->assertSame([
OtherFixtures::class,
WithDependenciesFixtures::class,
], $actualFixtureClasses);
$this->assertInstanceOf(WithDependenciesFixtures::class, $actualFixtures[1]);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage The getDependencies() method returned a class (Doctrine\Bundle\FixturesBundle\Tests\Fixtures\FooBundle\DataFixtures\RequiredConstructorArgsFixtures) that has required constructor arguments. Upgrade to "doctrine/data-fixtures" version 1.3 or higher to support this.
*/
public function testExceptionWithDependenciesWithRequiredArguments() : void
{
// see https://github.com/doctrine/data-fixtures/pull/274
// When that is merged, this test will only run when using
// an older version of that library.
if (method_exists(Loader::class, 'createFixture')) {
$this->markTestSkipped();
}
$kernel = new IntegrationTestKernel('dev', true);
$kernel->addServices(static function (ContainerBuilder $c) : void {
$c->autowire(DependentOnRequiredConstructorArgsFixtures::class)
->addTag(FixturesCompilerPass::FIXTURE_TAG);
$c->autowire(RequiredConstructorArgsFixtures::class)
->setArgument(0, 'foo')
->addTag(FixturesCompilerPass::FIXTURE_TAG);
$c->setAlias('test.doctrine.fixtures.loader', new Alias('doctrine.fixtures.loader', true));
});
$kernel->boot();
$container = $kernel->getContainer();
/** @var ContainerAwareLoader $loader */
$loader = $container->get('test.doctrine.fixtures.loader');
$loader->getFixtures();
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage The "Doctrine\Bundle\FixturesBundle\Tests\Fixtures\FooBundle\DataFixtures\RequiredConstructorArgsFixtures" fixture class is trying to be loaded, but is not available. Make sure this class is defined as a service and tagged with "doctrine.fixture.orm".
*/
public function testExceptionIfDependentFixtureNotWired() : void
{
// only runs on newer versions of doctrine/data-fixtures
if (! method_exists(Loader::class, 'createFixture')) {
$this->markTestSkipped();
}
$kernel = new IntegrationTestKernel('dev', true);
$kernel->addServices(static function (ContainerBuilder $c) : void {
$c->autowire(DependentOnRequiredConstructorArgsFixtures::class)
->addTag(FixturesCompilerPass::FIXTURE_TAG);
$c->setAlias('test.doctrine.fixtures.loader', new Alias('doctrine.fixtures.loader', true));
});
$kernel->boot();
$container = $kernel->getContainer();
/** @var ContainerAwareLoader $loader */
$loader = $container->get('test.doctrine.fixtures.loader');
$loader->getFixtures();
}
public function testFixturesLoaderWithGroupsOptionViaInterface() : void
{
$kernel = new IntegrationTestKernel('dev', true);
$kernel->addServices(static function (ContainerBuilder $c) : void {
// has a "staging" group via the getGroups() method
$c->autowire(OtherFixtures::class)
->addTag(FixturesCompilerPass::FIXTURE_TAG);
// no getGroups() method
$c->autowire(WithDependenciesFixtures::class)
->addTag(FixturesCompilerPass::FIXTURE_TAG);
$c->setAlias('test.doctrine.fixtures.loader', new Alias('doctrine.fixtures.loader', true));
});
$kernel->boot();
$container = $kernel->getContainer();
/** @var ContainerAwareLoader $loader */
$loader = $container->get('test.doctrine.fixtures.loader');
$actualFixtures = $loader->getFixtures(['staging']);
$this->assertCount(1, $actualFixtures);
$actualFixtureClasses = array_map(static function ($fixture) {
return get_class($fixture);
}, $actualFixtures);
$this->assertSame([
OtherFixtures::class,
], $actualFixtureClasses);
$this->assertInstanceOf(OtherFixtures::class, $actualFixtures[0]);
}
public function testFixturesLoaderWithGroupsOptionViaTag() : void
{
$kernel = new IntegrationTestKernel('dev', true);
$kernel->addServices(static function (ContainerBuilder $c) : void {
// has a "staging" group via the getGroups() method
$c->autowire(OtherFixtures::class)
->addTag(FixturesCompilerPass::FIXTURE_TAG, ['group' => 'group1'])
->addTag(FixturesCompilerPass::FIXTURE_TAG, ['group' => 'group2']);
// no getGroups() method
$c->autowire(WithDependenciesFixtures::class)
->addTag(FixturesCompilerPass::FIXTURE_TAG, ['group' => 'group2']);
$c->setAlias('test.doctrine.fixtures.loader', new Alias('doctrine.fixtures.loader', true));
});
$kernel->boot();
$container = $kernel->getContainer();
/** @var ContainerAwareLoader $loader */
$loader = $container->get('test.doctrine.fixtures.loader');
$this->assertCount(1, $loader->getFixtures(['staging']));
$this->assertCount(1, $loader->getFixtures(['group1']));
$this->assertCount(2, $loader->getFixtures(['group2']));
$this->assertCount(0, $loader->getFixtures(['group3']));
}
public function testLoadFixturesViaGroupWithMissingDependency() : void
{
$kernel = new IntegrationTestKernel('dev', true);
$kernel->addServices(static function (ContainerBuilder $c) : void {
// has a "staging" group via the getGroups() method
$c->autowire(OtherFixtures::class)
->addTag(FixturesCompilerPass::FIXTURE_TAG);
// no getGroups() method
$c->autowire(WithDependenciesFixtures::class)
->addTag(FixturesCompilerPass::FIXTURE_TAG);
$c->setAlias('test.doctrine.fixtures.loader', new Alias('doctrine.fixtures.loader', true));
});
$kernel->boot();
$container = $kernel->getContainer();
/** @var ContainerAwareLoader $loader */
$loader = $container->get('test.doctrine.fixtures.loader');
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Fixture "Doctrine\Bundle\FixturesBundle\Tests\Fixtures\FooBundle\DataFixtures\OtherFixtures" was declared as a dependency for fixture "Doctrine\Bundle\FixturesBundle\Tests\Fixtures\FooBundle\DataFixtures\WithDependenciesFixtures", but it was not included in any of the loaded fixture groups.');
$loader->getFixtures(['missingDependencyGroup']);
}
public function testLoadFixturesViaGroupWithFulfilledDependency() : void
{
$kernel = new IntegrationTestKernel('dev', true);
$kernel->addServices(static function (ContainerBuilder $c) : void {
// has a "staging" group via the getGroups() method
$c->autowire(OtherFixtures::class)
->addTag(FixturesCompilerPass::FIXTURE_TAG);
// no getGroups() method
$c->autowire(WithDependenciesFixtures::class)
->addTag(FixturesCompilerPass::FIXTURE_TAG);
$c->setAlias('test.doctrine.fixtures.loader', new Alias('doctrine.fixtures.loader', true));
});
$kernel->boot();
$container = $kernel->getContainer();
/** @var ContainerAwareLoader $loader */
$loader = $container->get('test.doctrine.fixtures.loader');
$actualFixtures = $loader->getFixtures(['fulfilledDependencyGroup']);
$this->assertCount(2, $actualFixtures);
$actualFixtureClasses = array_map(static function ($fixture) {
return get_class($fixture);
}, $actualFixtures);
$this->assertSame([
OtherFixtures::class,
WithDependenciesFixtures::class,
], $actualFixtureClasses);
}
public function testLoadFixturesByShortName() : void
{
$kernel = new IntegrationTestKernel('dev', true);
$kernel->addServices(static function (ContainerBuilder $c) : void {
// has a "staging" group via the getGroups() method
$c->autowire(OtherFixtures::class)
->addTag(FixturesCompilerPass::FIXTURE_TAG);
// no getGroups() method
$c->autowire(WithDependenciesFixtures::class)
->addTag(FixturesCompilerPass::FIXTURE_TAG);
$c->setAlias('test.doctrine.fixtures.loader', new Alias('doctrine.fixtures.loader', true));
});
$kernel->boot();
$container = $kernel->getContainer();
/** @var ContainerAwareLoader $loader */
$loader = $container->get('test.doctrine.fixtures.loader');
$actualFixtures = $loader->getFixtures(['OtherFixtures']);
$this->assertCount(1, $actualFixtures);
$actualFixtureClasses = array_map(static function ($fixture) {
return get_class($fixture);
}, $actualFixtures);
$this->assertSame([
OtherFixtures::class,
], $actualFixtureClasses);
}
}
class IntegrationTestKernel extends Kernel
{
use MicroKernelTrait;
/** @var callable */
private $servicesCallback;
/** @var int */
private $randomKey;
public function __construct(string $environment, bool $debug)
{
$this->randomKey = rand(100, 999);
parent::__construct($environment, $debug);
}
protected function getContainerClass() : string
{
return 'test' . $this->randomKey . parent::getContainerClass();
}
public function registerBundles() : array
{
return [
new FrameworkBundle(),
new DoctrineFixturesBundle(),
new FooBundle(),
];
}
public function addServices(callable $callback) : void
{
$this->servicesCallback = $callback;
}
protected function configureRoutes(RouteCollectionBuilder $routes) : void
{
}
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) : void
{
$c->setParameter('kernel.secret', 'foo');
$c->register('doctrine', ManagerRegistry::class);
$callback = $this->servicesCallback;
$callback($c);
}
public function getCacheDir() : string
{
return sys_get_temp_dir() . '/doctrine_fixtures_bundle' . $this->randomKey;
}
public function getLogDir() : string
{
return sys_get_temp_dir();
}
}