forked from timber/timber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-timber-function-wrapper.php
92 lines (79 loc) · 2.49 KB
/
test-timber-function-wrapper.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
<?php
class TestTimberFunctionWrapper extends Timber_UnitTestCase
{
public function testToStringWithException()
{
ob_start();
$wrapper = new Timber\FunctionWrapper('TestTimberFunctionWrapper::isNum', ['hi']);
echo $wrapper;
$content = trim(ob_get_contents());
ob_end_clean();
$this->assertEquals('Caught exception: Argument must be of type integer', $content);
}
public function testToStringWithoutException()
{
ob_start();
$wrapper = new Timber\FunctionWrapper('TestTimberFunctionWrapper::isNum', [4]);
echo $wrapper;
$content = trim(ob_get_contents());
ob_end_clean();
$this->assertSame('1', $content);
}
public function testToStringWithClassObject()
{
ob_start();
$wrapper = new Timber\FunctionWrapper([$this, 'isNum'], [4]);
echo $wrapper;
$content = trim(ob_get_contents());
ob_end_clean();
$this->assertSame('1', $content);
}
public function testToStringWithClassString()
{
ob_start();
$wrapper = new Timber\FunctionWrapper([get_class($this), 'isNum'], [4]);
echo $wrapper;
$content = trim(ob_get_contents());
ob_end_clean();
$this->assertSame('1', $content);
}
public function testWPHead()
{
$context = Timber::context();
$str = Timber::compile_string("{{ function('wp_head') }}", $context);
$this->assertMatchesRegularExpression('/<title>Test Blog/', trim($str));
}
public function testFunctionInTemplate()
{
$context = Timber::context();
$str = Timber::compile_string("{{ function('my_boo') }}", $context);
$this->assertEquals('bar!', trim($str));
}
public function testNakedSoloFunction()
{
add_filter('timber/twig', function ($twig) {
$twig->addFunction(new \Twig\TwigFunction('your_boo', [$this, 'your_boo']));
return $twig;
});
$context = Timber::context();
$str = Timber::compile_string("{{ your_boo() }}", $context);
$this->assertEquals('yourboo', trim($str));
}
/* Sample function to test exception handling */
public static function isNum($num)
{
if (!is_int($num)) {
throw new Exception("Argument must be of type integer");
} else {
return true;
}
}
public function your_boo()
{
return 'yourboo';
}
}
function my_boo()
{
return 'bar!';
}