add another test

This commit is contained in:
fzerorubigd 2014-02-26 21:13:11 +03:30
parent 939c58bbee
commit 9c91ea7d64
No known key found for this signature in database
GPG Key ID: D6EE858AF9D2999A
3 changed files with 113 additions and 63 deletions

View File

@ -1,3 +1,6 @@
inherit: true inherit: true
filter:
paths: [src/*]
excluded_paths: [tests/*]
tools: tools:
php_code_coverage: true php_code_coverage: true

View File

@ -278,12 +278,12 @@ class Context
/** /**
* Splits variable name to chunks. * Splits variable name to chunks.
* *
* @param string $variable_name Fully qualified name of a variable. * @param string $variableName Fully qualified name of a variable.
* *
* @throws \InvalidArgumentException if variable name is invalid. * @throws \InvalidArgumentException if variable name is invalid.
* @return array * @return array
*/ */
private function _splitVariableName($variable_name) private function _splitVariableName($variableName)
{ {
$bad_chars = preg_quote(self::NOT_VALID_NAME_CHARS, '/'); $bad_chars = preg_quote(self::NOT_VALID_NAME_CHARS, '/');
$bad_seg_chars = preg_quote(self::NOT_VALID_SEGMENT_NAME_CHARS, '/'); $bad_seg_chars = preg_quote(self::NOT_VALID_SEGMENT_NAME_CHARS, '/');
@ -292,11 +292,11 @@ class Context
$check_pattern = "/^((" . $name_pattern . ")\.)*(" . $name_pattern . ")\.?$/"; $check_pattern = "/^((" . $name_pattern . ")\.)*(" . $name_pattern . ")\.?$/";
$get_pattern = "/(?:" . $name_pattern . ")/"; $get_pattern = "/(?:" . $name_pattern . ")/";
if (!preg_match($check_pattern, $variable_name)) { if (!preg_match($check_pattern, $variableName)) {
throw new \InvalidArgumentException('variable name is invalid'); throw new \InvalidArgumentException('variable name is invalid');
} }
preg_match_all($get_pattern, $variable_name, $matches); preg_match_all($get_pattern, $variableName, $matches);
$chunks = array(); $chunks = array();
foreach ($matches[0] as $chunk) { foreach ($matches[0] as $chunk) {

View File

@ -24,6 +24,7 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
{ {
\Handlebars\Autoloader::register(); \Handlebars\Autoloader::register();
} }
/** /**
* Test handlebars autoloader * Test handlebars autoloader
* *
@ -265,10 +266,14 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
$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()));
$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()));
$engine->addHelper('renderme', function() {return new \Handlebars\String("{{test}}");}); $engine->addHelper('renderme', function () {
return new \Handlebars\String("{{test}}");
});
$this->assertEquals('Test helper is called', $engine->render('{{#renderme}}', array())); $this->assertEquals('Test helper is called', $engine->render('{{#renderme}}', array()));
$engine->addHelper('dontrenderme', function() {return "{{test}}";}); $engine->addHelper('dontrenderme', function () {
return "{{test}}";
});
$this->assertEquals('{{test}}', $engine->render('{{#dontrenderme}}', array())); $this->assertEquals('{{test}}', $engine->render('{{#dontrenderme}}', array()));
$engine->addHelper('markupHelper', function () { $engine->addHelper('markupHelper', function () {
@ -334,11 +339,13 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
public function testHelpersClass() public function testHelpersClass()
{ {
$helpers = new \Handlebars\Helpers(); $helpers = new \Handlebars\Helpers();
$helpers->add('test', function(){}); $helpers->add('test', function () {
});
$this->assertTrue($helpers->has('test')); $this->assertTrue($helpers->has('test'));
$this->assertTrue(isset($helpers->test)); $this->assertTrue(isset($helpers->test));
$this->assertFalse($helpers->isEmpty()); $this->assertFalse($helpers->isEmpty());
$helpers->test2 = function(){}; $helpers->test2 = function () {
};
$this->assertTrue($helpers->has('test2')); $this->assertTrue($helpers->has('test2'));
$this->assertTrue(isset($helpers->test2)); $this->assertTrue(isset($helpers->test2));
$this->assertFalse($helpers->isEmpty()); $this->assertFalse($helpers->isEmpty());
@ -450,6 +457,7 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
$engine->setLoader($loader); $engine->setLoader($loader);
$this->assertEquals('test', $engine->render('loader', array())); $this->assertEquals('test', $engine->render('loader', array()));
} }
/** /**
* Test file system loader * Test file system loader
*/ */
@ -473,6 +481,7 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
/** /**
* Test file system loader * Test file system loader
*
* @expectedException \InvalidArgumentException * @expectedException \InvalidArgumentException
*/ */
public function testFileSystemLoaderNotFound() public function testFileSystemLoaderNotFound()
@ -482,8 +491,10 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
$engine->setLoader($loader); $engine->setLoader($loader);
$engine->render('invalid_file', array()); $engine->render('invalid_file', array());
} }
/** /**
* Test file system loader * Test file system loader
*
* @expectedException \RuntimeException * @expectedException \RuntimeException
*/ */
public function testFileSystemLoaderInvalidFolder() public function testFileSystemLoaderInvalidFolder()
@ -611,7 +622,8 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
/** /**
* Test for proper handling of the length property * Test for proper handling of the length property
**/ **/
public function testArrayLengthEmulation(){ public function testArrayLengthEmulation()
{
$data = array("numbers" => array(1, 2, 3, 4), $data = array("numbers" => array(1, 2, 3, 4),
"object" => (object)array("prop1" => "val1", "prop2" => "val2"), "object" => (object)array("prop1" => "val1", "prop2" => "val2"),
@ -626,7 +638,8 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($context->get("object_with_length_property.length"), "15cm"); $this->assertEquals($context->get("object_with_length_property.length"), "15cm");
} }
public function argumentParserProvider(){ public function argumentParserProvider()
{
return array( return array(
array('arg1 arg2', array("arg1", "arg2")), array('arg1 arg2', array("arg1", "arg2")),
array('"arg1 arg2"', array("arg1 arg2")), array('"arg1 arg2"', array("arg1 arg2")),
@ -650,23 +663,28 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
* *
* @return void * @return void
*/ */
public function testArgumentParser($arg_string, $expected_array){ public function testArgumentParser($arg_string, $expected_array)
{
$engine = new \Handlebars\Handlebars(); $engine = new \Handlebars\Handlebars();
$template = new \Handlebars\Template($engine, null, null); $template = new \Handlebars\Template($engine, null, null);
// get the string version of the arguments array // get the string version of the arguments array
$args = $template->parseArguments($arg_string); $args = $template->parseArguments($arg_string);
$args = array_map(function($a){ return (string)$a; }, $args); $args = array_map(function ($a) {
return (string)$a;
}, $args);
$this->assertEquals($args, $expected_array); $this->assertEquals($args, $expected_array);
} }
public function stringLiteralInCustomHelperProvider(){ public function stringLiteralInCustomHelperProvider()
{
return array( return array(
array('{{#test2 arg1 "Argument 2"}}', array("arg1" => "Argument 1"), "Argument 1:Argument 2"), 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" "Argument 2"}}', array("arg1" => "Argument 1"), "Argument 1:Argument 2"),
array('{{#test2 "Argument 1" arg2}}', array("arg2" => "Argument 2"), "Argument 1:Argument 2") array('{{#test2 "Argument 1" arg2}}', array("arg2" => "Argument 2"), "Argument 1:Argument 2")
); );
} }
/** /**
* Test String literals in the context of a helper * Test String literals in the context of a helper
* *
@ -678,23 +696,52 @@ class HandlebarsTest extends \PHPUnit_Framework_TestCase
* *
* @return void * @return void
*/ */
public function testStringLiteralInCustomHelper($template, $data, $results){ public function testStringLiteralInCustomHelper($template, $data, $results)
{
$engine = new \Handlebars\Handlebars(); $engine = new \Handlebars\Handlebars();
$engine->addHelper('test2', function ($template, $context, $args) { $engine->addHelper('test2', function ($template, $context, $args) {
$args = $template->parseArguments($args); $args = $template->parseArguments($args);
$args = array_map(function ($a) use ($context) {return $context->get($a);}, $args); $args = array_map(function ($a) use ($context) {
return $context->get($a);
}, $args);
return implode(':', $args); return implode(':', $args);
}); });
$res = $engine->render($template, $data); $res = $engine->render($template, $data);
$this->assertEquals($res, $results); $this->assertEquals($res, $results);
} }
public function testString(){ public function testString()
{
$string = new \Handlebars\String("Hello World"); $string = new \Handlebars\String("Hello World");
$this->assertEquals((string)$string, "Hello World"); $this->assertEquals((string)$string, "Hello World");
} }
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);
}
}
}
} }