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

Commit eb9466b

Browse files
committed
Merge pull request #117 from symfony-cmf/extension_improvements
Improved Extension tests and error handling
2 parents 3b102bf + ee7a061 commit eb9466b

File tree

8 files changed

+110
-15
lines changed

8 files changed

+110
-15
lines changed

DependencyInjection/CmfRoutingAutoExtension.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Config\Definition\Processor;
1818
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1919
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
20+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
2021

2122
class CmfRoutingAutoExtension extends Extension
2223
{
@@ -39,7 +40,8 @@ public function load(array $configs, ContainerBuilder $container)
3940

4041
// auto mapping
4142
if ($config['auto_mapping']) {
42-
$resources = $this->findMappingFiles($container->getParameter('kernel.bundles'));
43+
$bundles = $container->getParameter('kernel.bundles');
44+
$resources = $this->findMappingFiles($bundles);
4345
}
4446

4547
// add configured mapping file resources
@@ -50,18 +52,29 @@ public function load(array $configs, ContainerBuilder $container)
5052
}
5153
$container->setParameter('cmf_routing_auto.metadata.loader.resources', $resources);
5254

55+
$hasProvider = false;
5356
if ($this->isConfigEnabled($container, $config['persistence']['phpcr'])) {
57+
$hasProvider = true;
58+
$loader->load('phpcr-odm.xml');
5459
$container->setParameter('cmf_routing_auto.persistence.phpcr.route_basepath', $config['persistence']['phpcr']['route_basepath']);
5560
}
61+
62+
if (!$hasProvider) {
63+
throw new InvalidConfigurationException(
64+
'The RoutingAUtoBundle requires that you enable one of the persistence layers in your application configuration. ' .
65+
'See the documentation for more information'
66+
);
67+
}
5668
}
5769

5870
protected function findMappingFiles($bundles)
5971
{
6072
$resources = array();
6173
foreach ($bundles as $bundle) {
62-
$obj = new $bundle;
74+
$refl = new \ReflectionClass($bundle);
75+
$bundlePath = dirname($refl->getFileName());
6376
foreach (array('xml', 'yml') as $extension) {
64-
$path = $obj->getPath().'/Resources/config/cmf_routing_auto.'.$extension;
77+
$path = $bundlePath.'/Resources/config/cmf_routing_auto.'.$extension;
6578
if (file_exists($path)) {
6679
$resources[] = array('path' => $path, 'type' => null);
6780
}

Resources/config/auto_route.xml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,6 @@
5050
<!-- Service Registry -->
5151
<service id="cmf_routing_auto.service_registry" class="%cmf_routing_auto.service_registry.class%" />
5252

53-
<!-- Adapters -->
54-
<service id="cmf_routing_auto.adapter.phpcr_odm" class="%cmf_routing_auto.adapter.phpcr_odm.class%">
55-
<argument type="service" id="doctrine_phpcr.odm.default_document_manager"/>
56-
<argument>%cmf_routing_auto.persistence.phpcr.route_basepath%</argument>
57-
</service>
58-
59-
<service id="cmf_routing_auto.phpcrodm_auto_route_listener" class="%cmf_routing_auto.phpcrodm_auto_route_listener.class%">
60-
<argument type="service" id="service_container"/>
61-
<tag name="doctrine_phpcr.event_listener" event="onFlush"/>
62-
<tag name="doctrine_phpcr.event_listener" event="endFlush"/>
63-
</service>
64-
6553
<!-- Metadata -->
6654
<service id="cmf_routing_auto.metadata.loader.yaml"
6755
class="%cmf_routing_auto.metadata.loader.yaml.class%"

Resources/config/phpcr-odm.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
6+
<parameters>
7+
8+
<parameter key="cmf_routing_auto.phpcrodm_auto_route_listener.class">Symfony\Cmf\Bundle\RoutingAutoBundle\Doctrine\Phpcr\AutoRouteListener</parameter>
9+
<parameter key="cmf_routing_auto.adapter.phpcr_odm.class">Symfony\Cmf\Bundle\RoutingAutoBundle\Adapter\PhpcrOdmAdapter</parameter>
10+
11+
</parameters>
12+
13+
<services>
14+
<!-- Adapters -->
15+
<service id="cmf_routing_auto.adapter.phpcr_odm" class="%cmf_routing_auto.adapter.phpcr_odm.class%">
16+
<argument type="service" id="doctrine_phpcr.odm.default_document_manager"/>
17+
<argument>%cmf_routing_auto.persistence.phpcr.route_basepath%</argument>
18+
</service>
19+
20+
<service id="cmf_routing_auto.phpcrodm_auto_route_listener" class="%cmf_routing_auto.phpcrodm_auto_route_listener.class%">
21+
<argument type="service" id="service_container"/>
22+
<tag name="doctrine_phpcr.event_listener" event="onFlush"/>
23+
<tag name="doctrine_phpcr.event_listener" event="endFlush"/>
24+
</service>
25+
</services>
26+
</container>

Tests/Resources/Bundle/TestBundle/Resources/config/cmf_routing_auto.xml

Whitespace-only changes.

Tests/Resources/Bundle/TestBundle/Resources/config/cmf_routing_auto.yml

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Bundle\TestBundle;
4+
5+
use Symfony\Component\HttpKernel\Bundle\Bundle;
6+
7+
class TestBundle extends Bundle
8+
{
9+
}

Tests/Resources/app/AppKernel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public function configure()
1414
$this->addBundles(array(
1515
new \Symfony\Cmf\Bundle\RoutingBundle\CmfRoutingBundle(),
1616
new \Symfony\Cmf\Bundle\RoutingAutoBundle\CmfRoutingAutoBundle(),
17+
18+
new \Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Bundle\TestBundle\TestBundle(),
1719
));
1820
}
1921

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Unit\DependencyInjection;
4+
5+
use Symfony\Component\DependencyInjection\ContainerBuilder;
6+
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
7+
use Symfony\Cmf\Bundle\RoutingAutoBundle\DependencyInjection\CmfRoutingAutoExtension;
8+
9+
class CmfRoutingAutoExtensionTest extends AbstractExtensionTestCase
10+
{
11+
public function setUp()
12+
{
13+
parent::setUp();
14+
15+
$this->setParameter('kernel.bundles', array(
16+
'Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Bundle\TestBundle\TestBundle'
17+
));
18+
}
19+
20+
protected function getContainerExtensions()
21+
{
22+
return array(
23+
new CmfRoutingAutoExtension()
24+
);
25+
}
26+
27+
protected function loadPhpcrOdm()
28+
{
29+
$this->load(array(
30+
'persistence' => array(
31+
'phpcr' => array(
32+
'enabled' => true
33+
)
34+
)
35+
));
36+
}
37+
38+
/**
39+
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
40+
* @expectedExceptionMessage enable one of the persistence layers
41+
*/
42+
public function testLoad()
43+
{
44+
$this->setParameter('kernel.bundles', array());
45+
$this->load();
46+
}
47+
48+
public function testAutoMappingRegistration()
49+
{
50+
$this->loadPhpcrOdm();
51+
52+
$resources = $this->container->getParameter('cmf_routing_auto.metadata.loader.resources');
53+
54+
// both the YAML and the XML files in the TestBundle have been registered
55+
$this->assertCount(2, $resources);
56+
}
57+
}

0 commit comments

Comments
 (0)