Permalink
Browse files

[DIC] Better handling of enableable configurations

  • Loading branch information...
vicb authored and fabpot committed Jan 23, 2013
1 parent e8bf115 commit ca5a57dd7d27c1435595050a06efa40db3c5674a
Showing with 110 additions and 9 deletions.
  1. +1 −0 CHANGELOG.md
  2. +28 −9 Extension/Extension.php
  3. +81 −0 Tests/Extension/ExtensionTest.php
View
@@ -4,6 +4,7 @@ CHANGELOG
2.2.0
-----
+ * added Extension::isConfigEnabled() to ease working with enableable configurations
* added an Extension base class with sensible defaults to be used in conjunction
with the Config component.
* added PrependExtensionInterface (to be able to allow extensions to prepend
View
@@ -12,6 +12,8 @@
namespace Symfony\Component\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Container;
+use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;
+use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\Definition\Processor;
@@ -62,26 +64,19 @@ public function getNamespace()
*
* @return string The alias
*
- * @throws \BadMethodCallException When the extension name does not follow conventions
+ * @throws BadMethodCallException When the extension name does not follow conventions
*/
public function getAlias()
{
$className = get_class($this);
if (substr($className, -9) != 'Extension') {
- throw new \BadMethodCallException('This extension does not follow the naming convention; you must overwrite the getAlias() method.');
+ throw new BadMethodCallException('This extension does not follow the naming convention; you must overwrite the getAlias() method.');
}
$classBaseName = substr(strrchr($className, '\\'), 1, -9);
return Container::underscore($classBaseName);
}
- final protected function processConfiguration(ConfigurationInterface $configuration, array $configs)
- {
- $processor = new Processor();
-
- return $processor->processConfiguration($configuration, $configs);
- }
-
/**
* {@inheritDoc}
*/
@@ -104,4 +99,28 @@ public function getConfiguration(array $config, ContainerBuilder $container)
return null;
}
+
+ final protected function processConfiguration(ConfigurationInterface $configuration, array $configs)
+ {
+ $processor = new Processor();
+
+ return $processor->processConfiguration($configuration, $configs);
+ }
+
+ /**
+ * @param ContainerBuilder $container
+ * @param array $config
+ *
+ * @return Boolean Whether the configuration is enabled
+ *
+ * @throws InvalidArgumentException When the config is not enableable
+ */
+ protected function isConfigEnabled(ContainerBuilder $container, array $config)
+ {
+ if (!array_key_exists('enabled', $config)) {
+ throw new InvalidArgumentException("The config array has no 'enabled' key.");
+ }
+
+ return (Boolean) $container->getParameterBag()->resolveValue($config['enabled']);
+ }
}
@@ -0,0 +1,81 @@
+<?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\Component\DependencyInjection\Tests\Extension;
+
+class ExtensionTest extends \PHPUnit_Framework_TestCase
+{
+ /**
+ * @dataProvider getResolvedEnabledFixtures
+ */
+ public function testIsConfigEnabledReturnsTheResolvedValue($enabled)
+ {
+ $pb = $this->getMockBuilder('Symfony\Component\DependencyInjection\ParameterBag\ParameterBag')
+ ->setMethods(array('resolveValue'))
+ ->getMock()
+ ;
+
+ $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')
+ ->setMethods(array('getParameterBag'))
+ ->getMock()
+ ;
+
+ $pb->expects($this->once())
+ ->method('resolveValue')
+ ->with($this->equalTo($enabled))
+ ->will($this->returnValue($enabled))
+ ;
+
+ $container->expects($this->once())
+ ->method('getParameterBag')
+ ->will($this->returnValue($pb))
+ ;
+
+ $extension = $this->getMockBuilder('Symfony\Component\DependencyInjection\Extension\Extension')
+ ->setMethods(array())
+ ->getMockForAbstractClass()
+ ;
+
+ $r = new \ReflectionMethod('Symfony\Component\DependencyInjection\Extension\Extension', 'isConfigEnabled');
+ $r->setAccessible(true);
+
+ $r->invoke($extension, $container, array('enabled' => $enabled));
+ }
+
+ public function getResolvedEnabledFixtures()
+ {
+ return array(
+ array(true),
+ array(false)
+ );
+ }
+
+ /**
+ * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
+ * @expectedExceptionMessage The config array has no 'enabled' key.
+ */
+ public function testIsConfigEnabledOnNonEnableableConfig()
+ {
+ $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')
+ ->getMock()
+ ;
+
+ $extension = $this->getMockBuilder('Symfony\Component\DependencyInjection\Extension\Extension')
+ ->setMethods(array())
+ ->getMockForAbstractClass()
+ ;
+
+ $r = new \ReflectionMethod('Symfony\Component\DependencyInjection\Extension\Extension', 'isConfigEnabled');
+ $r->setAccessible(true);
+
+ $r->invoke($extension, $container, array());
+ }
+}

0 comments on commit ca5a57d

Please sign in to comment.