|
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\Integration\Procedure; |
|
10
|
|
|
|
|
11
|
|
|
use JonnyW\PhantomJs\Http\Request; |
|
12
|
|
|
use JonnyW\PhantomJs\Procedure\ProcedureCompiler; |
|
13
|
|
|
use JonnyW\PhantomJs\DependencyInjection\ServiceContainer; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* PHP PhantomJs |
|
17
|
|
|
* |
|
18
|
|
|
* @author Jon Wenmoth <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class ProcedureCompilerTest extends \PHPUnit_Framework_TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** +++++++++++++++++++++++++++++++++++ **/ |
|
24
|
|
|
/** ++++++++++++++ TESTS ++++++++++++++ **/ |
|
25
|
|
|
/** +++++++++++++++++++++++++++++++++++ **/ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Test can compile procedure |
|
29
|
|
|
* |
|
30
|
|
|
* @access public |
|
31
|
|
|
* @return void |
|
32
|
|
|
*/ |
|
33
|
|
|
public function testCanCompileProcedure() |
|
34
|
|
|
{ |
|
35
|
|
|
$procedure = $this->getProcedure('http_default'); |
|
36
|
|
|
|
|
37
|
|
|
$uncompiled = $procedure->getTemplate(); |
|
38
|
|
|
|
|
39
|
|
|
$request = $this->getRequest(); |
|
40
|
|
|
$request->setUrl('http://test.com'); |
|
41
|
|
|
|
|
42
|
|
|
$compiler = $this->getProcedureCompiler(); |
|
43
|
|
|
$compiler->compile($procedure, $request); |
|
44
|
|
|
|
|
45
|
|
|
$this->assertNotSame($uncompiled, $procedure->getTemplate()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Test procedure is loaded from cache |
|
50
|
|
|
* if cache is enabled. |
|
51
|
|
|
* |
|
52
|
|
|
* @access public |
|
53
|
|
|
* @return void |
|
54
|
|
|
*/ |
|
55
|
|
|
public function testProcedureIsLoadedFromCacheIfCacheIsEnabled() |
|
56
|
|
|
{ |
|
57
|
|
|
$procedure1 = $this->getProcedure('http_default'); |
|
58
|
|
|
$procedure2 = $this->getProcedure('http_default'); |
|
59
|
|
|
|
|
60
|
|
|
$request = $this->getRequest(); |
|
61
|
|
|
$request->setUrl('http://test.com'); |
|
62
|
|
|
|
|
63
|
|
|
$renderer = $this->getMock('\JonnyW\PhantomJs\Template\TemplateRendererInterface'); |
|
64
|
|
|
$renderer->expects($this->exactly(1)) |
|
65
|
|
|
->method('render') |
|
66
|
|
|
->will($this->returnValue('var test=1; phantom.exit(1);')); |
|
67
|
|
|
|
|
68
|
|
|
$serviceContainer = ServiceContainer::getInstance(); |
|
69
|
|
|
|
|
70
|
|
|
$compiler = new ProcedureCompiler( |
|
71
|
|
|
$serviceContainer->get('phantomjs.procedure.chain_loader'), |
|
72
|
|
|
$serviceContainer->get('phantomjs.procedure.procedure_validator'), |
|
73
|
|
|
$serviceContainer->get('phantomjs.cache.file_cache'), |
|
74
|
|
|
$renderer |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
$compiler->enableCache(); |
|
78
|
|
|
$compiler->compile($procedure1, $request); |
|
79
|
|
|
$compiler->compile($procedure2, $request); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Test procedure is not loaded from |
|
84
|
|
|
* cache if cache is disabled. |
|
85
|
|
|
* |
|
86
|
|
|
* @access public |
|
87
|
|
|
* @return void |
|
88
|
|
|
*/ |
|
89
|
|
|
public function testProcedureIsNotLoadedFromCacheIfCacheIsDisabled() |
|
90
|
|
|
{ |
|
91
|
|
|
$procedure1 = $this->getProcedure('http_default'); |
|
92
|
|
|
$procedure2 = $this->getProcedure('http_default'); |
|
93
|
|
|
|
|
94
|
|
|
$request = $this->getRequest(); |
|
95
|
|
|
$request->setUrl('http://test.com'); |
|
96
|
|
|
|
|
97
|
|
|
$renderer = $this->getMock('\JonnyW\PhantomJs\Template\TemplateRendererInterface'); |
|
98
|
|
|
$renderer->expects($this->exactly(2)) |
|
99
|
|
|
->method('render') |
|
100
|
|
|
->will($this->returnValue('var test=1; phantom.exit(1);')); |
|
101
|
|
|
|
|
102
|
|
|
$serviceContainer = ServiceContainer::getInstance(); |
|
103
|
|
|
|
|
104
|
|
|
$compiler = new ProcedureCompiler( |
|
105
|
|
|
$serviceContainer->get('phantomjs.procedure.chain_loader'), |
|
106
|
|
|
$serviceContainer->get('phantomjs.procedure.procedure_validator'), |
|
107
|
|
|
$serviceContainer->get('phantomjs.cache.file_cache'), |
|
108
|
|
|
$renderer |
|
109
|
|
|
); |
|
110
|
|
|
|
|
111
|
|
|
$compiler->disableCache(); |
|
112
|
|
|
$compiler->compile($procedure1, $request); |
|
113
|
|
|
$compiler->compile($procedure2, $request); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Test procedure cache can be cleared. |
|
118
|
|
|
* |
|
119
|
|
|
* @access public |
|
120
|
|
|
* @return void |
|
121
|
|
|
*/ |
|
122
|
|
|
public function testProcedureCacheCanBeCleared() |
|
123
|
|
|
{ |
|
124
|
|
|
$procedure1 = $this->getProcedure('http_default'); |
|
125
|
|
|
$procedure2 = $this->getProcedure('http_default'); |
|
126
|
|
|
|
|
127
|
|
|
$request = $this->getRequest(); |
|
128
|
|
|
$request->setUrl('http://test.com'); |
|
129
|
|
|
|
|
130
|
|
|
$renderer = $this->getMock('\JonnyW\PhantomJs\Template\TemplateRendererInterface'); |
|
131
|
|
|
$renderer->expects($this->exactly(2)) |
|
132
|
|
|
->method('render') |
|
133
|
|
|
->will($this->returnValue('var test=1; phantom.exit(1);')); |
|
134
|
|
|
|
|
135
|
|
|
$serviceContainer = ServiceContainer::getInstance(); |
|
136
|
|
|
|
|
137
|
|
|
$compiler = new ProcedureCompiler( |
|
138
|
|
|
$serviceContainer->get('phantomjs.procedure.chain_loader'), |
|
139
|
|
|
$serviceContainer->get('phantomjs.procedure.procedure_validator'), |
|
140
|
|
|
$serviceContainer->get('phantomjs.cache.file_cache'), |
|
141
|
|
|
$renderer |
|
142
|
|
|
); |
|
143
|
|
|
|
|
144
|
|
|
$compiler->compile($procedure1, $request); |
|
145
|
|
|
$compiler->clearCache(); |
|
146
|
|
|
$compiler->compile($procedure2, $request); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Test syntax exception is thrown if compiled |
|
151
|
|
|
* template is not valid. |
|
152
|
|
|
* |
|
153
|
|
|
* @access public |
|
154
|
|
|
* @return void |
|
155
|
|
|
*/ |
|
156
|
|
|
public function testSyntaxExceptionIsThrownIfCompiledTemplateIsNotValid() |
|
157
|
|
|
{ |
|
158
|
|
|
$this->setExpectedException('\JonnyW\PhantomJs\Exception\SyntaxException'); |
|
159
|
|
|
|
|
160
|
|
|
$template = <<<EOF |
|
161
|
|
|
console.log(; |
|
162
|
|
|
EOF; |
|
163
|
|
|
$procedure = $this->getProcedure('http_default'); |
|
164
|
|
|
$procedure->setTemplate($template); |
|
165
|
|
|
|
|
166
|
|
|
$request = $this->getRequest(); |
|
167
|
|
|
$request->setUrl('http://test.com'); |
|
168
|
|
|
|
|
169
|
|
|
$compiler = $this->getProcedureCompiler(); |
|
170
|
|
|
$compiler->compile($procedure, $request); |
|
171
|
|
|
|
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** +++++++++++++++++++++++++++++++++++ **/ |
|
175
|
|
|
/** ++++++++++ TEST ENTITIES ++++++++++ **/ |
|
176
|
|
|
/** +++++++++++++++++++++++++++++++++++ **/ |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Get procedure compiler. |
|
180
|
|
|
* |
|
181
|
|
|
* @access protected |
|
182
|
|
|
* @return \JonnyW\PhantomJs\Procedure\ProcedureCompiler |
|
183
|
|
|
*/ |
|
184
|
|
|
protected function getProcedureCompiler() |
|
185
|
|
|
{ |
|
186
|
|
|
$serviceContainer = ServiceContainer::getInstance(); |
|
187
|
|
|
|
|
188
|
|
|
$compiler = new ProcedureCompiler( |
|
189
|
|
|
$serviceContainer->get('phantomjs.procedure.chain_loader'), |
|
190
|
|
|
$serviceContainer->get('phantomjs.procedure.procedure_validator'), |
|
191
|
|
|
$serviceContainer->get('phantomjs.cache.file_cache'), |
|
192
|
|
|
$serviceContainer->get('phantomjs.procedure.template_renderer') |
|
193
|
|
|
); |
|
194
|
|
|
|
|
195
|
|
|
return $compiler; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* getProcedure function. |
|
200
|
|
|
* |
|
201
|
|
|
* @access protected |
|
202
|
|
|
* @param string $id |
|
203
|
|
|
* @return \JonnyW\PhantomJs\Procedure\ProcedureInterface |
|
204
|
|
|
*/ |
|
205
|
|
|
protected function getProcedure($id) |
|
206
|
|
|
{ |
|
207
|
|
|
return ServiceContainer::getInstance()->get('procedure_loader')->load($id); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Get request |
|
212
|
|
|
* |
|
213
|
|
|
* @access protected |
|
214
|
|
|
* @return \JonnyW\PhantomJs\Http\Request |
|
215
|
|
|
*/ |
|
216
|
|
|
protected function getRequest() |
|
217
|
|
|
{ |
|
218
|
|
|
$request = new Request(); |
|
219
|
|
|
|
|
220
|
|
|
return $request; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** +++++++++++++++++++++++++++++++ **/ |
|
224
|
|
|
/** ++++++++++ UTILITIES ++++++++++ **/ |
|
225
|
|
|
/** +++++++++++++++++++++++++++++++ **/ |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Set up tasks. |
|
229
|
|
|
* |
|
230
|
|
|
* @access protected |
|
231
|
|
|
* @return void |
|
232
|
|
|
*/ |
|
233
|
|
|
protected function setUp() |
|
234
|
|
|
{ |
|
235
|
|
|
parent::setUp(); |
|
236
|
|
|
|
|
237
|
|
|
$this->cleanup(); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Tear down tasks. |
|
242
|
|
|
* |
|
243
|
|
|
* @access protected |
|
244
|
|
|
* @return void |
|
245
|
|
|
*/ |
|
246
|
|
|
protected function tearDown() |
|
247
|
|
|
{ |
|
248
|
|
|
parent::setUp(); |
|
|
|
|
|
|
249
|
|
|
|
|
250
|
|
|
$this->cleanup(); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* Clean up cache files. |
|
255
|
|
|
* |
|
256
|
|
|
* @access protected |
|
257
|
|
|
* @return void |
|
258
|
|
|
*/ |
|
259
|
|
|
protected function cleanup() |
|
260
|
|
|
{ |
|
261
|
|
|
$cache = sprintf('%s/phantomjs_*', sys_get_temp_dir()); |
|
262
|
|
|
|
|
263
|
|
|
array_map('unlink', glob($cache)); |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.