[FrameworkBundle] Fix and add tests for cache pool wiring

This commit is contained in:
Christian Flothmann 2016-03-11 09:48:42 +01:00 committed by Nicolas Grekas
parent e44bfdcde9
commit 92b1a20613
10 changed files with 163 additions and 162 deletions

View File

@ -14,7 +14,6 @@ namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Reference;
/**
* @author Nicolas Grekas <p@tchwork.com>
@ -28,7 +27,6 @@ class CachePoolPass implements CompilerPassInterface
{
foreach ($container->findTaggedServiceIds('cache.pool') as $id => $tags) {
$pool = $container->getDefinition($id);
$namespaceArgIndex = isset($tags[0]['namespace_arg_index']) ? $tags[0]['namespace_arg_index'] : -1;
if (!$pool instanceof DefinitionDecorator) {
throw new \InvalidArgumentException(sprintf('Services tagged with "cache.pool" must have a parent service but "%s" has none.', $id));
@ -39,7 +37,11 @@ class CachePoolPass implements CompilerPassInterface
do {
$adapterId = $adapter->getParent();
$adapter = $container->getDefinition($adapterId);
} while ($adapter instanceof DefinitionDecorator && !$adapter->getTag('cache.adapter'));
} while ($adapter instanceof DefinitionDecorator && !$adapter->hasTag('cache.adapter'));
if (!$adapter->hasTag('cache.adapter')) {
throw new \InvalidArgumentException(sprintf('Services tagged with "cache.pool" must have a parent service tagged with "cache.adapter" but "%s" has none.', $id));
}
$tags = $adapter->getTag('cache.adapter');
@ -51,7 +53,7 @@ class CachePoolPass implements CompilerPassInterface
throw new \InvalidArgumentException(sprintf('Services tagged as "cache.adapter" must be abstract: "%s" is not.', $adapterId));
}
if (0 <= $namespaceArgIndex) {
if (0 <= $namespaceArgIndex = $tags[0]['namespace_arg_index']) {
$pool->replaceArgument($namespaceArgIndex, $this->getNamespace($id));
}
}
@ -59,6 +61,6 @@ class CachePoolPass implements CompilerPassInterface
private function getNamespace($id)
{
return substr(str_replace('/', '-', base64_encode(md5('symfony.'.$id, true)), 0, 10));
return substr(str_replace('/', '-', base64_encode(md5('symfony.'.$id, true))), 0, 10);
}
}

View File

@ -557,18 +557,16 @@ class Configuration implements ConfigurationInterface
->info('Cache configuration')
->fixXmlConfig('pool')
->children()
->arrayNode('pool')
->arrayNode('pools')
->useAttributeAsKey('name')
->prototype('array')
->beforeNormalization()
->end()
->children()
->enumNode('type')
->info('The cache pool type (one of "apcu", "doctrine", "psr6" or "filesystem")')
->isRequired()
->values(array('apcu', 'doctrine', 'psr6', 'filesystem'))
->end()
->integerNode('default_lifetime')->default(0)->end()
->integerNode('default_lifetime')->defaultValue(0)->end()
->scalarNode('cache_provider_service')->defaultNull()->end()
->scalarNode('directory')->defaultNull()->end()
->end()

View File

@ -139,7 +139,7 @@ class FrameworkExtension extends Extension
}
if (isset($config['cache'])) {
$this->registerCacheConfiguration($config['cache'], $container);
$this->registerCacheConfiguration($config['cache'], $container, $loader);
}
$loader->load('debug_prod.xml');
@ -1022,11 +1022,11 @@ class FrameworkExtension extends Extension
private function registerCacheConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
{
if (!empty($config['pool'])) {
if (!empty($config['pools'])) {
$loader->load('cache_adapters.xml');
}
foreach ($config['pool'] as $name => $poolConfig) {
foreach ($config['pools'] as $name => $poolConfig) {
$poolDefinition = new DefinitionDecorator('cache.adapter.'.$poolConfig['type']);
$poolDefinition->replaceArgument(1, $poolConfig['default_lifetime']);

View File

@ -7,27 +7,27 @@
<services>
<service id="cache.adapter.apcu" class="Symfony\Component\Cache\Adapter\ApcuAdapter" abstract="true">
<tag name="cache.adapter" namespace-arg-index="0"></tag>
<tag name="cache.adapter" namespace-arg-index="0" />
<argument /> <!-- namespace -->
<argument /> <!-- default lifetime -->
</service>
<service id="cache.adapter.doctrine" class="Symfony\Component\Cache\Adapter\DoctrineAdapter" abstract="true">
<tag name="cache.adapter" namespace-arg-index="2"></tag>
<tag name="cache.adapter" namespace-arg-index="2" />
<argument /> <!-- doctrine provider service -->
<argument /> <!-- default lifetime -->
<argument /> <!-- namespace -->
</service>
<service id="cache.adapter.psr6" class="Symfony\Component\Cache\Adapter\ProxyAdapter" abstract="true">
<tag name="cache.adapter" namespace-arg-index="2"></tag>
<tag name="cache.adapter" namespace-arg-index="2" />
<argument /> <!-- PSR-6 provider service -->
<argument /> <!-- default lifetime -->
<argument /> <!-- namespace -->
</service>
<service id="cache.adapter.filesystem" class="Symfony\Component\Cache\Adapter\FilesystemAdapter" abstract="true">
<tag name="cache.adapter" namespace-arg-index="2"></tag>
<tag name="cache.adapter" namespace-arg-index="2" />
<argument>%kernel.cache_dir%</argument>
<argument /> <!-- default lifetime -->
<argument /> <!-- namespace -->

View File

@ -1,109 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
class CachePoolPassTest extends \PHPUnit_Framework_TestCase
{
private $cachePoolPass;
protected function setUp()
{
$this->cachePoolPass = new CachePoolPass();
}
public function testAdapterIsInjectedIntoConstructorArguments()
{
$container = $this->initializeContainer();
$this->cachePoolPass->process($container);
$adapter = $container->getDefinition('foo')->getArgument(0);
$this->assertInstanceOf('Symfony\Component\DependencyInjection\DefinitionDecorator', $adapter);
$this->assertFalse($adapter->isAbstract());
$this->assertSame('cache.adapter.apcu_adapter', $adapter->getParent());
$this->assertSame('0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33', $adapter->getArgument(0));
}
public function testAdapterIsInjectedIntoMethodArguments()
{
$container = $this->initializeContainer();
$this->cachePoolPass->process($container);
$methodCalls = $container->getDefinition('bar')->getMethodCalls();
$arguments = $methodCalls[0][1];
$adapter = $arguments[0];
$this->assertInstanceOf('Symfony\Component\DependencyInjection\DefinitionDecorator', $adapter);
$this->assertFalse($adapter->isAbstract());
$this->assertSame('cache.adapter.doctrine_adapter', $adapter->getParent());
}
public function testAdapterIsInjectIntoProperties()
{
$container = $this->initializeContainer();
$this->cachePoolPass->process($container);
$properties = $container->getDefinition('baz')->getProperties();
$adapter = $properties['cache'];
$this->assertInstanceOf('Symfony\Component\DependencyInjection\DefinitionDecorator', $adapter);
$this->assertFalse($adapter->isAbstract());
$this->assertSame('cache.adapter.fs_adapter', $adapter->getParent());
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The cache adapter "bar" is not configured
*/
public function testThrowsExceptionWhenReferencedAdapterIsNotConfigured()
{
$container = new ContainerBuilder();
$container->setDefinition('foo', new Definition('Foo', array(new Reference('cache.adapter.bar'))));
$this->cachePoolPass->process($container);
}
private function initializeContainer()
{
$container = new ContainerBuilder();
$apcuAdapter = new Definition('Symfony\Component\Cache\Adapter\ApcuAdapter');
$apcuAdapter->setAbstract(true);
$apcuAdapter->addTag('cache.adapter', array('id' => 'adapter1', 'namespace-arg-index' => 0));
$container->setDefinition('cache.adapter.apcu_adapter', $apcuAdapter);
$doctrineAdapter = new Definition('Symfony\Component\Cache\Adapter\DoctrineAdapter');
$doctrineAdapter->setAbstract(true);
$doctrineAdapter->addTag('cache.adapter', array('id' => 'adapter2'));
$container->setDefinition('cache.adapter.doctrine_adapter', $doctrineAdapter);
$filesystemAdapter = new Definition('Symfony\Component\Cache\Adapter\FilesystemAdapter');
$filesystemAdapter->setAbstract(true);
$filesystemAdapter->addTag('cache.adapter', array('id' => 'adapter3'));
$container->setDefinition('cache.adapter.fs_adapter', $filesystemAdapter);
$foo = new Definition();
$foo->setArguments(array(new Reference('cache.adapter.adapter1')));
$container->setDefinition('foo', $foo);
$bar = new Definition();
$bar->addMethodCall('setCache', array(new Reference('cache.adapter.adapter2')));
$container->setDefinition('bar', $bar);
$baz = new Definition();
$baz->setProperty('cache', new Reference('cache.adapter.adapter3'));
$container->setDefinition('baz', $baz);
return $container;
}
}

View File

@ -0,0 +1,108 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
class CachePoolPassTest extends \PHPUnit_Framework_TestCase
{
private $cachePoolPass;
protected function setUp()
{
$this->cachePoolPass = new CachePoolPass();
}
public function testNamespaceArgumentIsReplaced()
{
$container = new ContainerBuilder();
$adapter = new Definition();
$adapter->setAbstract(true);
$adapter->addTag('cache.adapter', array('namespace_arg_index' => 0));
$container->setDefinition('app.cache_adapter', $adapter);
$cachePool = new DefinitionDecorator('app.cache_adapter');
$cachePool->addArgument(null);
$cachePool->addTag('cache.pool');
$container->setDefinition('app.cache_pool', $cachePool);
$this->cachePoolPass->process($container);
$this->assertSame('yRnzIIVLvL', $cachePool->getArgument(0));
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Services tagged with "cache.pool" must have a parent service but "app.cache_pool" has none.
*/
public function testThrowsExceptionWhenCachePoolHasNoParentDefinition()
{
$container = new ContainerBuilder();
$cachePool = new Definition();
$cachePool->addTag('cache.pool');
$container->setDefinition('app.cache_pool', $cachePool);
$this->cachePoolPass->process($container);
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Services tagged with "cache.pool" must have a parent service tagged with "cache.adapter" but "app.cache_pool" has none.
*/
public function testThrowsExceptionWhenCachePoolIsNotBasedOnAdapter()
{
$container = new ContainerBuilder();
$container->register('app.cache_adapter');
$cachePool = new DefinitionDecorator('app.cache_adapter');
$cachePool->addTag('cache.pool');
$container->setDefinition('app.cache_pool', $cachePool);
$this->cachePoolPass->process($container);
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid "cache.adapter" tag for service "app.cache_adapter": attribute "namespace_arg_index" is missing.
*/
public function testThrowsExceptionWhenCacheAdapterDefinesNoNamespaceArgument()
{
$container = new ContainerBuilder();
$adapter = new Definition();
$adapter->setAbstract(true);
$adapter->addTag('cache.adapter');
$container->setDefinition('app.cache_adapter', $adapter);
$cachePool = new DefinitionDecorator('app.cache_adapter');
$cachePool->addTag('cache.pool');
$container->setDefinition('app.cache_pool', $cachePool);
$this->cachePoolPass->process($container);
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Services tagged as "cache.adapter" must be abstract: "app.cache_adapter" is not.
*/
public function testThrowsExceptionWhenCacheAdapterIsNotAbstract()
{
$container = new ContainerBuilder();
$adapter = new Definition();
$adapter->addTag('cache.adapter', array('namespace_arg_index' => 0));
$container->setDefinition('app.cache_adapter', $adapter);
$cachePool = new DefinitionDecorator('app.cache_adapter');
$cachePool->addTag('cache.pool');
$container->setDefinition('app.cache_pool', $cachePool);
$this->cachePoolPass->process($container);
}
}

View File

@ -2,26 +2,25 @@
$container->loadFromExtension('framework', array(
'cache' => array(
'adapters' => array(
'pools' => array(
'foo' => array(
'type' => 'apcu',
'options' => array(
'default_lifetime' => 30,
),
'default_lifetime' => 30,
),
'bar' => array(
'type' => 'doctrine',
'options' => array(
'default_lifetime' => 5,
'cache_provider_service' => 'app.doctrine_cache_provider',
),
'default_lifetime' => 5,
'cache_provider_service' => 'app.doctrine_cache_provider',
),
'baz' => array(
'type' => 'filesystem',
'options' => array(
'default_lifetime' => 7,
'directory' => 'app/cache/psr',
),
'default_lifetime' => 7,
'directory' => 'app/cache/psr',
),
'foobar' => array(
'type' => 'psr6',
'default_lifetime' => 10,
'cache_provider_service' => 'app.cache_pool',
),
),
),

View File

@ -7,9 +7,10 @@
<framework:config>
<framework:cache>
<framework:adapter name="foo" type="apcu" default-lifetime="30" />
<framework:adapter name="bar" type="doctrine" default-lifetime="5" cache-provider-service="app.doctrine_cache_provider" />
<framework:adapter name="baz" type="filesystem" default-lifetime="7" directory="app/cache/psr" />
<framework:pool name="foo" type="apcu" default-lifetime="30" />
<framework:pool name="bar" type="doctrine" default-lifetime="5" cache-provider-service="app.doctrine_cache_provider" />
<framework:pool name="baz" type="filesystem" default-lifetime="7" directory="app/cache/psr" />
<framework:pool name="foobar" type="psr6" default-lifetime="10" cache-provider-service="app.cache_pool" />
</framework:cache>
</framework:config>
</container>

View File

@ -1,17 +1,18 @@
framework:
cache:
adapters:
pools:
foo:
type: apcu
options:
default_lifetime: 30
default_lifetime: 30
bar:
type: doctrine
options:
default_lifetime: 5
cache_provider_service: app.doctrine_cache_provider
default_lifetime: 5
cache_provider_service: app.doctrine_cache_provider
baz:
type: filesystem
options:
default_lifetime: 7
directory: app/cache/psr
default_lifetime: 7
directory: app/cache/psr
foobar:
type: psr6
default_lifetime: 10
cache_provider_service: app.cache_pool

View File

@ -571,13 +571,14 @@ abstract class FrameworkExtensionTest extends TestCase
$this->assertTrue($container->has('property_info'));
}
public function testCacheAdaptersAbstractServices()
public function testCachePoolServices()
{
$container = $this->createContainerFromFile('cache');
$this->assertCacheAdapterIsRegistered($container, 'foo', 'apcu', array(null, 30), 0);
$this->assertCacheAdapterIsRegistered($container, 'bar', 'doctrine', array(new Reference('app.doctrine_cache_provider'), 5));
$this->assertCacheAdapterIsRegistered($container, 'baz', 'filesystem', array('app/cache/psr', 7));
$this->assertCachePoolServiceDefinitionIsCreated($container, 'foo', 'apcu', array('index_1' => 30), 0);
$this->assertCachePoolServiceDefinitionIsCreated($container, 'bar', 'doctrine', array('index_0' => new Reference('app.doctrine_cache_provider'), 'index_1' => 5));
$this->assertCachePoolServiceDefinitionIsCreated($container, 'baz', 'filesystem', array('index_0' => 'app/cache/psr', 'index_1' => 7));
$this->assertCachePoolServiceDefinitionIsCreated($container, 'foobar', 'psr6', array('index_0' => new Reference('app.cache_pool'), 'index_1' => 10));
}
protected function createContainer(array $data = array())
@ -649,13 +650,18 @@ abstract class FrameworkExtensionTest extends TestCase
}
}
private function assertCacheAdapterIsRegistered(ContainerBuilder $container, $name, $type, array $arguments, $namespaceArgumentIndex = null)
private function assertCachePoolServiceDefinitionIsCreated(ContainerBuilder $container, $name, $type, array $arguments, $namespaceArgumentIndex = null)
{
$id = 'cache.adapter.'.$name;
$id = 'cache.pool.'.$name;
$this->assertTrue($container->has($id), sprintf('Service definition "%s" for cache adapter of type "%s" is registered', $id, $type));
$this->assertTrue($container->has($id), sprintf('Service definition "%s" for cache pool of type "%s" is registered', $id, $type));
$adapterDefinition = $container->getDefinition($id);
$poolDefinition = $container->getDefinition($id);
$this->assertInstanceOf(DefinitionDecorator::class, $poolDefinition, sprintf('Cache pool "%s" is based on an abstract cache adapter.', $name));
$this->assertEquals($arguments, $poolDefinition->getArguments());
$adapterDefinition = $container->getDefinition($poolDefinition->getParent());
switch ($type) {
case 'apcu':
@ -669,15 +675,10 @@ abstract class FrameworkExtensionTest extends TestCase
break;
}
$this->assertTrue($adapterDefinition->isAbstract(), sprintf('Service definition "%s" for cache adapter "%s" is abstract', $id, $name));
$this->assertEquals($arguments, $adapterDefinition->getArguments());
$this->assertTrue($adapterDefinition->hasTag('cache.adapter'), sprintf('Service definition "%s" is tagged with the "cache.adapter" tag.', $id));
$tag = $adapterDefinition->getTag('cache.adapter');
$this->assertTrue(isset($tag[0]['id']), 'The adapter name is the "id" attribute of the "cache.adapter" tag.');
$this->assertSame($name, $tag[0]['id'], 'The adapter name is the "id" attribute of the "cache.adapter" tag.');
if (null !== $namespaceArgumentIndex) {
$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.');
$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.');