2013-11-07 22:17:57 +04:00
|
|
|
<?php
|
2013-12-06 21:13:51 +04:00
|
|
|
/**
|
|
|
|
* This file is part of Handlebars-php
|
|
|
|
* Base on mustache-php https://github.com/bobthecow/mustache.php
|
|
|
|
*
|
|
|
|
* PHP version 5.3
|
|
|
|
*
|
|
|
|
* @category Xamin
|
|
|
|
* @package Handlebars
|
|
|
|
* @author fzerorubigd <fzerorubigd@gmail.com>
|
2014-01-23 13:59:13 +04:00
|
|
|
* @author Dmitriy Simushev <simushevds@gmail.com>
|
2013-12-06 21:13:51 +04:00
|
|
|
* @copyright 2013 (c) f0ruD A
|
|
|
|
* @license MIT <http://opensource.org/licenses/MIT>
|
|
|
|
* @version GIT: $Id$
|
|
|
|
* @link http://xamin.ir
|
|
|
|
*/
|
2013-11-07 22:17:57 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class AutoloaderTest
|
|
|
|
*/
|
|
|
|
class HandlebarsTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2013-12-06 22:02:36 +04:00
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
\Handlebars\Autoloader::register();
|
|
|
|
}
|
2014-02-26 21:43:11 +04:00
|
|
|
|
2013-11-07 22:17:57 +04:00
|
|
|
/**
|
|
|
|
* Test handlebars autoloader
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testAutoLoad()
|
|
|
|
{
|
|
|
|
Handlebars\Autoloader::register(realpath(__DIR__ . '/../fixture/'));
|
|
|
|
|
|
|
|
$this->assertTrue(class_exists('Handlebars\\Test'));
|
2013-12-06 22:02:36 +04:00
|
|
|
$this->assertTrue(class_exists('\\Handlebars\\Test'));
|
2013-11-07 22:17:57 +04:00
|
|
|
$this->assertTrue(class_exists('Handlebars\\Example\\Test'));
|
2013-12-06 22:02:36 +04:00
|
|
|
$this->assertTrue(class_exists('\\Handlebars\\Example\\Test'));
|
|
|
|
$this->assertFalse(class_exists('\\Another\\Example\\Test'));
|
2013-11-07 22:17:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test basic tags
|
|
|
|
*
|
|
|
|
* @param string $src handlebars source
|
|
|
|
* @param array $data data
|
|
|
|
* @param string $result expected data
|
|
|
|
*
|
|
|
|
* @dataProvider simpleTagdataProvider
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testBasicTags($src, $data, $result)
|
|
|
|
{
|
|
|
|
$loader = new \Handlebars\Loader\StringLoader();
|
|
|
|
$engine = new \Handlebars\Handlebars(array('loader' => $loader));
|
|
|
|
$this->assertEquals($result, $engine->render($src, $data));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple tag provider
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function simpleTagdataProvider()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(
|
|
|
|
'{{! This is comment}}',
|
|
|
|
array(),
|
|
|
|
''
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'{{data}}',
|
|
|
|
array('data' => 'result'),
|
|
|
|
'result'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'{{data.key}}',
|
|
|
|
array('data' => array('key' => 'result')),
|
|
|
|
'result'
|
|
|
|
),
|
2014-01-20 07:08:38 +04:00
|
|
|
array(
|
|
|
|
'{{data.length}}',
|
2014-02-26 21:43:11 +04:00
|
|
|
array("data" => array(1, 2, 3, 4)),
|
2014-01-20 07:08:38 +04:00
|
|
|
'4'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'{{data.length}}',
|
2014-02-26 21:43:11 +04:00
|
|
|
array("data" => (object)array(1, 2, 3, 4)),
|
2014-01-20 07:08:38 +04:00
|
|
|
''
|
2014-01-20 19:04:31 +04:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'{{data.length}}',
|
2014-02-26 21:43:11 +04:00
|
|
|
array("data" => array("length" => "15 inches", "test", "test", "test")),
|
2014-01-20 19:04:31 +04:00
|
|
|
"15 inches"
|
2014-01-20 19:57:16 +04:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'{{data.0}}',
|
2014-02-26 21:43:11 +04:00
|
|
|
array("data" => array(1, 2, 3, 4)),
|
2014-01-20 19:57:16 +04:00
|
|
|
'1'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'{{data.property.3}}',
|
2014-02-26 21:43:11 +04:00
|
|
|
array("data" => array("property" => array(1, 2, 3, 4))),
|
2014-01-20 19:57:16 +04:00
|
|
|
'4'
|
2014-02-05 18:03:56 +04:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'{{data.unsafe}}',
|
|
|
|
array('data' => array('unsafe' => '<strong>Test</strong>')),
|
|
|
|
'<strong>Test</strong>'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'{{{data.safe}}}',
|
|
|
|
array('data' => array('safe' => '<strong>Test</strong>')),
|
|
|
|
'<strong>Test</strong>'
|
|
|
|
),
|
2014-03-08 15:17:06 +04:00
|
|
|
array(
|
|
|
|
"\{{data}}", // is equal to \\{{data}}
|
|
|
|
array('data' => 'foo'),
|
|
|
|
'{{data}}',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'\\\\{{data}}',
|
|
|
|
array('data' => 'foo'),
|
|
|
|
'\\\\foo'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'\\\{{data}}', // is equal to \\\\{{data}} in php
|
|
|
|
array('data' => 'foo'),
|
|
|
|
'\\\\foo'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'\{{{data}}}',
|
|
|
|
array('data' => 'foo'),
|
|
|
|
'{{{data}}}'
|
|
|
|
),
|
2013-11-07 22:17:57 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test helpers (internal helpers)
|
|
|
|
*
|
|
|
|
* @param string $src handlebars source
|
|
|
|
* @param array $data data
|
|
|
|
* @param string $result expected data
|
|
|
|
*
|
|
|
|
* @dataProvider internalHelpersdataProvider
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testSimpleHelpers($src, $data, $result)
|
|
|
|
{
|
|
|
|
$loader = new \Handlebars\Loader\StringLoader();
|
|
|
|
$helpers = new \Handlebars\Helpers();
|
|
|
|
$engine = new \Handlebars\Handlebars(array('loader' => $loader, 'helpers' => $helpers));
|
|
|
|
|
|
|
|
$this->assertEquals($result, $engine->render($src, $data));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple helpers provider
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function internalHelpersdataProvider()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(
|
|
|
|
'{{#if data}}Yes{{/if}}',
|
|
|
|
array('data' => true),
|
|
|
|
'Yes'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'{{#if data}}Yes{{/if}}',
|
|
|
|
array('data' => false),
|
|
|
|
''
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'{{#with data}}{{key}}{{/with}}',
|
|
|
|
array('data' => array('key' => 'result')),
|
|
|
|
'result'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'{{#each data}}{{this}}{{/each}}',
|
2013-11-08 19:55:01 +04:00
|
|
|
array('data' => array(1, 2, 3, 4)),
|
2013-11-07 22:17:57 +04:00
|
|
|
'1234'
|
|
|
|
),
|
2013-12-06 19:47:27 +04:00
|
|
|
array(
|
|
|
|
'{{#each data}}{{@key}}=>{{this}}{{/each}}',
|
2014-02-26 21:43:11 +04:00
|
|
|
array('data' => array('key1' => 1, 'key2' => 2,)),
|
2013-12-06 19:47:27 +04:00
|
|
|
'key1=>1key2=>2'
|
|
|
|
),
|
2014-02-25 16:40:47 +04:00
|
|
|
array(
|
|
|
|
'{{#each data}}{{@key}}=>{{this}}{{/each}}',
|
2014-02-26 21:43:11 +04:00
|
|
|
array('data' => new \ArrayIterator(array('key1' => 1, 'key2' => 2))),
|
2014-02-25 16:40:47 +04:00
|
|
|
'key1=>1key2=>2'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'{{#each data}}{{@index}}=>{{this}},{{/each}}',
|
2014-02-26 21:43:11 +04:00
|
|
|
array('data' => array('key1' => 1, 'key2' => 2,)),
|
2014-02-25 16:40:47 +04:00
|
|
|
'0=>1,1=>2,'
|
|
|
|
),
|
2014-02-25 17:24:41 +04:00
|
|
|
array(
|
|
|
|
'{{#each data}}{{#if @first}}the first is {{this}}{{/if}}{{/each}}',
|
|
|
|
array('data' => array('one', 'two', 'three')),
|
|
|
|
'the first is one'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'{{#each data}}{{#if @last}}the last is {{this}}{{/if}}{{/each}}',
|
|
|
|
array('data' => array('one', 'two', 'three')),
|
|
|
|
'the last is three'
|
|
|
|
),
|
2014-01-23 15:34:52 +04:00
|
|
|
array(
|
|
|
|
'{{#each data}}{{this}}{{else}}fail{{/each}}',
|
|
|
|
array('data' => array(1, 2, 3, 4)),
|
|
|
|
'1234'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'{{#each data}}fail{{else}}ok{{/each}}',
|
|
|
|
array('data' => false),
|
|
|
|
'ok'
|
|
|
|
),
|
2013-11-22 00:05:30 +04:00
|
|
|
array(
|
|
|
|
'{{#unless data}}ok{{/unless}}',
|
|
|
|
array('data' => true),
|
|
|
|
''
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'{{#unless data}}ok{{/unless}}',
|
|
|
|
array('data' => false),
|
|
|
|
'ok'
|
2013-12-06 19:47:27 +04:00
|
|
|
),
|
2014-01-23 13:59:13 +04:00
|
|
|
array(
|
|
|
|
'{{#unless data}}ok{{else}}fail{{/unless}}',
|
|
|
|
array('data' => false),
|
|
|
|
'ok'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'{{#unless data}}fail{{else}}ok{{/unless}}',
|
|
|
|
array('data' => true),
|
|
|
|
'ok'
|
|
|
|
),
|
2013-12-06 19:47:27 +04:00
|
|
|
array(
|
|
|
|
'{{#bindAttr data}}',
|
|
|
|
array(),
|
|
|
|
'data'
|
2014-03-12 19:59:54 +04:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'{{#if 1}}ok{{else}}fail{{/if}}',
|
|
|
|
array(),
|
|
|
|
'ok'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'{{#if 0}}ok{{else}}fail{{/if}}',
|
|
|
|
array(),
|
|
|
|
'fail'
|
2013-11-22 00:05:30 +04:00
|
|
|
)
|
|
|
|
|
2013-11-07 22:17:57 +04:00
|
|
|
);
|
|
|
|
}
|
2013-11-08 19:30:54 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Management helpers
|
|
|
|
*/
|
|
|
|
public function testHelpersManagement()
|
|
|
|
{
|
2013-11-08 19:55:01 +04:00
|
|
|
$helpers = new \Handlebars\Helpers(array('test' => function () {
|
2014-02-26 21:43:11 +04:00
|
|
|
}), false);
|
2013-11-08 19:55:01 +04:00
|
|
|
$engine = new \Handlebars\Handlebars(array('helpers' => $helpers));
|
2013-11-08 19:30:54 +04:00
|
|
|
$this->assertTrue(is_callable($engine->getHelper('test')));
|
|
|
|
$this->assertTrue($engine->hasHelper('test'));
|
|
|
|
$engine->removeHelper('test');
|
|
|
|
$this->assertFalse($engine->hasHelper('test'));
|
|
|
|
}
|
2013-11-08 19:55:01 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Custom helper test
|
|
|
|
*/
|
|
|
|
public function testCustomHelper()
|
|
|
|
{
|
|
|
|
$loader = new \Handlebars\Loader\StringLoader();
|
|
|
|
$engine = new \Handlebars\Handlebars(array('loader' => $loader));
|
|
|
|
$engine->addHelper('test', function () {
|
|
|
|
return 'Test helper is called';
|
|
|
|
});
|
|
|
|
$this->assertEquals('Test helper is called', $engine->render('{{#test}}', array()));
|
2013-12-06 18:27:44 +04:00
|
|
|
$this->assertEquals('Test helper is called', $engine->render('{{test}}', array()));
|
|
|
|
|
|
|
|
$engine->addHelper('test2', function ($template, $context, $arg) {
|
|
|
|
return 'Test helper is called with ' . $arg;
|
|
|
|
});
|
|
|
|
$this->assertEquals('Test helper is called with a b c', $engine->render('{{#test2 a b c}}', array()));
|
|
|
|
$this->assertEquals('Test helper is called with a b c', $engine->render('{{test2 a b c}}', array()));
|
|
|
|
|
2014-02-26 21:43:11 +04:00
|
|
|
$engine->addHelper('renderme', function () {
|
|
|
|
return new \Handlebars\String("{{test}}");
|
|
|
|
});
|
2013-12-06 19:47:27 +04:00
|
|
|
$this->assertEquals('Test helper is called', $engine->render('{{#renderme}}', array()));
|
|
|
|
|
2014-02-26 21:43:11 +04:00
|
|
|
$engine->addHelper('dontrenderme', function () {
|
|
|
|
return "{{test}}";
|
|
|
|
});
|
2013-12-06 19:47:27 +04:00
|
|
|
$this->assertEquals('{{test}}', $engine->render('{{#dontrenderme}}', array()));
|
2014-01-31 19:12:44 +04:00
|
|
|
|
2014-02-26 21:43:11 +04:00
|
|
|
$engine->addHelper('markupHelper', function () {
|
2014-01-31 19:12:44 +04:00
|
|
|
return '<strong>Test</strong>';
|
|
|
|
});
|
|
|
|
$this->assertEquals('<strong>Test</strong>', $engine->render('{{{markupHelper}}}', array()));
|
|
|
|
$this->assertEquals('<strong>Test</strong>', $engine->render('{{markupHelper}}', array()));
|
2014-02-03 12:35:17 +04:00
|
|
|
|
2014-02-26 21:43:11 +04:00
|
|
|
$engine->addHelper('safeStringTest', function () {
|
2014-02-03 12:35:17 +04:00
|
|
|
return new \Handlebars\SafeString('<strong>Test</strong>');
|
|
|
|
});
|
|
|
|
$this->assertEquals('<strong>Test</strong>', $engine->render('{{safeStringTest}}', array()));
|
2013-12-06 19:47:27 +04:00
|
|
|
}
|
|
|
|
|
2014-02-26 20:56:51 +04:00
|
|
|
public function testInvalidHelperMustacheStyle()
|
|
|
|
{
|
|
|
|
$this->setExpectedException('RuntimeException');
|
|
|
|
$loader = new \Handlebars\Loader\StringLoader();
|
|
|
|
$engine = new \Handlebars\Handlebars(array('loader' => $loader));
|
|
|
|
$engine->render('{{#NOTVALID}}XXX{{/NOTVALID}}', array());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidHelper()
|
|
|
|
{
|
|
|
|
$this->setExpectedException('RuntimeException');
|
|
|
|
$loader = new \Handlebars\Loader\StringLoader();
|
|
|
|
$engine = new \Handlebars\Handlebars(array('loader' => $loader));
|
|
|
|
$engine->render('{{#NOTVALID argument}}XXX{{/NOTVALID}}', array());
|
|
|
|
}
|
|
|
|
|
2013-12-06 19:47:27 +04:00
|
|
|
/**
|
|
|
|
* Test mustache style loop and if
|
|
|
|
*/
|
|
|
|
public function testMustacheStyle()
|
|
|
|
{
|
|
|
|
$loader = new \Handlebars\Loader\StringLoader();
|
|
|
|
$engine = new \Handlebars\Handlebars(array('loader' => $loader));
|
2014-02-26 21:43:11 +04:00
|
|
|
$this->assertEquals('yes', $engine->render('{{#x}}yes{{/x}}', array('x' => true)));
|
|
|
|
$this->assertEquals('', $engine->render('{{#x}}yes{{/x}}', array('x' => false)));
|
|
|
|
$this->assertEquals('yes', $engine->render('{{^x}}yes{{/x}}', array('x' => false)));
|
|
|
|
$this->assertEquals('1234', $engine->render('{{#x}}{{this}}{{/x}}', array('x' => array(1, 2, 3, 4))));
|
|
|
|
$this->assertEquals('012', $engine->render('{{#x}}{{@index}}{{/x}}', array('x' => array('a', 'b', 'c'))));
|
|
|
|
$this->assertEquals('abc', $engine->render('{{#x}}{{@key}}{{/x}}', array('x' => array('a' => 1, 'b' => 2, 'c' => 3))));
|
2013-12-06 19:47:27 +04:00
|
|
|
$std = new stdClass();
|
|
|
|
$std->value = 1;
|
2014-02-26 21:43:11 +04:00
|
|
|
$this->assertEquals('1', $engine->render('{{#x}}{{value}}{{/x}}', array('x' => $std)));
|
|
|
|
$this->assertEquals('1', $engine->render('{{{x}}}', array('x' => 1)));
|
2013-12-06 19:47:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \LogicException
|
|
|
|
*/
|
|
|
|
public function testParserException()
|
|
|
|
{
|
|
|
|
$loader = new \Handlebars\Loader\StringLoader();
|
|
|
|
$engine = new \Handlebars\Handlebars(array('loader' => $loader));
|
|
|
|
$engine->render('{{#test}}{{#test2}}{{/test}}{{/test2}}', array());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test add/get/has/clear functions on helper class
|
|
|
|
*/
|
|
|
|
public function testHelpersClass()
|
|
|
|
{
|
|
|
|
$helpers = new \Handlebars\Helpers();
|
2014-02-26 21:43:11 +04:00
|
|
|
$helpers->add('test', function () {
|
|
|
|
});
|
2013-12-06 19:47:27 +04:00
|
|
|
$this->assertTrue($helpers->has('test'));
|
|
|
|
$this->assertTrue(isset($helpers->test));
|
|
|
|
$this->assertFalse($helpers->isEmpty());
|
2014-02-26 21:43:11 +04:00
|
|
|
$helpers->test2 = function () {
|
|
|
|
};
|
2013-12-06 19:47:27 +04:00
|
|
|
$this->assertTrue($helpers->has('test2'));
|
|
|
|
$this->assertTrue(isset($helpers->test2));
|
|
|
|
$this->assertFalse($helpers->isEmpty());
|
|
|
|
unset($helpers->test2);
|
|
|
|
$this->assertFalse($helpers->has('test2'));
|
|
|
|
$this->assertFalse(isset($helpers->test2));
|
|
|
|
$helpers->clear();
|
|
|
|
$this->assertFalse($helpers->has('test'));
|
|
|
|
$this->assertFalse(isset($helpers->test));
|
|
|
|
$this->assertTrue($helpers->isEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testHelperWrongConstructor()
|
|
|
|
{
|
|
|
|
$helper = new \Handlebars\Helpers("helper");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testHelperWrongCallable()
|
|
|
|
{
|
|
|
|
$helper = new \Handlebars\Helpers();
|
|
|
|
$helper->add('test', 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testHelperWrongGet()
|
|
|
|
{
|
|
|
|
$helper = new \Handlebars\Helpers();
|
|
|
|
$x = $helper->test;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testHelperWrongUnset()
|
|
|
|
{
|
|
|
|
$helper = new \Handlebars\Helpers();
|
|
|
|
unset($helper->test);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test String class
|
|
|
|
*/
|
|
|
|
public function testStringClass()
|
|
|
|
{
|
|
|
|
$string = new \Handlebars\String('test');
|
|
|
|
$this->assertEquals('test', $string->getString());
|
|
|
|
$string->setString('new');
|
|
|
|
$this->assertEquals('new', $string->getString());
|
2013-11-08 19:55:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $dir
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function delTree($dir)
|
|
|
|
{
|
|
|
|
$files = array_diff(scandir($dir), array('.', '..'));
|
|
|
|
foreach ($files as $file) {
|
2013-11-21 20:31:31 +04:00
|
|
|
(is_dir("$dir/$file")) ? $this->delTree("$dir/$file") : unlink("$dir/$file");
|
2013-11-08 19:55:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rmdir($dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Its not a good test :) but ok
|
|
|
|
*/
|
|
|
|
public function testCacheSystem()
|
|
|
|
{
|
|
|
|
$path = sys_get_temp_dir() . '/__cache__handlebars';
|
|
|
|
|
2013-11-08 20:10:32 +04:00
|
|
|
@$this->delTree($path);
|
2013-11-08 19:55:01 +04:00
|
|
|
|
|
|
|
$dummy = new \Handlebars\Cache\Disk($path);
|
|
|
|
$engine = new \Handlebars\Handlebars(array('cache' => $dummy));
|
|
|
|
$this->assertEquals(0, count(glob($path . '/*')));
|
|
|
|
$engine->render('test', array());
|
|
|
|
$this->assertEquals(1, count(glob($path . '/*')));
|
|
|
|
}
|
2013-11-21 21:13:52 +04:00
|
|
|
|
2014-02-26 20:56:51 +04:00
|
|
|
public function testArrayLoader()
|
|
|
|
{
|
|
|
|
$loader = new \Handlebars\Loader\ArrayLoader(array('test' => 'HELLO'));
|
|
|
|
$loader->addTemplate('another', 'GOODBYE');
|
|
|
|
$engine = new \Handlebars\Handlebars(array('loader' => $loader));
|
|
|
|
$this->assertEquals($engine->render('test', array()), 'HELLO');
|
|
|
|
$this->assertEquals($engine->render('another', array()), 'GOODBYE');
|
|
|
|
|
|
|
|
$this->setExpectedException('RuntimeException');
|
|
|
|
$engine->render('invalid-template', array());
|
|
|
|
}
|
|
|
|
|
2013-11-21 21:13:52 +04:00
|
|
|
/**
|
|
|
|
* Test file system loader
|
|
|
|
*/
|
|
|
|
public function testFileSystemLoader()
|
|
|
|
{
|
|
|
|
$loader = new \Handlebars\Loader\FilesystemLoader(realpath(__DIR__ . '/../fixture/data'));
|
|
|
|
$engine = new \Handlebars\Handlebars();
|
|
|
|
$engine->setLoader($loader);
|
|
|
|
$this->assertEquals('test', $engine->render('loader', array()));
|
|
|
|
}
|
2014-02-26 21:43:11 +04:00
|
|
|
|
2013-12-06 22:02:36 +04:00
|
|
|
/**
|
|
|
|
* Test file system loader
|
|
|
|
*/
|
|
|
|
public function testFileSystemLoaderMultipleFolder()
|
|
|
|
{
|
|
|
|
$paths = array(
|
|
|
|
realpath(__DIR__ . '/../fixture/data'),
|
|
|
|
realpath(__DIR__ . '/../fixture/another')
|
|
|
|
);
|
|
|
|
|
|
|
|
$options = array(
|
|
|
|
'prefix' => '__',
|
|
|
|
'extension' => 'hb'
|
|
|
|
);
|
|
|
|
$loader = new \Handlebars\Loader\FilesystemLoader($paths, $options);
|
|
|
|
$engine = new \Handlebars\Handlebars();
|
|
|
|
$engine->setLoader($loader);
|
|
|
|
$this->assertEquals('test_extra', $engine->render('loader', array()));
|
|
|
|
$this->assertEquals('another_extra', $engine->render('another', array()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test file system loader
|
2014-02-26 21:43:11 +04:00
|
|
|
*
|
2013-12-06 22:02:36 +04:00
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testFileSystemLoaderNotFound()
|
|
|
|
{
|
|
|
|
$loader = new \Handlebars\Loader\FilesystemLoader(realpath(__DIR__ . '/../fixture/data'));
|
|
|
|
$engine = new \Handlebars\Handlebars();
|
|
|
|
$engine->setLoader($loader);
|
|
|
|
$engine->render('invalid_file', array());
|
|
|
|
}
|
2014-02-26 21:43:11 +04:00
|
|
|
|
2013-12-06 22:02:36 +04:00
|
|
|
/**
|
|
|
|
* Test file system loader
|
2014-02-26 21:43:11 +04:00
|
|
|
*
|
2013-12-06 22:02:36 +04:00
|
|
|
* @expectedException \RuntimeException
|
|
|
|
*/
|
|
|
|
public function testFileSystemLoaderInvalidFolder()
|
|
|
|
{
|
|
|
|
new \Handlebars\Loader\FilesystemLoader(realpath(__DIR__ . '/../fixture/') . 'invalid/path');
|
|
|
|
}
|
2013-11-21 21:13:52 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test partial loader
|
|
|
|
*/
|
|
|
|
public function testPartialLoader()
|
|
|
|
{
|
|
|
|
$loader = new \Handlebars\Loader\StringLoader();
|
|
|
|
$partialLoader = new \Handlebars\Loader\FilesystemLoader(realpath(__DIR__ . '/../fixture/data'));
|
|
|
|
$engine = new \Handlebars\Handlebars();
|
|
|
|
$engine->setLoader($loader);
|
|
|
|
$engine->setPartialsLoader($partialLoader);
|
|
|
|
|
|
|
|
$this->assertEquals('test', $engine->render('{{>loader}}', array()));
|
|
|
|
}
|
|
|
|
|
2014-02-26 20:56:51 +04:00
|
|
|
public function testPartial()
|
|
|
|
{
|
|
|
|
$loader = new \Handlebars\Loader\StringLoader();
|
|
|
|
$partialLoader = new \Handlebars\Loader\ArrayLoader(array('test' => '{{key}}', 'bar' => 'its foo'));
|
|
|
|
$partialAliasses = array('foo' => 'bar');
|
|
|
|
$engine = new \Handlebars\Handlebars(
|
|
|
|
array(
|
|
|
|
'loader' => $loader,
|
|
|
|
'partials_loader' => $partialLoader,
|
|
|
|
'partials_alias' => $partialAliasses
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('HELLO', $engine->render('{{>test parameter}}', array('parameter' => array('key' => 'HELLO'))));
|
|
|
|
$this->assertEquals('its foo', $engine->render('{{>foo}}', array()));
|
|
|
|
$engine->registerPartial('foo-again', 'bar');
|
|
|
|
$this->assertEquals('its foo', $engine->render('{{>foo-again}}', array()));
|
|
|
|
$engine->unRegisterPartial('foo-again');
|
|
|
|
|
|
|
|
$this->setExpectedException('RuntimeException');
|
|
|
|
$engine->render('{{>foo-again}}', array());
|
|
|
|
}
|
|
|
|
|
2013-11-21 21:13:52 +04:00
|
|
|
/**
|
|
|
|
* test variable access
|
|
|
|
*/
|
|
|
|
public function testVariableAccess()
|
|
|
|
{
|
|
|
|
$loader = new \Handlebars\Loader\StringLoader();
|
2013-11-22 00:05:30 +04:00
|
|
|
$engine = \Handlebars\Handlebars::factory();
|
2013-11-21 21:13:52 +04:00
|
|
|
$engine->setLoader($loader);
|
|
|
|
|
|
|
|
$var = new \StdClass();
|
|
|
|
$var->x = 'var-x';
|
|
|
|
$var->y = array(
|
|
|
|
'z' => 'var-y-z'
|
|
|
|
);
|
|
|
|
$this->assertEquals('test', $engine->render('{{var}}', array('var' => 'test')));
|
|
|
|
$this->assertEquals('var-x', $engine->render('{{var.x}}', array('var' => $var)));
|
|
|
|
$this->assertEquals('var-y-z', $engine->render('{{var.y.z}}', array('var' => $var)));
|
|
|
|
// Access parent context in with helper
|
|
|
|
$this->assertEquals('var-x', $engine->render('{{#with var.y}}{{../var.x}}{{/with}}', array('var' => $var)));
|
|
|
|
|
|
|
|
$obj = new DateTime();
|
|
|
|
$time = $obj->getTimestamp();
|
|
|
|
$this->assertEquals($time, $engine->render('{{time.getTimestamp}}', array('time' => $obj)));
|
|
|
|
|
|
|
|
}
|
2013-12-06 21:13:51 +04:00
|
|
|
|
|
|
|
|
|
|
|
public function testContext()
|
|
|
|
{
|
|
|
|
$test = new stdClass();
|
|
|
|
$test->value = 'value';
|
2014-02-04 20:11:29 +04:00
|
|
|
$test->array = array(
|
|
|
|
'a' => '1',
|
|
|
|
'b' => '2',
|
|
|
|
'!"#%&\'()*+,./;<=>@[\\^`{|}~ ' => '3',
|
|
|
|
);
|
2013-12-06 21:13:51 +04:00
|
|
|
$context = new \Handlebars\Context($test);
|
|
|
|
$this->assertEquals('value', $context->get('value'));
|
|
|
|
$this->assertEquals('value', $context->get('value', true));
|
2014-02-04 20:11:29 +04:00
|
|
|
$this->assertEquals('value', $context->get('[value]', true));
|
2013-12-06 21:13:51 +04:00
|
|
|
$this->assertEquals('1', $context->get('array.a', true));
|
|
|
|
$this->assertEquals('2', $context->get('array.b', true));
|
2014-02-04 20:11:29 +04:00
|
|
|
$this->assertEquals('3', $context->get('array.[!"#%&\'()*+,./;<=>@[\\^`{|}~ ]', true));
|
2013-12-06 21:13:51 +04:00
|
|
|
$new = array('value' => 'new value');
|
|
|
|
$context->push($new);
|
|
|
|
$this->assertEquals('new value', $context->get('value'));
|
|
|
|
$this->assertEquals('new value', $context->get('value', true));
|
|
|
|
$this->assertEquals('value', $context->get('../value'));
|
|
|
|
$this->assertEquals('value', $context->get('../value', true));
|
|
|
|
$this->assertEquals($new, $context->last());
|
|
|
|
$this->assertEquals($new, $context->get('.'));
|
|
|
|
$this->assertEquals($new, $context->get('this'));
|
|
|
|
$this->assertEquals($new, $context->get('this.'));
|
|
|
|
$this->assertEquals($test, $context->get('../.'));
|
|
|
|
$context->pop();
|
|
|
|
$this->assertEquals('value', $context->get('value'));
|
|
|
|
$this->assertEquals('value', $context->get('value', true));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
* @dataProvider getInvalidData
|
|
|
|
*/
|
|
|
|
public function testInvalidAccessContext($invalid)
|
|
|
|
{
|
|
|
|
$context = new \Handlebars\Context(array());
|
|
|
|
$this->assertEmpty($context->get($invalid));
|
|
|
|
$context->get($invalid, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getInvalidData()
|
|
|
|
{
|
2014-02-26 21:43:11 +04:00
|
|
|
return array(
|
2013-12-06 21:13:51 +04:00
|
|
|
array('../../data'),
|
|
|
|
array('data'),
|
|
|
|
array(''),
|
|
|
|
array('data.key.key'),
|
|
|
|
);
|
|
|
|
}
|
2014-02-26 21:43:11 +04:00
|
|
|
|
2014-01-20 19:04:31 +04:00
|
|
|
/**
|
|
|
|
* Test for proper handling of the length property
|
|
|
|
**/
|
2014-02-26 21:43:11 +04:00
|
|
|
public function testArrayLengthEmulation()
|
|
|
|
{
|
|
|
|
|
|
|
|
$data = array("numbers" => array(1, 2, 3, 4),
|
|
|
|
"object" => (object)array("prop1" => "val1", "prop2" => "val2"),
|
|
|
|
"object_with_length_property" => (object)array("length" => "15cm")
|
2014-01-20 19:04:31 +04:00
|
|
|
);
|
|
|
|
$context = new \Handlebars\Context($data);
|
|
|
|
// make sure we are getting the array length when given an array
|
|
|
|
$this->assertEquals($context->get("numbers.length"), 4);
|
|
|
|
// make sure we are not getting a length when given an object
|
|
|
|
$this->assertEmpty($context->get("object.length"));
|
|
|
|
// make sure we can still get the length property when given an object
|
2014-02-26 21:43:11 +04:00
|
|
|
$this->assertEquals($context->get("object_with_length_property.length"), "15cm");
|
2014-01-20 19:04:31 +04:00
|
|
|
}
|
2014-02-26 21:43:11 +04:00
|
|
|
|
|
|
|
public function argumentParserProvider()
|
|
|
|
{
|
2014-01-20 19:04:31 +04:00
|
|
|
return array(
|
2014-02-26 21:43:11 +04:00
|
|
|
array('arg1 arg2', array("arg1", "arg2")),
|
|
|
|
array('"arg1 arg2"', array("arg1 arg2")),
|
|
|
|
array('arg1 arg2 "arg number 3"', array("arg1", "arg2", "arg number 3")),
|
|
|
|
array('arg1 "arg\"2" "\"arg3\""', array("arg1", 'arg"2', '"arg3"')),
|
|
|
|
array("'arg1 arg2'", array("arg1 arg2")),
|
|
|
|
array("arg1 arg2 'arg number 3'", array("arg1", "arg2", "arg number 3")),
|
|
|
|
array('arg1 "arg\"2" "\\\'arg3\\\'"', array("arg1", 'arg"2', "'arg3'")),
|
|
|
|
array('arg1 arg2.[value\'s "segment"].val', array("arg1", 'arg2.[value\'s "segment"].val')),
|
|
|
|
array('"arg1.[value 1]" arg2', array("arg1.[value 1]", 'arg2')),
|
2014-01-20 19:04:31 +04:00
|
|
|
);
|
|
|
|
}
|
2014-02-26 20:56:51 +04:00
|
|
|
|
2014-01-20 19:04:31 +04:00
|
|
|
/**
|
|
|
|
* Test Argument Parser
|
|
|
|
*
|
|
|
|
* @param string $arg_string argument text
|
2014-02-26 20:56:51 +04:00
|
|
|
* @param $expected_array
|
2014-01-20 19:04:31 +04:00
|
|
|
*
|
|
|
|
* @dataProvider argumentParserProvider
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2014-02-26 21:43:11 +04:00
|
|
|
public function testArgumentParser($arg_string, $expected_array)
|
|
|
|
{
|
2014-01-20 19:04:31 +04:00
|
|
|
$engine = new \Handlebars\Handlebars();
|
|
|
|
$template = new \Handlebars\Template($engine, null, null);
|
|
|
|
// get the string version of the arguments array
|
|
|
|
$args = $template->parseArguments($arg_string);
|
2014-02-26 21:43:11 +04:00
|
|
|
$args = array_map(function ($a) {
|
|
|
|
return (string)$a;
|
|
|
|
}, $args);
|
2014-01-20 19:04:31 +04:00
|
|
|
$this->assertEquals($args, $expected_array);
|
|
|
|
|
|
|
|
}
|
2014-02-26 21:43:11 +04:00
|
|
|
|
|
|
|
public function stringLiteralInCustomHelperProvider()
|
|
|
|
{
|
2014-01-20 19:57:16 +04:00
|
|
|
return array(
|
2014-02-26 21:43:11 +04:00
|
|
|
array('{{#test2 arg1 "Argument 2"}}', array("arg1" => "Argument 1"), "Argument 1:Argument 2"),
|
|
|
|
array('{{#test2 "Argument 1" "Argument 2"}}', array("arg1" => "Argument 1"), "Argument 1:Argument 2"),
|
|
|
|
array('{{#test2 "Argument 1" arg2}}', array("arg2" => "Argument 2"), "Argument 1:Argument 2")
|
2014-01-20 19:57:16 +04:00
|
|
|
);
|
|
|
|
}
|
2014-02-26 21:43:11 +04:00
|
|
|
|
2014-01-20 19:57:16 +04:00
|
|
|
/**
|
|
|
|
* Test String literals in the context of a helper
|
|
|
|
*
|
|
|
|
* @param string $template template text
|
2014-02-26 21:43:11 +04:00
|
|
|
* @param array $data context data
|
|
|
|
* @param string $results The Expected Results
|
2014-01-20 19:57:16 +04:00
|
|
|
*
|
|
|
|
* @dataProvider stringLiteralInCustomHelperProvider
|
|
|
|
*
|
|
|
|
* @return void
|
2014-02-26 21:43:11 +04:00
|
|
|
*/
|
|
|
|
public function testStringLiteralInCustomHelper($template, $data, $results)
|
|
|
|
{
|
2014-01-20 19:57:16 +04:00
|
|
|
$engine = new \Handlebars\Handlebars();
|
|
|
|
$engine->addHelper('test2', function ($template, $context, $args) {
|
|
|
|
$args = $template->parseArguments($args);
|
2014-02-26 21:43:11 +04:00
|
|
|
|
|
|
|
$args = array_map(function ($a) use ($context) {
|
|
|
|
return $context->get($a);
|
|
|
|
}, $args);
|
|
|
|
|
2014-01-20 19:57:16 +04:00
|
|
|
return implode(':', $args);
|
|
|
|
});
|
|
|
|
$res = $engine->render($template, $data);
|
|
|
|
$this->assertEquals($res, $results);
|
|
|
|
}
|
2014-02-26 21:43:11 +04:00
|
|
|
|
|
|
|
public function testString()
|
|
|
|
{
|
2014-01-20 19:57:16 +04:00
|
|
|
$string = new \Handlebars\String("Hello World");
|
|
|
|
$this->assertEquals((string)$string, "Hello World");
|
|
|
|
}
|
2014-02-26 21:43:11 +04:00
|
|
|
|
|
|
|
public function testInvalidNames()
|
|
|
|
{
|
|
|
|
$loader = new \Handlebars\Loader\StringLoader();
|
|
|
|
$engine = new \Handlebars\Handlebars(
|
|
|
|
array(
|
|
|
|
'loader' => $loader,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$all = \Handlebars\Context::NOT_VALID_NAME_CHARS;
|
|
|
|
|
|
|
|
for ($i = 0; $i < strlen($all); $i++) {
|
|
|
|
// Dot in string is valid, its an exception here
|
|
|
|
if ($all{$i} === '.') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$name = 'var' . $all{$i} . 'var';
|
|
|
|
$engine->render('{{' . $name . '}}', array($name => 'VALUE'));
|
|
|
|
throw new Exception("Accept the $name :/");
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->assertInstanceOf("InvalidArgumentException", $e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-12-06 21:13:51 +04:00
|
|
|
|
2014-03-12 22:34:15 +04:00
|
|
|
/**
|
|
|
|
* Helper subexpressions test
|
|
|
|
*/
|
|
|
|
public function testHelperSubexpressions()
|
|
|
|
{
|
|
|
|
$loader = new \Handlebars\Loader\StringLoader();
|
|
|
|
$engine = new \Handlebars\Handlebars(array('loader' => $loader));
|
|
|
|
$engine->addHelper('test', function ($template, $context, $arg) {
|
|
|
|
return $arg.'Test.';
|
|
|
|
});
|
|
|
|
|
|
|
|
// assert that nested syntax is accepted and sub-helper is run
|
|
|
|
$this->assertEquals('Test.Test.', $engine->render('{{test (test)}}', array()));
|
|
|
|
|
|
|
|
$engine->addHelper('add', function ($template, $context, $arg) {
|
|
|
|
$values = explode( " ", $arg );
|
|
|
|
return $values[0] + $values[1];
|
|
|
|
});
|
|
|
|
|
|
|
|
// assert that subexpression result is inserted correctly as argument to top level helper
|
|
|
|
$this->assertEquals('42', $engine->render('{{add 21 (add 10 (add 5 6))}}', array()));
|
2014-03-18 02:00:36 +04:00
|
|
|
|
|
|
|
|
|
|
|
// assert that bracketed expressions within string literals are treated correctly
|
|
|
|
$this->assertEquals("'(test)'Test.", $engine->render("{{test '(test)'}}", array()));
|
|
|
|
$this->assertEquals("')'Test.Test.", $engine->render("{{test (test ')')}}", array()));
|
2014-03-12 22:34:15 +04:00
|
|
|
}
|
|
|
|
|
2013-11-07 22:17:57 +04:00
|
|
|
}
|