From 1b4b8bf486b0473427f6bcb7d648a58ff1f30d3a Mon Sep 17 00:00:00 2001 From: Xavier Coureau Date: Sat, 28 Jun 2014 13:43:43 +0200 Subject: [PATCH] Added get_option helper in twig when available --- .../EkinoWordpressExtension.php | 14 ++++ Manager/OptionManager.php | 2 - Resources/config/twig.xml | 19 +++++ Tests/Twig/Extension/OptionExtensionTest.php | 72 +++++++++++++++++++ Twig/Extension/OptionExtension.php | 52 ++++++++++++++ 5 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 Resources/config/twig.xml create mode 100644 Tests/Twig/Extension/OptionExtensionTest.php create mode 100644 Twig/Extension/OptionExtension.php diff --git a/DependencyInjection/EkinoWordpressExtension.php b/DependencyInjection/EkinoWordpressExtension.php index be12127..42980fe 100755 --- a/DependencyInjection/EkinoWordpressExtension.php +++ b/DependencyInjection/EkinoWordpressExtension.php @@ -51,6 +51,8 @@ public function load(array $configs, ContainerBuilder $container) if (isset($config['entity_manager'])) { $this->loadEntityManager($container, $config['entity_manager']); } + + $this->loadTwigExtensions($loader); } /** @@ -102,6 +104,18 @@ protected function loadEntityManager(ContainerBuilder $container, $em) $container->getDefinition('ekino.wordpress.manager.user_meta')->replaceArgument(0, $reference); } + /** + * Will load twig extensions if enabled + * + * @param XmlFileLoader $loader + */ + protected function loadTwigExtensions(XmlFileLoader $loader) + { + if (class_exists('\Twig_Extension')) { + $loader->load('twig.xml'); + } + } + /** * Returns bundle alias name * diff --git a/Manager/OptionManager.php b/Manager/OptionManager.php index 21b4daf..fb26623 100644 --- a/Manager/OptionManager.php +++ b/Manager/OptionManager.php @@ -10,8 +10,6 @@ namespace Ekino\WordpressBundle\Manager; -use Ekino\WordpressBundle\Manager\BaseManager; - /** * Class OptionManager * diff --git a/Resources/config/twig.xml b/Resources/config/twig.xml new file mode 100644 index 0000000..b66627e --- /dev/null +++ b/Resources/config/twig.xml @@ -0,0 +1,19 @@ + + + + + + Ekino\WordpressBundle\Twig\Extension\OptionExtension + + + + + + + + + + + diff --git a/Tests/Twig/Extension/OptionExtensionTest.php b/Tests/Twig/Extension/OptionExtensionTest.php new file mode 100644 index 0000000..f967285 --- /dev/null +++ b/Tests/Twig/Extension/OptionExtensionTest.php @@ -0,0 +1,72 @@ + + */ +class OptionExtensionTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $optionManager; + + /** + * @var OptionExtension + */ + protected $extension; + + protected function setUp() + { + if (!class_exists('\Twig_Extension')) { + $this->markTestSkipped('Twig is not enabled'); + } + + $this->optionManager = $this->getMockBuilder('Ekino\WordpressBundle\Manager\OptionManager')->disableOriginalConstructor()->getMock(); + $this->extension = new OptionExtension($this->optionManager); + } + + /** + * Check the correct result for an existing option + */ + public function testGetOption() + { + $optionMock = $this->getMock('Ekino\WordpressBundle\Entity\Option'); + + $this->optionManager->expects($this->once()) + ->method('findOneByOptionName') + ->with($this->equalTo('test')) + ->will($this->returnValue($optionMock)); + + $result = $this->extension->getOption('test'); + $this->assertEquals($optionMock, $result); + } + + /** + * Check the usage of default return value for a non existing option + */ + public function testGetOptionUndefined() + { + $this->optionManager->expects($this->once()) + ->method('findOneByOptionName') + ->with($this->equalTo('test')) + ->will($this->returnValue(null)); + + $result = $this->extension->getOption('test', 'poney'); + $this->assertEquals('poney', $result); + } +} \ No newline at end of file diff --git a/Twig/Extension/OptionExtension.php b/Twig/Extension/OptionExtension.php new file mode 100644 index 0000000..3698458 --- /dev/null +++ b/Twig/Extension/OptionExtension.php @@ -0,0 +1,52 @@ +optionManager = $optionManager; + } + + /** + * @return array + */ + public function getFunctions() + { + return array( + new \Twig_SimpleFunction('get_option', array($this, 'getOption')), + ); + } + + /** + * @param string $optionName + * @param mixed $defaultValue + * + * @return mixed + */ + public function getOption($optionName, $defaultValue = null) + { + $option = $this->optionManager->findOneByOptionName($optionName); + + return $option ?: $defaultValue; + } +} \ No newline at end of file