This repository was archived by the owner on May 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHandlebarsTest.php
135 lines (119 loc) · 3.91 KB
/
HandlebarsTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace Handlebars\Tests;
use Handlebars\Compiler;
use Handlebars\DefaultRegistry;
use Handlebars\Handlebars;
use Handlebars\Tests\Common;
use ReflectionObject;
class HandlebarsTest extends Common
{
public function setUp()
{
if( !extension_loaded('handlebars') ) {
return $this->markTestSkipped('The handlebars extension is not loaded.');
}
}
public function testCompileThrowsExceptionWithInvalidTemplate1()
{
$stub = $this->getMockBuilder('\\Handlebars\\Compiler\\PhpCompiler')
->getMock();
$stub->method('compile')
->willReturn(false);
$handlebars = new Handlebars(array(
'phpCompiler' => $stub
));
// Note: not testing parse error in eval because it's not possible
// to catch the output. eval returning false should have the same
// behaviour (minus the output)
$this->setExpectedException('\\Handlebars\\CompileException');
$handlebars->compile('{{foo}}');
}
public function testCompilerRenderMode()
{
$handlebars = new Handlebars();
$this->assertEquals('bar', $handlebars->render('{{foo}}', array(
'foo' => 'bar',
)));
}
public function testHelpersSpecifiedAtConstruction()
{
$handlebars = new Handlebars(array(
'helpers' => array(
'foo' => function () {
return 'bar';
}
)
));
$this->assertEquals('bar', $handlebars->render('{{foo}}'));
}
public function testPartialsSpecifiedAtConstruction()
{
$handlebars = new Handlebars(array(
'partials' => array(
'foo' => '{{foo}}'
)
));
$this->assertEquals('bar', $handlebars->render('{{> foo}}', array(
'foo' => 'bar',
)));
}
public function testRenderSupportsStdClass()
{
$handlebars = new Handlebars();
$this->assertEquals('foo', $handlebars->render('{{bar.baz}}', array(
'bar' => (object) array(
'baz' => 'foo'
)
)));
}
public function testRenderFile()
{
$handlebars = new \Handlebars\VM();
$this->assertEquals('bar', $handlebars->renderFile(__DIR__ . '/fixture1.hbs', array('foo' => 'bar')));
}
public function testGH29RCEFix()
{
$handlebars = new Handlebars();
$this->assertEquals('time', $handlebars->render('{{foo}}', array(
'foo' => 'time'
)));
}
public function testConsecutiveMultilineComments()
{
$tmpl = "{{!-- blah1 --}}\nfoo\n{{!-- blah2 --}}";
$handlebars = new Handlebars();
$actual = trim($handlebars->render($tmpl));
$this->assertEquals('foo', $actual);
}
public function testLog()
{
$tmpl = '{{log "test"}}';
$logger = new MockLogger();
$handlebars = new Handlebars();
$handlebars->setLogger($logger);
$handlebars->render($tmpl);
$this->assertEquals('info', $logger->logs[0][0]);
$this->assertEquals('test', $logger->logs[0][1]);
}
public function testLogWithNewVM()
{
$tmpl = '{{log "test"}}';
$logger = new MockLogger();
$handlebars = new \Handlebars\VM();
$handlebars->setLogger($logger);
$handlebars->render($tmpl);
$this->assertEquals('info', $logger->logs[0][0]);
$this->assertEquals('string(test) ', $logger->logs[0][1]);
}
public function testPartialFunctionWithNewVM()
{
$tmpl = '{{> foo}}';
$handlebars = new \Handlebars\VM();
$partials = new DefaultRegistry();
$partials['foo'] = function() {
return 'bar{{baz}}';
};
$handlebars->setPartials($partials);
$this->assertEquals('bar{{baz}}', $handlebars->render($tmpl, array('grr' => 'asdasd')));
}
}