Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit c37c4d0

Browse files
committed
Can override implicit adapter
1 parent 4b2e3af commit c37c4d0

File tree

4 files changed

+82
-11
lines changed

4 files changed

+82
-11
lines changed

DependencyInjection/CmfRoutingAutoExtension.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,31 @@ public function load(array $configs, ContainerBuilder $container)
5151
}
5252
}
5353
$container->setParameter('cmf_routing_auto.metadata.loader.resources', $resources);
54-
$container->setParameter('cmf_routing_auto.adapter_name', $config['adapter']);
5554

5655
$hasProvider = false;
5756

57+
$adapterName = null;
58+
if (isset($config['adapter'])) {
59+
$adapterName = $config['adapter'];
60+
}
61+
5862
if ($this->isConfigEnabled($container, $config['persistence']['phpcr'])) {
5963
$hasProvider = true;
6064
$loader->load('phpcr-odm.xml');
65+
if (null === $adapterName) {
66+
$adapterName = 'doctrine_phpcr_odm';
67+
}
6168
$container->setParameter('cmf_routing_auto.persistence.phpcr.route_basepath', $config['persistence']['phpcr']['route_basepath']);
6269
}
70+
71+
if (false === $hasProvider && null === $adapterName) {
72+
throw new InvalidConfigurationException(sprintf(
73+
'No adapter has been configured, you either need to enable a persistence layer or '.
74+
'explicitly specify an adapter using the "adapter" configuration key.'
75+
));
76+
}
77+
78+
$container->setParameter('cmf_routing_auto.adapter_name', $adapterName);
6379
}
6480

6581
protected function findMappingFiles($bundles)

DependencyInjection/Configuration.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ public function getConfigTreeBuilder()
2828
$treeBuilder->root('cmf_routing_auto')
2929
->addDefaultsIfNotSet()
3030
->children()
31-
->scalarNode('adapter')
32-
->defaultValue('doctrine_phpcr_odm')
33-
->end()
31+
->scalarNode('adapter')->info('Use a specific adapter, overrides any implicit selection')->end()
3432
->booleanNode('auto_mapping')->defaultTrue()->end()
3533
->arrayNode('mapping')
3634
->fixXmlConfig('resource')

Tests/Unit/DependencyInjection/CmfRoutingAutoExtensionTest.php

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,89 @@ public function setUp()
1717
));
1818
}
1919

20-
protected function getContainerExtensions()
20+
/**
21+
* An exception should be thrown if an adapter has not been explicitly or
22+
* implicitly configured.
23+
*
24+
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
25+
* @expectedExceptionMessage No adapter has been configured, you either need to
26+
*/
27+
public function testLoad()
2128
{
22-
return array(
23-
new CmfRoutingAutoExtension()
24-
);
29+
$this->setParameter('kernel.bundles', array());
30+
$this->load();
2531
}
2632

27-
protected function loadPhpcrOdm()
33+
/**
34+
* It should be possible to explicitly specify an adapter
35+
*/
36+
public function testExplicitAdapter()
2837
{
2938
$this->load(array(
39+
'adapter' => 'foobar'
40+
));
41+
42+
$adapter = $this->container->getParameter('cmf_routing_auto.adapter_name');
43+
$this->assertEquals('foobar', $adapter);
44+
}
45+
46+
/**
47+
* The adapter should be implicitly configured if the PHPCR ODM integration has
48+
* been enabled
49+
*/
50+
public function testImplicitPhpcrOdmAdapter()
51+
{
52+
$this->loadPhpcrOdm();
53+
54+
$adapter = $this->container->getParameter('cmf_routing_auto.adapter_name');
55+
$this->assertEquals('doctrine_phpcr_odm', $adapter);
56+
}
57+
58+
/**
59+
* It should be possible to override the implicitly configured adapter
60+
*/
61+
public function testOverrideImplicitPhpcrOdmAdapter()
62+
{
63+
$this->load(array(
64+
'adapter' => 'foobar',
3065
'persistence' => array(
3166
'phpcr' => array(
3267
'enabled' => true
3368
)
3469
)
3570
));
71+
$adapter = $this->container->getParameter('cmf_routing_auto.adapter_name');
72+
$this->assertEquals('foobar', $adapter);
3673
}
3774

75+
/**
76+
* The bundle should automatically register routing auto mapping configuration in
77+
* the Resources/config directory.
78+
*/
3879
public function testAutoMappingRegistration()
3980
{
4081
$this->loadPhpcrOdm();
4182

4283
$resources = $this->container->getParameter('cmf_routing_auto.metadata.loader.resources');
4384

44-
// both the YAML and the XML files in the TestBundle have been registered
4585
$this->assertCount(2, $resources);
4686
}
87+
88+
protected function getContainerExtensions()
89+
{
90+
return array(
91+
new CmfRoutingAutoExtension()
92+
);
93+
}
94+
95+
protected function loadPhpcrOdm()
96+
{
97+
$this->load(array(
98+
'persistence' => array(
99+
'phpcr' => array(
100+
'enabled' => true
101+
)
102+
)
103+
));
104+
}
47105
}

Tests/Unit/DependencyInjection/ConfigurationTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ protected function getConfiguration()
3131
public function testSupportsAllConfigFormats()
3232
{
3333
$expectedConfiguration = array(
34-
'adapter' => 'doctrine_phpcr_odm',
3534
'auto_mapping' => false,
3635
'mapping' => array(
3736
'resources' => array(

0 commit comments

Comments
 (0)