1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the php-phantomjs. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace JonnyW\PhantomJs\Tests\Unit\Template; |
10
|
|
|
|
11
|
|
|
use Twig_Environment; |
12
|
|
|
use Twig_Loader_String; |
13
|
|
|
use JonnyW\PhantomJs\Http\Request; |
14
|
|
|
use JonnyW\PhantomJs\Template\TemplateRenderer; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* PHP PhantomJs |
18
|
|
|
* |
19
|
|
|
* @author Jon Wenmoth <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class TemplateRendererTest extends \PHPUnit_Framework_TestCase |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** +++++++++++++++++++++++++++++++++++ **/ |
25
|
|
|
/** ++++++++++++++ TESTS ++++++++++++++ **/ |
26
|
|
|
/** +++++++++++++++++++++++++++++++++++ **/ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Test render injects single parameter |
30
|
|
|
* into template. |
31
|
|
|
* |
32
|
|
|
* @access public |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
|
|
public function testRenderInjectsSingleParameterIntoTemplate() |
36
|
|
|
{ |
37
|
|
|
$template = 'var param = "{{ test }}"'; |
38
|
|
|
|
39
|
|
|
$renderer = $this->getTemplateRenderer(); |
40
|
|
|
$result = $renderer->render($template, array('test' => 'data')); |
41
|
|
|
|
42
|
|
|
$this->assertSame('var param = "data"', $result); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Test render injects multiple parameters |
47
|
|
|
* into template. |
48
|
|
|
* |
49
|
|
|
* @access public |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
|
|
public function testRenderInjectsMultipleParametersIntoTemplates() |
53
|
|
|
{ |
54
|
|
|
$template = 'var param = "{{ test }}", var param2 = "{{ test2 }}"'; |
55
|
|
|
|
56
|
|
|
$renderer = $this->getTemplateRenderer(); |
57
|
|
|
$result = $renderer->render($template, array('test' => 'data', 'test2' => 'more data')); |
58
|
|
|
|
59
|
|
|
$this->assertSame('var param = "data", var param2 = "more data"', $result); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Test render injects parameter into |
64
|
|
|
* template using object method. |
65
|
|
|
* |
66
|
|
|
* @access public |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
public function testRenderInjectsParameterIntoTemplateUsingObjectMethod() |
70
|
|
|
{ |
71
|
|
|
$template = 'var param = {{ request.getTimeout() }}'; |
72
|
|
|
|
73
|
|
|
$request = $this->getRequest(); |
74
|
|
|
$request->setTimeout(5000); |
75
|
|
|
|
76
|
|
|
$renderer = $this->getTemplateRenderer(); |
77
|
|
|
$result = $renderer->render($template, array('request' => $request)); |
78
|
|
|
|
79
|
|
|
$this->assertSame('var param = 5000', $result); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Test render injects parameter into |
84
|
|
|
* template using object method |
85
|
|
|
* with parameter. |
86
|
|
|
* |
87
|
|
|
* @access public |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
public function testRenderInjectsParameterIntoTemplateUsingObjectMethodWithParameter() |
91
|
|
|
{ |
92
|
|
|
$template = 'var param = {{ request.getHeaders("json") }}'; |
93
|
|
|
|
94
|
|
|
$request = $this->getRequest(); |
95
|
|
|
$request->addHeader('json', 'test'); |
96
|
|
|
|
97
|
|
|
$renderer = $this->getTemplateRenderer(); |
98
|
|
|
$result = $renderer->render($template, array('request' => $request)); |
99
|
|
|
|
100
|
|
|
$this->assertSame(htmlspecialchars('var param = {"json":"test"}'), $result); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** +++++++++++++++++++++++++++++++++++ **/ |
104
|
|
|
/** ++++++++++ TEST ENTITIES ++++++++++ **/ |
105
|
|
|
/** +++++++++++++++++++++++++++++++++++ **/ |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get template renderer instance. |
109
|
|
|
* |
110
|
|
|
* @return \JonnyW\PhantomJs\Message\TemplateRenderer |
111
|
|
|
*/ |
112
|
|
|
protected function getTemplateRenderer() |
113
|
|
|
{ |
114
|
|
|
$templateRenderer = new TemplateRenderer( |
115
|
|
|
$this->getTwig() |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
return $templateRenderer; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get request |
123
|
|
|
* |
124
|
|
|
* @access protected |
125
|
|
|
* @return \JonnyW\PhantomJs\Http\Request |
126
|
|
|
*/ |
127
|
|
|
protected function getRequest() |
128
|
|
|
{ |
129
|
|
|
$request = new Request(); |
130
|
|
|
|
131
|
|
|
return $request; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get twig |
136
|
|
|
* |
137
|
|
|
* @access protected |
138
|
|
|
* @return \Twig_Environment |
139
|
|
|
*/ |
140
|
|
|
protected function getTwig() |
141
|
|
|
{ |
142
|
|
|
$twig = new Twig_Environment( |
143
|
|
|
new Twig_Loader_String() |
|
|
|
|
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
return $twig; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.