Skip to content

Commit a4e3712

Browse files
committed
Merge pull request #198 from WouterJ/di_testing
Improved DI Testing (config, extension, compiler)
2 parents 186013b + bd0b68f commit a4e3712

File tree

10 files changed

+279
-171
lines changed

10 files changed

+279
-171
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Changelog
22
=========
33

4+
* **2013-11-28**: [BC BREAK] the alias attribute of the <template-by-class> is
5+
renamed to class in the bundle configuration.
6+
47
1.1.0
58
-----
69

DependencyInjection/Compiler/SetRouterPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function process(ContainerBuilder $container)
2525
{
2626

2727
// only replace the default router by overwriting the 'router' alias if config tells us to
28-
if (true === $container->getParameter('cmf_routing.replace_symfony_router')) {
28+
if ($container->hasParameter('cmf_routing.replace_symfony_router') && true === $container->getParameter('cmf_routing.replace_symfony_router')) {
2929
$container->setAlias('router', 'cmf_routing.router');
3030
}
3131

DependencyInjection/Configuration.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function getConfigTreeBuilder()
6767
->prototype('scalar')->end()
6868
->end()
6969
->arrayNode('templates_by_class')
70-
->useAttributeAsKey('alias')
70+
->useAttributeAsKey('class')
7171
->prototype('scalar')->end()
7272
->end()
7373
->arrayNode('persistence')
@@ -81,6 +81,21 @@ public function getConfigTreeBuilder()
8181
->scalarNode('route_basepath')->defaultValue('/cms/routes')->end()
8282
->scalarNode('content_basepath')->defaultValue('/cms/content')->end()
8383
->enumNode('use_sonata_admin')
84+
->beforeNormalization()
85+
->ifString()
86+
->then(function ($v) {
87+
switch ($v) {
88+
case 'true':
89+
return true;
90+
91+
case 'false':
92+
return false;
93+
94+
default:
95+
return $v;
96+
}
97+
})
98+
->end()
8499
->values(array(true, false, 'auto'))
85100
->defaultValue('auto')
86101
->end()
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
$container->loadFromExtension('cmf_routing', array(
4+
'chain' => array(
5+
'routers_by_id' => array(
6+
'cmf_routing.router' => 300,
7+
'router.default' => 100,
8+
),
9+
'replace_symfony_router' => true,
10+
),
11+
'dynamic' => array(
12+
'generic_controller' => 'acme_main.controller:mainAction',
13+
'controllers_by_type' => array(
14+
'editable' => 'acme_main.some_controller:editableAction',
15+
),
16+
'controllers_by_class' => array(
17+
'Symfony\Cmf\Bundle\ContentBundle\Document\StaticContent' => 'cmf_content.controller:indexAction',
18+
),
19+
'templates_by_class' => array(
20+
'Symfony\Cmf\Bundle\ContentBundle\Document\StaticContent' => 'CmfContentBundle:StaticContent:index.html.twig',
21+
),
22+
'persistence' => array(
23+
'phpcr' => array(
24+
'route_basepath' => '/cms/routes',
25+
'content_basepath' => '/cms/content',
26+
'use_sonata_admin' => 'false',
27+
),
28+
),
29+
'locales' => array('en', 'fr'),
30+
),
31+
));
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services">
3+
4+
<config xmlns="http://cmf.symfony.com/schema/dic/routing">
5+
6+
<chain replace-symfony-router="true">
7+
<router-by-id id="cmf_routing.router">300</router-by-id>
8+
<router-by-id id="router.default">100</router-by-id>
9+
</chain>
10+
11+
<dynamic generic-controller="acme_main.controller:mainAction">
12+
<controller-by-type type="editable">acme_main.some_controller:editableAction</controller-by-type>
13+
<controller-by-class class="Symfony\Cmf\Bundle\ContentBundle\Document\StaticContent">
14+
cmf_content.controller:indexAction
15+
</controller-by-class>
16+
<template-by-class class="Symfony\Cmf\Bundle\ContentBundle\Document\StaticContent">
17+
CmfContentBundle:StaticContent:index.html.twig
18+
</template-by-class>
19+
20+
<persistence>
21+
<phpcr
22+
route-basepath="/cms/routes"
23+
content-basepath="/cms/content"
24+
use-sonata-admin="false"
25+
/>
26+
</persistence>
27+
28+
<locale>en</locale>
29+
<locale>fr</locale>
30+
</dynamic>
31+
</config>
32+
</container>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
cmf_routing:
2+
chain:
3+
routers_by_id:
4+
cmf_routing.router: 300
5+
router.default: 100
6+
replace_symfony_router: true
7+
dynamic:
8+
generic_controller: acme_main.controller:mainAction
9+
controllers_by_type:
10+
editable: acme_main.some_controller:editableAction
11+
controllers_by_class:
12+
Symfony\Cmf\Bundle\ContentBundle\Document\StaticContent: cmf_content.controller:indexAction
13+
templates_by_class:
14+
Symfony\Cmf\Bundle\ContentBundle\Document\StaticContent: CmfContentBundle:StaticContent:index.html.twig
15+
persistence:
16+
phpcr:
17+
route_basepath: /cms/routes
18+
content_basepath: /cms/content
19+
use_sonata_admin: false
20+
locales: [en, fr]

Tests/Unit/DependencyInjection/CmfRoutingExtensionTest.php

Lines changed: 73 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -12,180 +12,107 @@
1212

1313
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\DependencyInjection;
1414

15+
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
1516
use Symfony\Cmf\Bundle\RoutingBundle\DependencyInjection\CmfRoutingExtension;
16-
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Reference;
1718

18-
class CmfRoutingExtensionTest extends \PHPUnit_Framework_TestCase
19+
class CmfRoutingExtensionTest extends AbstractExtensionTestCase
1920
{
20-
21-
/**
22-
* @param array $config
23-
* @return \Symfony\Component\DependencyInjection\ContainerBuilder
24-
*/
25-
protected function getBuilder(array $config = array())
21+
protected function getContainerExtensions()
2622
{
27-
$builder = new ContainerBuilder();
28-
29-
$loader = new CmfRoutingExtension();
30-
$loader->load($config, $builder);
31-
32-
return $builder;
23+
return array(
24+
new CmfRoutingExtension(),
25+
);
3326
}
3427

3528
public function testLoadDefault()
3629
{
37-
38-
$builder = $this->getBuilder(
39-
array(
40-
array(
41-
'dynamic' => array(
30+
$this->load(array(
31+
'dynamic' => array(
32+
'enabled' => true,
33+
'persistence' => array(
34+
'phpcr' => array(
4235
'enabled' => true,
43-
'persistence' => array(
44-
'phpcr' => array(
45-
'enabled' => true,
46-
'use_sonata_admin' => false,
47-
),
48-
),
36+
'use_sonata_admin' => false,
4937
),
50-
)
51-
)
52-
);
53-
54-
$this->assertTrue($builder->hasAlias('cmf_routing.route_provider'));
55-
$alias = $builder->getAlias('cmf_routing.route_provider');
56-
$this->assertEquals('cmf_routing.phpcr_route_provider', $alias->__toString());
57-
58-
$this->assertTrue($builder->hasAlias('cmf_routing.content_repository'));
59-
$alias = $builder->getAlias('cmf_routing.content_repository');
60-
$this->assertEquals('cmf_routing.phpcr_content_repository', $alias->__toString());
61-
62-
$this->assertTrue($builder->getParameter('cmf_routing.replace_symfony_router'));
63-
64-
$this->assertTrue($builder->hasDefinition('cmf_routing.router'));
65-
$methodCalls = $builder->getDefinition('cmf_routing.router')->getMethodCalls();
66-
$addMethodCalls = array_filter(
67-
$methodCalls,
68-
function ($call) {
69-
return 'add' == $call[0];
70-
}
71-
);
72-
73-
$this->assertCount(1, $addMethodCalls);
74-
$addMethodCall = reset($addMethodCalls);
75-
76-
$params = $addMethodCall[1];
77-
$this->assertCount(2, $params);
78-
79-
/** @var $reference \Symfony\Component\DependencyInjection\Reference */
80-
list($reference, $priority) = $params;
38+
),
39+
),
40+
));
8141

82-
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $reference);
83-
$this->assertEquals(100, $priority);
42+
$this->assertContainerBuilderHasAlias('cmf_routing.route_provider', 'cmf_routing.phpcr_route_provider');
43+
$this->assertContainerBuilderHasAlias('cmf_routing.content_repository', 'cmf_routing.phpcr_content_repository');
8444

85-
$this->assertEquals('router.default', $reference->__toString());
45+
$this->assertContainerBuilderHasParameter('cmf_routing.replace_symfony_router', true);
8646

47+
$this->assertContainerBuilderHasService('cmf_routing.router', '%cmf_routing.chain_router.class%');
48+
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall('cmf_routing.router', 'add', array(
49+
new Reference('router.default'),
50+
100,
51+
));
8752
}
8853

8954
public function testLoadConfigured()
9055
{
91-
$config = array(
92-
array(
93-
'dynamic' => array(
94-
'enabled' => true,
95-
'route_provider_service_id' => 'test_route_provider_service',
96-
'content_repository_service_id' => 'test_content_repository_service',
97-
'persistence' => array(
98-
'phpcr' => array(
99-
'use_sonata_admin' => false,
100-
),
56+
$this->load(array(
57+
'dynamic' => array(
58+
'enabled' => true,
59+
'route_provider_service_id' => 'test_route_provider_service',
60+
'content_repository_service_id' => 'test_content_repository_service',
61+
'persistence' => array(
62+
'phpcr' => array(
63+
'use_sonata_admin' => false,
10164
),
10265
),
103-
'chain' => array(
104-
'routers_by_id' => $providedRouters = array(
105-
'router.custom' => 200,
106-
'router.default' => 300
107-
)
66+
),
67+
'chain' => array(
68+
'routers_by_id' => array(
69+
'router.custom' => 200,
70+
'router.default' => 300
10871
)
10972
)
110-
);
111-
112-
$builder = $this->getBuilder($config);
113-
114-
$this->assertTrue($builder->hasAlias('cmf_routing.route_provider'));
115-
$alias = $builder->getAlias('cmf_routing.route_provider');
116-
$this->assertEquals('test_route_provider_service', $alias->__toString());
117-
118-
$this->assertTrue($builder->hasAlias('cmf_routing.content_repository'));
119-
$alias = $builder->getAlias('cmf_routing.content_repository');
120-
$this->assertEquals('test_content_repository_service', $alias->__toString());
121-
122-
$this->assertTrue($builder->hasDefinition('cmf_routing.router'));
123-
$methodCalls = $builder->getDefinition('cmf_routing.router')->getMethodCalls();
124-
$addMethodCalls = array_filter(
125-
$methodCalls,
126-
function ($call) {
127-
return 'add' == $call[0];
128-
}
129-
);
130-
131-
$this->assertCount(2, $addMethodCalls);
132-
133-
$routersAdded = array();
134-
135-
foreach ($addMethodCalls as $addMethodCall) {
136-
$params = $addMethodCall[1];
137-
$this->assertCount(2, $params);
138-
/** @var $reference \Symfony\Component\DependencyInjection\Reference */
139-
list($reference, $priority) = $params;
140-
141-
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $reference);
142-
143-
$routersAdded[$reference->__toString()] = $priority;
144-
}
145-
146-
$this->assertEquals($providedRouters, $routersAdded);
73+
));
74+
75+
$this->assertContainerBuilderHasAlias('cmf_routing.route_provider', 'test_route_provider_service');
76+
$this->assertContainerBuilderHasAlias('cmf_routing.content_repository', 'test_content_repository_service');
77+
78+
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall('cmf_routing.router', 'add', array(
79+
new Reference('router.custom'),
80+
200,
81+
));
82+
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall('cmf_routing.router', 'add', array(
83+
new Reference('router.default'),
84+
300,
85+
));
14786
}
14887

14988
public function testWhitespaceInPriorities()
15089
{
151-
$config = array(
152-
array(
153-
'dynamic' => array(
154-
'route_provider_service_id' => 'test_route_provider_service',
155-
'enabled' => true,
156-
'controllers_by_type' => array(
157-
'Acme\Foo' => '
158-
acme_main.controller:indexAction
159-
'
160-
),
90+
$this->load(array(
91+
'dynamic' => array(
92+
'route_provider_service_id' => 'test_route_provider_service',
93+
'enabled' => true,
94+
'controllers_by_type' => array(
95+
'Acme\Foo' => '
96+
acme_main.controller:indexAction
97+
'
16198
),
162-
'chain' => array(
163-
'routers_by_id' => array(
164-
'acme_test.router' => '
165-
100
166-
',
167-
),
99+
),
100+
'chain' => array(
101+
'routers_by_id' => array(
102+
'acme_test.router' => '
103+
100
104+
',
168105
),
169-
)
170-
);
171-
172-
$builder = $this->getBuilder($config);
173-
174-
$methodCalls = $builder->getDefinition('cmf_routing.router')->getMethodCalls();
175-
$addMethodCalls = array_filter(
176-
$methodCalls,
177-
function ($call) {
178-
return 'add' == $call[0];
179-
}
180-
);
181-
182-
$this->assertCount(1, $addMethodCalls);
183-
184-
$methodCall = current($addMethodCalls);
106+
),
107+
));
185108

186-
$this->assertSame('100', $methodCall[1][1]);
109+
$this->assertContainerBuilderHasServiceDefinitionWithMethodCall('cmf_routing.router', 'add', array(
110+
new Reference('acme_test.router'),
111+
100
112+
));
187113

188-
$controllers = $builder->getParameter('cmf_routing.controllers_by_type');
189-
$this->assertSame('acme_main.controller:indexAction', $controllers['Acme\Foo']);
114+
$this->assertContainerBuilderHasParameter('cmf_routing.controllers_by_type', array(
115+
'Acme\Foo' => 'acme_main.controller:indexAction',
116+
));
190117
}
191118
}

0 commit comments

Comments
 (0)