mirror of
https://github.com/Mibew/handlebars.php.git
synced 2025-03-21 16:11:24 +03:00
116 lines
2.8 KiB
PHP
116 lines
2.8 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Class AutoloaderTest
|
|
*/
|
|
class HandlebarsTest extends \PHPUnit_Framework_TestCase
|
|
{
|
|
/**
|
|
* Test handlebars autoloader
|
|
*
|
|
* @return void
|
|
*/
|
|
public function testAutoLoad()
|
|
{
|
|
Handlebars\Autoloader::register(realpath(__DIR__ . '/../fixture/'));
|
|
|
|
$this->assertTrue(class_exists('Handlebars\\Test'));
|
|
$this->assertTrue(class_exists('Handlebars\\Example\\Test'));
|
|
}
|
|
|
|
/**
|
|
* 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'
|
|
),
|
|
);
|
|
}
|
|
|
|
|
|
/**
|
|
* 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}}',
|
|
array('data' => array(1,2,3,4)),
|
|
'1234'
|
|
),
|
|
);
|
|
}
|
|
} |