Skip to content

Commit 8cb4619

Browse files
authored
Merge pull request #534 from thomasvargiu/feature/platform-service
Allow retrieving platform from container
2 parents 7c7b47e + 84dbd97 commit 8cb4619

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

src/DoctrineORMModule/Service/DBALConnectionFactory.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
5858
];
5959
$params = array_merge($params, $options->getParams());
6060

61+
if (
62+
array_key_exists('platform', $params)
63+
&& is_string($params['platform'])
64+
&& $container->has($params['platform'])
65+
) {
66+
$params['platform'] = $container->get($params['platform']);
67+
}
68+
6169
$configuration = $container->get($options->getConfiguration());
6270
$eventManager = $container->get($options->getEventManager());
6371

tests/DoctrineORMModuleTest/Service/DBALConnectionFactoryTest.php

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@
1919

2020
namespace DoctrineORMModuleTest\Service;
2121

22-
use PHPUnit\Framework\TestCase;
22+
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
23+
use Doctrine\DBAL\Driver\PDOSqlite\Driver as PDOSqliteDriver;
24+
use Doctrine\DBAL\Platforms\AbstractPlatform;
25+
use Doctrine\ORM\Configuration;
2326
use DoctrineORMModuleTest\Assets\Types\MoneyType;
27+
use PHPUnit\Framework\TestCase;
2428
use DoctrineORMModule\Service\DBALConnectionFactory;
2529
use Doctrine\DBAL\Types\Type;
2630
use Doctrine\Common\Cache\ArrayCache;
@@ -59,15 +63,15 @@ public function testNoConnectWithoutCustomMappingsAndCommentedTypes()
5963
'doctrine' => [
6064
'connection' => [
6165
'orm_default' => [
62-
'driverClass' => \Doctrine\DBAL\Driver\PDOSqlite\Driver::class,
66+
'driverClass' => PDOSqliteDriver::class,
6367
'params' => [
6468
'memory' => true,
6569
],
6670
]
6771
],
6872
],
6973
];
70-
$configurationMock = $this->getMockBuilder(\Doctrine\ORM\Configuration::class)
74+
$configurationMock = $this->getMockBuilder(Configuration::class)
7175
->disableOriginalConstructor()
7276
->getMock();
7377

@@ -85,7 +89,7 @@ public function testDoctrineMappingTypeReturnCorrectParent()
8589
'doctrine' => [
8690
'connection' => [
8791
'orm_default' => [
88-
'driverClass' => \Doctrine\DBAL\Driver\PDOSqlite\Driver::class,
92+
'driverClass' => PDOSqliteDriver::class,
8993
'params' => [
9094
'memory' => true,
9195
],
@@ -96,7 +100,7 @@ public function testDoctrineMappingTypeReturnCorrectParent()
96100
],
97101
],
98102
];
99-
$configurationMock = $this->getMockBuilder(\Doctrine\ORM\Configuration::class)
103+
$configurationMock = $this->getMockBuilder(Configuration::class)
100104
->disableOriginalConstructor()
101105
->getMock();
102106

@@ -115,7 +119,7 @@ public function testDoctrineAddCustomCommentedType()
115119
'doctrine' => [
116120
'connection' => [
117121
'orm_default' => [
118-
'driverClass' => \Doctrine\DBAL\Driver\PDOSqlite\Driver::class,
122+
'driverClass' => PDOSqliteDriver::class,
119123
'params' => [
120124
'memory' => true,
121125
],
@@ -130,7 +134,7 @@ public function testDoctrineAddCustomCommentedType()
130134
'configuration' => [
131135
'orm_default' => [
132136
'types' => [
133-
'money' => \DoctrineORMModuleTest\Assets\Types\MoneyType::class,
137+
'money' => MoneyType::class,
134138
],
135139
],
136140
],
@@ -140,7 +144,7 @@ public function testDoctrineAddCustomCommentedType()
140144
$this->serviceManager->setService('Configuration', $config);
141145
$this->serviceManager->setService(
142146
'doctrine.driver.orm_default',
143-
$this->createMock(\Doctrine\Common\Persistence\Mapping\Driver\MappingDriver::class)
147+
$this->createMock(MappingDriver::class)
144148
);
145149
$configurationFactory = new ConfigurationFactory('orm_default');
146150
$this->serviceManager->setService(
@@ -151,7 +155,39 @@ public function testDoctrineAddCustomCommentedType()
151155
$platform = $dbal->getDatabasePlatform();
152156
$type = Type::getType($platform->getDoctrineTypeMapping("money"));
153157

154-
$this->assertInstanceOf(\DoctrineORMModuleTest\Assets\Types\MoneyType::class, $type);
158+
$this->assertInstanceOf(MoneyType::class, $type);
155159
$this->assertTrue($platform->isCommentedDoctrineType($type));
156160
}
161+
162+
public function testGettingPlatformFromContainer()
163+
{
164+
$config = [
165+
'doctrine' => [
166+
'connection' => [
167+
'orm_default' => [
168+
'driverClass' => PDOSqliteDriver::class,
169+
'params' => [
170+
'platform' => 'platform_service',
171+
],
172+
]
173+
],
174+
],
175+
];
176+
$configurationMock = $this->getMockBuilder(Configuration::class)
177+
->disableOriginalConstructor()
178+
->getMock();
179+
180+
$platformMock = $this->getMockBuilder(AbstractPlatform::class)
181+
->disableOriginalConstructor()
182+
->getMock();
183+
184+
$this->serviceManager->setService('doctrine.configuration.orm_default', $configurationMock);
185+
$this->serviceManager->setService('config', $config);
186+
$this->serviceManager->setService('Configuration', $config);
187+
$this->serviceManager->setService('platform_service', $platformMock);
188+
189+
$dbal = $this->factory->createService($this->serviceManager);
190+
$platform = $dbal->getDatabasePlatform();
191+
$this->assertSame($platformMock, $platform);
192+
}
157193
}

0 commit comments

Comments
 (0)