Skip to content

Commit aecb007

Browse files
committed
fix FrameworkExtension tests
1 parent fba8f64 commit aecb007

File tree

9 files changed

+98
-106
lines changed

9 files changed

+98
-106
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/CachePoolPass.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\DefinitionDecorator;
17-
use Symfony\Component\DependencyInjection\Reference;
1817

1918
/**
2019
* @author Nicolas Grekas <p@tchwork.com>
@@ -28,7 +27,6 @@ public function process(ContainerBuilder $container)
2827
{
2928
foreach ($container->findTaggedServiceIds('cache.pool') as $id => $tags) {
3029
$pool = $container->getDefinition($id);
31-
$namespaceArgIndex = isset($tags[0]['namespace_arg_index']) ? $tags[0]['namespace_arg_index'] : -1;
3230

3331
if (!$pool instanceof DefinitionDecorator) {
3432
throw new \InvalidArgumentException(sprintf('Services tagged with "cache.pool" must have a parent service but "%s" has none.', $id));
@@ -39,7 +37,11 @@ public function process(ContainerBuilder $container)
3937
do {
4038
$adapterId = $adapter->getParent();
4139
$adapter = $container->getDefinition($adapterId);
42-
} while ($adapter instanceof DefinitionDecorator && !$adapter->getTag('cache.adapter'));
40+
} while ($adapter instanceof DefinitionDecorator && !$adapter->hasTag('cache.adapter'));
41+
42+
if (!$adapter->hasTag('cache.adapter')) {
43+
throw new \InvalidArgumentException(sprintf('Services tagged with "cache.pool" must have a parent service tagged with "cache.adapter" but "%s" has none.', $id));
44+
}
4345

4446
$tags = $adapter->getTag('cache.adapter');
4547

@@ -51,7 +53,7 @@ public function process(ContainerBuilder $container)
5153
throw new \InvalidArgumentException(sprintf('Services tagged as "cache.adapter" must be abstract: "%s" is not.', $adapterId));
5254
}
5355

54-
if (0 <= $namespaceArgIndex) {
56+
if (0 <= $namespaceArgIndex = $tags[0]['namespace_arg_index']) {
5557
$pool->replaceArgument($namespaceArgIndex, $this->getNamespace($id));
5658
}
5759
}

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ private function addCacheSection(ArrayNodeDefinition $rootNode)
557557
->info('Cache configuration')
558558
->fixXmlConfig('pool')
559559
->children()
560-
->arrayNode('pool')
560+
->arrayNode('pools')
561561
->useAttributeAsKey('name')
562562
->prototype('array')
563563
->children()

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public function load(array $configs, ContainerBuilder $container)
139139
}
140140

141141
if (isset($config['cache'])) {
142-
$this->registerCacheConfiguration($config['cache'], $container);
142+
$this->registerCacheConfiguration($config['cache'], $container, $loader);
143143
}
144144

145145
$loader->load('debug_prod.xml');
@@ -1023,11 +1023,11 @@ private function registerPropertyInfoConfiguration(array $config, ContainerBuild
10231023

10241024
private function registerCacheConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
10251025
{
1026-
if (!empty($config['pool'])) {
1026+
if (!empty($config['pools'])) {
10271027
$loader->load('cache_adapters.xml');
10281028
}
10291029

1030-
foreach ($config['pool'] as $name => $poolConfig) {
1030+
foreach ($config['pools'] as $name => $poolConfig) {
10311031
$poolDefinition = new DefinitionDecorator('cache.adapter.'.$poolConfig['type']);
10321032
$poolDefinition->replaceArgument(1, $poolConfig['default_lifetime']);
10331033

src/Symfony/Bundle/FrameworkBundle/Resources/config/cache_adapters.xml

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,27 @@
77
<services>
88

99
<service id="cache.adapter.apcu" class="Symfony\Component\Cache\Adapter\ApcuAdapter" abstract="true">
10-
<tag name="cache.adapter" namespace-arg-index="0"></tag>
10+
<tag name="cache.adapter" namespace-arg-index="0" />
1111
<argument /> <!-- namespace -->
1212
<argument /> <!-- default lifetime -->
1313
</service>
1414

1515
<service id="cache.adapter.doctrine" class="Symfony\Component\Cache\Adapter\DoctrineAdapter" abstract="true">
16-
<tag name="cache.adapter" namespace-arg-index="2"></tag>
16+
<tag name="cache.adapter" namespace-arg-index="2" />
1717
<argument /> <!-- doctrine provider service -->
1818
<argument /> <!-- default lifetime -->
1919
<argument /> <!-- namespace -->
2020
</service>
2121

2222
<service id="cache.adapter.psr6" class="Symfony\Component\Cache\Adapter\ProxyAdapter" abstract="true">
23-
<tag name="cache.adapter" namespace-arg-index="2"></tag>
23+
<tag name="cache.adapter" namespace-arg-index="2" />
2424
<argument /> <!-- PSR-6 provider service -->
2525
<argument /> <!-- default lifetime -->
2626
<argument /> <!-- namespace -->
2727
</service>
2828

2929
<service id="cache.adapter.filesystem" class="Symfony\Component\Cache\Adapter\FilesystemAdapter" abstract="true">
30-
<tag name="cache.adapter" namespace-arg-index="2"></tag>
30+
<tag name="cache.adapter" namespace-arg-index="2" />
3131
<argument>%kernel.cache_dir%</argument>
3232
<argument /> <!-- default lifetime -->
3333
<argument /> <!-- namespace -->

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/CachePoolPassTest.php

+56-57
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPass;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\Definition;
17-
use Symfony\Component\DependencyInjection\Reference;
17+
use Symfony\Component\DependencyInjection\DefinitionDecorator;
1818

1919
class CachePoolPassTest extends \PHPUnit_Framework_TestCase
2020
{
@@ -25,85 +25,84 @@ protected function setUp()
2525
$this->cachePoolPass = new CachePoolPass();
2626
}
2727

28-
public function testAdapterIsInjectedIntoConstructorArguments()
28+
public function testNamespaceArgumentIsReplaced()
2929
{
30-
$container = $this->initializeContainer();
30+
$container = new ContainerBuilder();
31+
$adapter = new Definition();
32+
$adapter->setAbstract(true);
33+
$adapter->addTag('cache.adapter', array('namespace_arg_index' => 0));
34+
$container->setDefinition('app.cache_adapter', $adapter);
35+
$cachePool = new DefinitionDecorator('app.cache_adapter');
36+
$cachePool->addArgument(null);
37+
$cachePool->addTag('cache.pool');
38+
$container->setDefinition('app.cache_pool', $cachePool);
39+
3140
$this->cachePoolPass->process($container);
32-
$adapter = $container->getDefinition('foo')->getArgument(0);
3341

34-
$this->assertInstanceOf('Symfony\Component\DependencyInjection\DefinitionDecorator', $adapter);
35-
$this->assertFalse($adapter->isAbstract());
36-
$this->assertSame('cache.adapter.apcu_adapter', $adapter->getParent());
37-
$this->assertSame('0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33', $adapter->getArgument(0));
42+
$this->assertSame('yRnzIIVLvL', $cachePool->getArgument(0));
3843
}
3944

40-
public function testAdapterIsInjectedIntoMethodArguments()
45+
/**
46+
* @expectedException \InvalidArgumentException
47+
* @expectedExceptionMessage Services tagged with "cache.pool" must have a parent service but "app.cache_pool" has none.
48+
*/
49+
public function testThrowsExceptionWhenCachePoolHasNoParentDefinition()
4150
{
42-
$container = $this->initializeContainer();
43-
$this->cachePoolPass->process($container);
44-
$methodCalls = $container->getDefinition('bar')->getMethodCalls();
45-
$arguments = $methodCalls[0][1];
46-
$adapter = $arguments[0];
51+
$container = new ContainerBuilder();
52+
$cachePool = new Definition();
53+
$cachePool->addTag('cache.pool');
54+
$container->setDefinition('app.cache_pool', $cachePool);
4755

48-
$this->assertInstanceOf('Symfony\Component\DependencyInjection\DefinitionDecorator', $adapter);
49-
$this->assertFalse($adapter->isAbstract());
50-
$this->assertSame('cache.adapter.doctrine_adapter', $adapter->getParent());
56+
$this->cachePoolPass->process($container);
5157
}
5258

53-
public function testAdapterIsInjectIntoProperties()
59+
/**
60+
* @expectedException \InvalidArgumentException
61+
* @expectedExceptionMessage Services tagged with "cache.pool" must have a parent service tagged with "cache.adapter" but "app.cache_pool" has none.
62+
*/
63+
public function testThrowsExceptionWhenCachePoolIsNotBasedOnAdapter()
5464
{
55-
$container = $this->initializeContainer();
56-
$this->cachePoolPass->process($container);
57-
$properties = $container->getDefinition('baz')->getProperties();
58-
$adapter = $properties['cache'];
65+
$container = new ContainerBuilder();
66+
$container->register('app.cache_adapter');
67+
$cachePool = new DefinitionDecorator('app.cache_adapter');
68+
$cachePool->addTag('cache.pool');
69+
$container->setDefinition('app.cache_pool', $cachePool);
5970

60-
$this->assertInstanceOf('Symfony\Component\DependencyInjection\DefinitionDecorator', $adapter);
61-
$this->assertFalse($adapter->isAbstract());
62-
$this->assertSame('cache.adapter.fs_adapter', $adapter->getParent());
71+
$this->cachePoolPass->process($container);
6372
}
6473

6574
/**
6675
* @expectedException \InvalidArgumentException
67-
* @expectedExceptionMessage The cache adapter "bar" is not configured
76+
* @expectedExceptionMessage Invalid "cache.adapter" tag for service "app.cache_adapter": attribute "namespace_arg_index" is missing.
6877
*/
69-
public function testThrowsExceptionWhenReferencedAdapterIsNotConfigured()
78+
public function testThrowsExceptionWhenCacheAdapterDefinesNoNamespaceArgument()
7079
{
7180
$container = new ContainerBuilder();
72-
$container->setDefinition('foo', new Definition('Foo', array(new Reference('cache.adapter.bar'))));
81+
$adapter = new Definition();
82+
$adapter->setAbstract(true);
83+
$adapter->addTag('cache.adapter');
84+
$container->setDefinition('app.cache_adapter', $adapter);
85+
$cachePool = new DefinitionDecorator('app.cache_adapter');
86+
$cachePool->addTag('cache.pool');
87+
$container->setDefinition('app.cache_pool', $cachePool);
88+
7389
$this->cachePoolPass->process($container);
7490
}
7591

76-
private function initializeContainer()
92+
/**
93+
* @expectedException \InvalidArgumentException
94+
* @expectedExceptionMessage Services tagged as "cache.adapter" must be abstract: "app.cache_adapter" is not.
95+
*/
96+
public function testThrowsExceptionWhenCacheAdapterIsNotAbstract()
7797
{
7898
$container = new ContainerBuilder();
99+
$adapter = new Definition();
100+
$adapter->addTag('cache.adapter', array('namespace_arg_index' => 0));
101+
$container->setDefinition('app.cache_adapter', $adapter);
102+
$cachePool = new DefinitionDecorator('app.cache_adapter');
103+
$cachePool->addTag('cache.pool');
104+
$container->setDefinition('app.cache_pool', $cachePool);
79105

80-
$apcuAdapter = new Definition('Symfony\Component\Cache\Adapter\ApcuAdapter');
81-
$apcuAdapter->setAbstract(true);
82-
$apcuAdapter->addTag('cache.adapter', array('id' => 'adapter1', 'namespace-arg-index' => 0));
83-
$container->setDefinition('cache.adapter.apcu_adapter', $apcuAdapter);
84-
85-
$doctrineAdapter = new Definition('Symfony\Component\Cache\Adapter\DoctrineAdapter');
86-
$doctrineAdapter->setAbstract(true);
87-
$doctrineAdapter->addTag('cache.adapter', array('id' => 'adapter2'));
88-
$container->setDefinition('cache.adapter.doctrine_adapter', $doctrineAdapter);
89-
90-
$filesystemAdapter = new Definition('Symfony\Component\Cache\Adapter\FilesystemAdapter');
91-
$filesystemAdapter->setAbstract(true);
92-
$filesystemAdapter->addTag('cache.adapter', array('id' => 'adapter3'));
93-
$container->setDefinition('cache.adapter.fs_adapter', $filesystemAdapter);
94-
95-
$foo = new Definition();
96-
$foo->setArguments(array(new Reference('cache.adapter.adapter1')));
97-
$container->setDefinition('foo', $foo);
98-
99-
$bar = new Definition();
100-
$bar->addMethodCall('setCache', array(new Reference('cache.adapter.adapter2')));
101-
$container->setDefinition('bar', $bar);
102-
103-
$baz = new Definition();
104-
$baz->setProperty('cache', new Reference('cache.adapter.adapter3'));
105-
$container->setDefinition('baz', $baz);
106-
107-
return $container;
106+
$this->cachePoolPass->process($container);
108107
}
109108
}

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/cache.php

+6-12
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,20 @@
22

33
$container->loadFromExtension('framework', array(
44
'cache' => array(
5-
'adapters' => array(
5+
'pools' => array(
66
'foo' => array(
77
'type' => 'apcu',
8-
'options' => array(
9-
'default_lifetime' => 30,
10-
),
8+
'default_lifetime' => 30,
119
),
1210
'bar' => array(
1311
'type' => 'doctrine',
14-
'options' => array(
15-
'default_lifetime' => 5,
16-
'cache_provider_service' => 'app.doctrine_cache_provider',
17-
),
12+
'default_lifetime' => 5,
13+
'cache_provider_service' => 'app.doctrine_cache_provider',
1814
),
1915
'baz' => array(
2016
'type' => 'filesystem',
21-
'options' => array(
22-
'default_lifetime' => 7,
23-
'directory' => 'app/cache/psr',
24-
),
17+
'default_lifetime' => 7,
18+
'directory' => 'app/cache/psr',
2519
),
2620
),
2721
),

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/cache.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
<framework:config>
99
<framework:cache>
10-
<framework:adapter name="foo" type="apcu" default-lifetime="30" />
11-
<framework:adapter name="bar" type="doctrine" default-lifetime="5" cache-provider-service="app.doctrine_cache_provider" />
12-
<framework:adapter name="baz" type="filesystem" default-lifetime="7" directory="app/cache/psr" />
10+
<framework:pool name="foo" type="apcu" default-lifetime="30" />
11+
<framework:pool name="bar" type="doctrine" default-lifetime="5" cache-provider-service="app.doctrine_cache_provider" />
12+
<framework:pool name="baz" type="filesystem" default-lifetime="7" directory="app/cache/psr" />
1313
</framework:cache>
1414
</framework:config>
1515
</container>
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
framework:
22
cache:
3-
adapters:
3+
pools:
44
foo:
55
type: apcu
6-
options:
7-
default_lifetime: 30
6+
default_lifetime: 30
87
bar:
98
type: doctrine
10-
options:
11-
default_lifetime: 5
12-
cache_provider_service: app.doctrine_cache_provider
9+
default_lifetime: 5
10+
cache_provider_service: app.doctrine_cache_provider
1311
baz:
1412
type: filesystem
15-
options:
16-
default_lifetime: 7
17-
directory: app/cache/psr
13+
default_lifetime: 7
14+
directory: app/cache/psr

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

+13-13
Original file line numberDiff line numberDiff line change
@@ -571,13 +571,13 @@ public function testPropertyInfoEnabled()
571571
$this->assertTrue($container->has('property_info'));
572572
}
573573

574-
public function testCacheAdaptersAbstractServices()
574+
public function testCachePoolServices()
575575
{
576576
$container = $this->createContainerFromFile('cache');
577577

578-
$this->assertCacheAdapterIsRegistered($container, 'foo', 'apcu', array(null, 30), 0);
579-
$this->assertCacheAdapterIsRegistered($container, 'bar', 'doctrine', array(new Reference('app.doctrine_cache_provider'), 5));
580-
$this->assertCacheAdapterIsRegistered($container, 'baz', 'filesystem', array('app/cache/psr', 7));
578+
$this->assertCachePoolServiceDefinitionIsCreated($container, 'foo', 'apcu', array('index_1' => 30), 0);
579+
$this->assertCachePoolServiceDefinitionIsCreated($container, 'bar', 'doctrine', array('index_0' => new Reference('app.doctrine_cache_provider'), 'index_1' => 5));
580+
$this->assertCachePoolServiceDefinitionIsCreated($container, 'baz', 'filesystem', array('index_0' => 'app/cache/psr', 'index_1' => 7));
581581
}
582582

583583
protected function createContainer(array $data = array())
@@ -649,13 +649,18 @@ private function assertVersionStrategy(ContainerBuilder $container, Reference $r
649649
}
650650
}
651651

652-
private function assertCacheAdapterIsRegistered(ContainerBuilder $container, $name, $type, array $arguments, $namespaceArgumentIndex = null)
652+
private function assertCachePoolServiceDefinitionIsCreated(ContainerBuilder $container, $name, $type, array $arguments, $namespaceArgumentIndex = null)
653653
{
654-
$id = 'cache.adapter.'.$name;
654+
$id = 'cache.pool.'.$name;
655655

656-
$this->assertTrue($container->has($id), sprintf('Service definition "%s" for cache adapter of type "%s" is registered', $id, $type));
656+
$this->assertTrue($container->has($id), sprintf('Service definition "%s" for cache pool of type "%s" is registered', $id, $type));
657657

658-
$adapterDefinition = $container->getDefinition($id);
658+
$poolDefinition = $container->getDefinition($id);
659+
660+
$this->assertInstanceOf(DefinitionDecorator::class, $poolDefinition, sprintf('Cache pool "%s" is based on an abstract cache adapter.', $name));
661+
$this->assertEquals($arguments, $poolDefinition->getArguments());
662+
663+
$adapterDefinition = $container->getDefinition($poolDefinition->getParent());
659664

660665
switch ($type) {
661666
case 'apcu':
@@ -669,15 +674,10 @@ private function assertCacheAdapterIsRegistered(ContainerBuilder $container, $na
669674
break;
670675
}
671676

672-
$this->assertTrue($adapterDefinition->isAbstract(), sprintf('Service definition "%s" for cache adapter "%s" is abstract', $id, $name));
673-
$this->assertEquals($arguments, $adapterDefinition->getArguments());
674677
$this->assertTrue($adapterDefinition->hasTag('cache.adapter'), sprintf('Service definition "%s" is tagged with the "cache.adapter" tag.', $id));
675678

676679
$tag = $adapterDefinition->getTag('cache.adapter');
677680

678-
$this->assertTrue(isset($tag[0]['id']), 'The adapter name is the "id" attribute of the "cache.adapter" tag.');
679-
$this->assertSame($name, $tag[0]['id'], 'The adapter name is the "id" attribute of the "cache.adapter" tag.');
680-
681681
if (null !== $namespaceArgumentIndex) {
682682
$this->assertTrue(isset($tag[0]['namespace-arg-index']), 'The namespace argument index is given by the "namespace-arg-index" attribute of the "cache.adapter" tag.');
683683
$this->assertSame($namespaceArgumentIndex, $tag[0]['namespace-arg-index'], 'The namespace argument index is given by the "namespace-arg-index" attribute of the "cache.adapter" tag.');

0 commit comments

Comments
 (0)