Add Helper implementation.
This commit is contained in:
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Mustache.php.
|
||||
*
|
||||
* (c) 2012 Justin Hileman
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Mustache\Test;
|
||||
|
||||
use Mustache\HelperCollection;
|
||||
|
||||
class HelperCollectionTest extends \PHPUnit_Framework_TestCase {
|
||||
public function testConstructor() {
|
||||
$foo = function() { echo 'foo'; };
|
||||
$bar = 'BAR';
|
||||
|
||||
$helpers = new HelperCollection(array(
|
||||
'foo' => $foo,
|
||||
'bar' => $bar,
|
||||
));
|
||||
|
||||
$this->assertSame($foo, $helpers->get('foo'));
|
||||
$this->assertSame($bar, $helpers->get('bar'));
|
||||
}
|
||||
|
||||
public function testAccessorsAndMutators() {
|
||||
$foo = function() { echo 'foo'; };
|
||||
$bar = 'BAR';
|
||||
|
||||
$helpers = new HelperCollection;
|
||||
$this->assertTrue($helpers->isEmpty());
|
||||
$this->assertFalse($helpers->has('foo'));
|
||||
$this->assertFalse($helpers->has('bar'));
|
||||
|
||||
$helpers->add('foo', $foo);
|
||||
$this->assertFalse($helpers->isEmpty());
|
||||
$this->assertTrue($helpers->has('foo'));
|
||||
$this->assertFalse($helpers->has('bar'));
|
||||
|
||||
$helpers->add('bar', $bar);
|
||||
$this->assertFalse($helpers->isEmpty());
|
||||
$this->assertTrue($helpers->has('foo'));
|
||||
$this->assertTrue($helpers->has('bar'));
|
||||
|
||||
$helpers->remove('foo');
|
||||
$this->assertFalse($helpers->isEmpty());
|
||||
$this->assertFalse($helpers->has('foo'));
|
||||
$this->assertTrue($helpers->has('bar'));
|
||||
}
|
||||
|
||||
public function testMagicMethods() {
|
||||
$foo = function() { echo 'foo'; };
|
||||
$bar = 'BAR';
|
||||
|
||||
$helpers = new HelperCollection;
|
||||
$this->assertTrue($helpers->isEmpty());
|
||||
$this->assertFalse($helpers->has('foo'));
|
||||
$this->assertFalse($helpers->has('bar'));
|
||||
$this->assertFalse(isset($helpers->foo));
|
||||
$this->assertFalse(isset($helpers->bar));
|
||||
|
||||
$helpers->foo = $foo;
|
||||
$this->assertFalse($helpers->isEmpty());
|
||||
$this->assertTrue($helpers->has('foo'));
|
||||
$this->assertFalse($helpers->has('bar'));
|
||||
$this->assertTrue(isset($helpers->foo));
|
||||
$this->assertFalse(isset($helpers->bar));
|
||||
|
||||
$helpers->bar = $bar;
|
||||
$this->assertFalse($helpers->isEmpty());
|
||||
$this->assertTrue($helpers->has('foo'));
|
||||
$this->assertTrue($helpers->has('bar'));
|
||||
$this->assertTrue(isset($helpers->foo));
|
||||
$this->assertTrue(isset($helpers->bar));
|
||||
|
||||
unset($helpers->foo);
|
||||
$this->assertFalse($helpers->isEmpty());
|
||||
$this->assertFalse($helpers->has('foo'));
|
||||
$this->assertTrue($helpers->has('bar'));
|
||||
$this->assertFalse(isset($helpers->foo));
|
||||
$this->assertTrue(isset($helpers->bar));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getInvalidHelperArguments
|
||||
*/
|
||||
public function testHelperCollectionIsntAfraidToThrowExceptions($helpers = array(), $actions = array(), $exception = null) {
|
||||
if ($exception) {
|
||||
$this->setExpectedException($exception);
|
||||
}
|
||||
|
||||
$helpers = new HelperCollection($helpers);
|
||||
|
||||
foreach ($actions as $method => $args) {
|
||||
call_user_func_array(array($helpers, $method), $args);
|
||||
}
|
||||
}
|
||||
|
||||
public function getInvalidHelperArguments() {
|
||||
return array(
|
||||
array(
|
||||
'not helpers',
|
||||
array(),
|
||||
'\InvalidArgumentException',
|
||||
),
|
||||
array(
|
||||
array(),
|
||||
array('get' => array('foo')),
|
||||
'\InvalidArgumentException',
|
||||
),
|
||||
array(
|
||||
array('foo' => 'FOO'),
|
||||
array('get' => array('foo')),
|
||||
null,
|
||||
),
|
||||
array(
|
||||
array('foo' => 'FOO'),
|
||||
array('get' => array('bar')),
|
||||
'\InvalidArgumentException',
|
||||
),
|
||||
array(
|
||||
array('foo' => 'FOO'),
|
||||
array(
|
||||
'add' => array('bar', 'BAR'),
|
||||
'get' => array('bar'),
|
||||
),
|
||||
null,
|
||||
),
|
||||
array(
|
||||
array('foo' => 'FOO'),
|
||||
array(
|
||||
'get' => array('foo'),
|
||||
'remove' => array('foo'),
|
||||
),
|
||||
null,
|
||||
),
|
||||
array(
|
||||
array('foo' => 'FOO'),
|
||||
array(
|
||||
'remove' => array('foo'),
|
||||
'get' => array('foo'),
|
||||
),
|
||||
'\InvalidArgumentException',
|
||||
),
|
||||
array(
|
||||
array(),
|
||||
array('remove' => array('foo')),
|
||||
'\InvalidArgumentException',
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,10 @@ class MustacheTest extends \PHPUnit_Framework_TestCase {
|
||||
'partials' => array(
|
||||
'foo' => '{{ foo }}',
|
||||
),
|
||||
'helpers' => array(
|
||||
'foo' => function() { return 'foo'; },
|
||||
'bar' => 'BAR',
|
||||
),
|
||||
'charset' => 'ISO-8859-1',
|
||||
));
|
||||
|
||||
@@ -51,6 +55,9 @@ class MustacheTest extends \PHPUnit_Framework_TestCase {
|
||||
$this->assertEquals('{{ foo }}', $partialsLoader->load('foo'));
|
||||
$this->assertContains('__whot__', $mustache->getTemplateClassName('{{ foo }}'));
|
||||
$this->assertEquals('ISO-8859-1', $mustache->getCharset());
|
||||
$this->assertTrue($mustache->hasHelper('foo'));
|
||||
$this->assertTrue($mustache->hasHelper('bar'));
|
||||
$this->assertFalse($mustache->hasHelper('baz'));
|
||||
}
|
||||
|
||||
public function testRender() {
|
||||
@@ -144,6 +151,49 @@ class MustacheTest extends \PHPUnit_Framework_TestCase {
|
||||
$mustache->setPartials(array('foo' => '{{ foo }}'));
|
||||
}
|
||||
|
||||
public function testHelpers() {
|
||||
$foo = function() { return 'foo'; };
|
||||
$bar = 'BAR';
|
||||
$mustache = new Mustache(array('helpers' => array(
|
||||
'foo' => $foo,
|
||||
'bar' => $bar,
|
||||
)));
|
||||
|
||||
$helpers = $mustache->getHelpers();
|
||||
$this->assertTrue($mustache->hasHelper('foo'));
|
||||
$this->assertTrue($mustache->hasHelper('bar'));
|
||||
$this->assertTrue($helpers->has('foo'));
|
||||
$this->assertTrue($helpers->has('bar'));
|
||||
$this->assertSame($foo, $mustache->getHelper('foo'));
|
||||
$this->assertSame($bar, $mustache->getHelper('bar'));
|
||||
|
||||
$mustache->removeHelper('bar');
|
||||
$this->assertFalse($mustache->hasHelper('bar'));
|
||||
$mustache->addHelper('bar', $bar);
|
||||
$this->assertSame($bar, $mustache->getHelper('bar'));
|
||||
|
||||
$baz = function($text) { return '__'.$text.'__'; };
|
||||
$this->assertFalse($mustache->hasHelper('baz'));
|
||||
$this->assertFalse($helpers->has('baz'));
|
||||
|
||||
$mustache->addHelper('baz', $baz);
|
||||
$this->assertTrue($mustache->hasHelper('baz'));
|
||||
$this->assertTrue($helpers->has('baz'));
|
||||
|
||||
// ... and a functional test
|
||||
$tpl = $mustache->loadTemplate('{{foo}} - {{bar}} - {{#baz}}qux{{/baz}}');
|
||||
$this->assertEquals('foo - BAR - __qux__', $tpl->render());
|
||||
$this->assertEquals('foo - BAR - __qux__', $tpl->render(array('qux' => "won't mess things up")));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testSetHelpersThrowsExceptions() {
|
||||
$mustache = new Mustache;
|
||||
$mustache->setHelpers('monkeymonkeymonkey');
|
||||
}
|
||||
|
||||
private static function rmdir($path) {
|
||||
$path = rtrim($path, '/').'/';
|
||||
$handle = opendir($path);
|
||||
|
||||
Reference in New Issue
Block a user