Skip to content

Commit 7f3597c

Browse files
authored
Merge pull request #364 from doctrine/manager-loader
Add manager loader support introduced in migrations 3.1 (dev)
2 parents 0185170 + 24854ad commit 7f3597c

File tree

4 files changed

+94
-11
lines changed

4 files changed

+94
-11
lines changed

DependencyInjection/CompilerPass/ConfigureDependencyFactoryPass.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,41 @@ class ConfigureDependencyFactoryPass implements CompilerPassInterface
1717
{
1818
public function process(ContainerBuilder $container): void
1919
{
20-
$preferredEm = $container->getParameter('doctrine.migrations.preferred_em');
2120
$diDefinition = $container->getDefinition('doctrine.migrations.dependency_factory');
2221

22+
$preferredEm = $container->getParameter('doctrine.migrations.preferred_em');
23+
if ($container->has('doctrine')) {
24+
$loaderDefinition = $container->getDefinition('doctrine.migrations.registry_loader');
25+
$loaderDefinition->setArgument(0, new Reference('doctrine'));
26+
if ($preferredEm !== null) {
27+
$loaderDefinition->setArgument(1, $preferredEm);
28+
}
29+
30+
$diDefinition->setFactory([DependencyFactory::class, 'fromEntityManager']);
31+
$diDefinition->setArgument(1, new Reference('doctrine.migrations.registry_loader'));
32+
33+
return;
34+
}
35+
2336
assert(is_string($preferredEm) || $preferredEm === null);
2437
$emID = sprintf('doctrine.orm.%s_entity_manager', $preferredEm ?? 'default');
25-
2638
if ($container->has($emID)) {
2739
$container->getDefinition('doctrine.migrations.em_loader')
2840
->setArgument(0, new Reference($emID));
2941

3042
$diDefinition->setFactory([DependencyFactory::class, 'fromEntityManager']);
3143
$diDefinition->setArgument(1, new Reference('doctrine.migrations.em_loader'));
32-
} else {
33-
$preferredConnection = $container->getParameter('doctrine.migrations.preferred_connection');
44+
45+
return;
46+
}
47+
48+
$preferredConnection = $container->getParameter('doctrine.migrations.preferred_connection');
3449
assert(is_string($preferredConnection) || $preferredConnection === null);
3550
$connectionId = sprintf('doctrine.dbal.%s_connection', $preferredConnection ?? 'default');
36-
$container->getDefinition('doctrine.migrations.connection_loader')
37-
->setArgument(0, new Reference($connectionId));
51+
$container->getDefinition('doctrine.migrations.connection_loader')
52+
->setArgument(0, new Reference($connectionId));
3853

39-
$diDefinition->setFactory([DependencyFactory::class, 'fromConnection']);
40-
$diDefinition->setArgument(1, new Reference('doctrine.migrations.connection_loader'));
41-
}
54+
$diDefinition->setFactory([DependencyFactory::class, 'fromConnection']);
55+
$diDefinition->setArgument(1, new Reference('doctrine.migrations.connection_loader'));
4256
}
4357
}

Resources/config/services.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525

2626
</service>
2727

28+
<service id="doctrine.migrations.registry_loader" class="Doctrine\Migrations\Configuration\EntityManager\ManagerRegistryEntityManager" public="false">
29+
<factory class="Doctrine\Migrations\Configuration\EntityManager\ManagerRegistryEntityManager" method="withSimpleDefault"/>
30+
<argument type="service" id="doctrine"/>
31+
</service>
32+
2833
<service id="doctrine.migrations.configuration" class="Doctrine\Migrations\Configuration\Configuration" public="false">
2934
</service>
3035

Tests/DependencyInjection/DoctrineMigrationsExtensionTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Doctrine\Migrations\Version\Comparator;
1818
use Doctrine\Migrations\Version\Version;
1919
use Doctrine\ORM\EntityManager;
20+
use Doctrine\Persistence\ManagerRegistry;
2021
use Exception;
2122
use InvalidArgumentException;
2223
use PHPUnit\Framework\TestCase;
@@ -300,6 +301,68 @@ public function testPrefersEntityManagerOverConnection(): void
300301
self::assertSame($em, $di->getEntityManager());
301302
}
302303

304+
/**
305+
* @return array<array{string|null}>
306+
*/
307+
public function getEmNames(): array
308+
{
309+
return [
310+
[null],
311+
['custom'],
312+
];
313+
}
314+
315+
/**
316+
* @dataProvider getEmNames
317+
*/
318+
public function testPrefersManagerRegistry(?string $emName): void
319+
{
320+
$config = ['em' => $emName];
321+
$container = $this->getContainer($config);
322+
323+
$em = $this->createMock(EntityManager::class);
324+
$registryMock = $this->createMock(ManagerRegistry::class);
325+
326+
$registryMock->expects(self::once())
327+
->method('getManager')
328+
->with($emName)
329+
->willReturn($em);
330+
331+
$container->set('doctrine', $registryMock);
332+
333+
$container->compile();
334+
335+
$di = $container->get('doctrine.migrations.dependency_factory');
336+
337+
self::assertInstanceOf(DependencyFactory::class, $di);
338+
self::assertSame($em, $di->getEntityManager());
339+
}
340+
341+
public function testManagerRegistryThrowsExceptionOnUnknownEm(): void
342+
{
343+
$config = ['em' => 'foo'];
344+
$container = $this->getContainer($config);
345+
346+
$em = $this->createMock(EntityManager::class);
347+
$registryMock = $this->createMock(ManagerRegistry::class);
348+
349+
$registryMock->expects(self::once())
350+
->method('getManager')
351+
->with('foo')
352+
->willThrowException(new InvalidArgumentException());
353+
354+
$container->set('doctrine', $registryMock);
355+
356+
$container->compile();
357+
358+
$di = $container->get('doctrine.migrations.dependency_factory');
359+
360+
self::assertInstanceOf(DependencyFactory::class, $di);
361+
362+
$this->expectException(InvalidArgumentException::class);
363+
self::assertSame($em, $di->getEntityManager());
364+
}
365+
303366
public function testCustomEntityManager(): void
304367
{
305368
$config = [

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"php": "^7.2|^8.0",
2424
"symfony/framework-bundle": "~3.4|~4.0|~5.0",
2525
"doctrine/doctrine-bundle": "~1.0|~2.0",
26-
"doctrine/migrations": "^3.0.3"
26+
"doctrine/migrations": "^3.1"
2727
},
2828
"require-dev": {
2929
"phpunit/phpunit": "^7.0|^8.0|^9.0",
@@ -32,7 +32,8 @@
3232
"phpstan/phpstan-deprecation-rules": "^0.12",
3333
"phpstan/phpstan-phpunit": "^0.12",
3434
"phpstan/phpstan-strict-rules": "^0.12",
35-
"doctrine/orm": "^2.6"
35+
"doctrine/orm": "^2.6",
36+
"doctrine/persistence": "^1.3||^2.0"
3637
},
3738
"autoload": {
3839
"psr-4": { "Doctrine\\Bundle\\MigrationsBundle\\": "" },

0 commit comments

Comments
 (0)