Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added get_option helper in twig when available
- Loading branch information
Xavier Coureau
committed
Jun 28, 2014
1 parent
2d93923
commit 1b4b8bf
Showing
5 changed files
with
157 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<parameters> | ||
<parameter key="ekino.wordpress.twig.extension.option.class">Ekino\WordpressBundle\Twig\Extension\OptionExtension</parameter> | ||
</parameters> | ||
|
||
<services> | ||
<service id="ekino.wordpress.twig.extension.option" class="%ekino.wordpress.twig.extension.option.class%"> | ||
<tag name="twig.extension" /> | ||
|
||
<argument type="service" id="ekino.wordpress.manager.option" /> | ||
</service> | ||
|
||
</services> | ||
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Ekino Wordpress package. | ||
* | ||
* (c) 2013 Ekino | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Ekino\WordpressBundle\Tests\Twig\Extension; | ||
|
||
use Ekino\WordpressBundle\Twig\Extension\OptionExtension; | ||
|
||
/** | ||
* Class OptionExtensionTest | ||
* | ||
* @author Xavier Coureau <xav.is@2cool4school.fr> | ||
*/ | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Ekino\WordpressBundle\Twig\Extension; | ||
|
||
use Ekino\WordpressBundle\Manager\OptionManager; | ||
|
||
class OptionExtension extends \Twig_Extension | ||
{ | ||
/** | ||
* @var OptionManager | ||
*/ | ||
protected $optionManager; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName() | ||
{ | ||
return 'ekino_wordpress_option'; | ||
} | ||
|
||
/** | ||
* @param OptionManager $optionManager | ||
*/ | ||
public function __contruct(OptionManager $optionManager) | ||
{ | ||
$this->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; | ||
} | ||
} |