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; |
10
|
|
|
|
11
|
|
|
use JonnyW\PhantomJs\Engine; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* PHP PhantomJs |
15
|
|
|
* |
16
|
|
|
* @author Jon Wenmoth <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class EngineTest extends \PHPUnit_Framework_TestCase |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** +++++++++++++++++++++++++++++++++++ **/ |
22
|
|
|
/** ++++++++++++++ TESTS ++++++++++++++ **/ |
23
|
|
|
/** +++++++++++++++++++++++++++++++++++ **/ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Test invalid executable exception is thrown |
27
|
|
|
* if phantom JS path is invalid. |
28
|
|
|
* |
29
|
|
|
* @access public |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
public function testInvalidExecutableExceptionIsThrownIfPhantomJSPathIsInvalid() |
33
|
|
|
{ |
34
|
|
|
$this->setExpectedException('\JonnyW\PhantomJs\Exception\InvalidExecutableException'); |
35
|
|
|
|
36
|
|
|
$engine = $this->getEngine(); |
37
|
|
|
$engine->setPath('/invalid/phantomjs/path'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Test default phantom JS path is returned |
42
|
|
|
* if no custom path is set. |
43
|
|
|
* |
44
|
|
|
* @access public |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function testDefaultPhantomJSPathIsReturnedIfNoCustomPathIsSet() |
48
|
|
|
{ |
49
|
|
|
$engine = $this->getEngine(); |
50
|
|
|
|
51
|
|
|
$this->assertSame('bin/phantomjs', $engine->getPath()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Test can log data. |
56
|
|
|
* |
57
|
|
|
* @access public |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function testCanLogData() |
61
|
|
|
{ |
62
|
|
|
$log = 'Test log info'; |
63
|
|
|
|
64
|
|
|
$engine = $this->getEngine(); |
65
|
|
|
$engine->log($log); |
66
|
|
|
|
67
|
|
|
$this->assertSame($log, $engine->getLog()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Test can clear log. |
72
|
|
|
* |
73
|
|
|
* @access public |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
public function testCanClearLog() |
77
|
|
|
{ |
78
|
|
|
$log = 'Test log info'; |
79
|
|
|
|
80
|
|
|
$engine = $this->getEngine(); |
81
|
|
|
$engine->log($log); |
82
|
|
|
$engine->clearLog(); |
83
|
|
|
|
84
|
|
|
$this->assertEmpty($engine->getLog()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Test can add run option. |
89
|
|
|
* |
90
|
|
|
* @access public |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
public function testCanAddRunOption() |
94
|
|
|
{ |
95
|
|
|
$options = array( |
96
|
|
|
'option1', |
97
|
|
|
'option2' |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$engine = $this->getEngine(); |
101
|
|
|
$engine->setOptions($options); |
102
|
|
|
$engine->addOption('option3'); |
103
|
|
|
|
104
|
|
|
array_push($options, 'option3'); |
105
|
|
|
|
106
|
|
|
$this->assertSame($options, $engine->getOptions()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Test invalid executable exception is thrown when |
111
|
|
|
* building command if path to phantom JS is valid. |
112
|
|
|
* |
113
|
|
|
* @access public |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
|
|
public function testInvalidExecutableExceptionIsThrownWhenBuildingCommandIfPathToPhantomJSIsInvalid() |
117
|
|
|
{ |
118
|
|
|
$this->setExpectedException('\JonnyW\PhantomJs\Exception\InvalidExecutableException'); |
119
|
|
|
|
120
|
|
|
$engine = $this->getEngine(); |
121
|
|
|
|
122
|
|
|
$phantomJs = new \ReflectionProperty(get_class($engine), 'path'); |
123
|
|
|
$phantomJs->setAccessible(true); |
124
|
|
|
$phantomJs->setValue($engine, 'invalid/path'); |
125
|
|
|
|
126
|
|
|
$engine->getCommand(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Test command contains phantom JS executable |
131
|
|
|
* |
132
|
|
|
* @access public |
133
|
|
|
* @return void |
134
|
|
|
*/ |
135
|
|
|
public function testCommandContainsPhantomJSExecutable() |
136
|
|
|
{ |
137
|
|
|
$engine = $this->getEngine(); |
138
|
|
|
|
139
|
|
|
$this->assertContains($engine->getPath(), $engine->getCommand()); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Test debug flag can be set. |
144
|
|
|
* |
145
|
|
|
* @access public |
146
|
|
|
* @return void |
147
|
|
|
*/ |
148
|
|
|
public function testDebugFlagCanBeSet() |
149
|
|
|
{ |
150
|
|
|
$engine = $this->getEngine(); |
151
|
|
|
$engine->debug(true); |
152
|
|
|
|
153
|
|
|
$this->assertContains('--debug=true', $engine->getCommand()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Test debug flag is not set if |
158
|
|
|
* debugging is not enabled. |
159
|
|
|
* |
160
|
|
|
* @access public |
161
|
|
|
* @return void |
162
|
|
|
*/ |
163
|
|
|
public function testDebugFlagIsNotSetIfDebuggingIsNotEnabled() |
164
|
|
|
{ |
165
|
|
|
$engine = $this->getEngine(); |
166
|
|
|
$engine->debug(false); |
167
|
|
|
|
168
|
|
|
$this->assertNotContains('--debug=true', $engine->getCommand()); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Test disk cache flag can be set. |
173
|
|
|
* |
174
|
|
|
* @access public |
175
|
|
|
* @return void |
176
|
|
|
*/ |
177
|
|
|
public function testDiskCacheFlagCanBeSet() |
178
|
|
|
{ |
179
|
|
|
$engine = $this->getEngine(); |
180
|
|
|
$engine->cache(true); |
181
|
|
|
|
182
|
|
|
$this->assertContains('--disk-cache=true', $engine->getCommand()); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Test disk cache flag is not set if |
187
|
|
|
* caching is not enabled. |
188
|
|
|
* |
189
|
|
|
* @access public |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
|
|
public function testDiskCacheFlagIsNotSetIfCachingIsNotEnabled() |
193
|
|
|
{ |
194
|
|
|
$engine = $this->getEngine(); |
195
|
|
|
$engine->cache(false); |
196
|
|
|
|
197
|
|
|
$this->assertNotContains('--disk-cache=true', $engine->getCommand()); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Test command contains run options. |
202
|
|
|
* |
203
|
|
|
* @access public |
204
|
|
|
* @return void |
205
|
|
|
*/ |
206
|
|
|
public function testCommandContainsRunOptions() |
207
|
|
|
{ |
208
|
|
|
$option1 = '--local-storage-path=/some/path'; |
209
|
|
|
$option2 = '--local-storage-quota=5'; |
210
|
|
|
$option3 = '--local-to-remote-url-access=true'; |
211
|
|
|
|
212
|
|
|
$engine = $this->getEngine(); |
213
|
|
|
$engine->addOption($option1); |
214
|
|
|
$engine->addOption($option2); |
215
|
|
|
$engine->addOption($option3); |
216
|
|
|
|
217
|
|
|
$command = $engine->getCommand(); |
218
|
|
|
|
219
|
|
|
$this->assertContains($option1, $command); |
220
|
|
|
$this->assertContains($option2, $command); |
221
|
|
|
$this->assertContains($option3, $command); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Test debug flag is set if runs options |
226
|
|
|
* are also set. |
227
|
|
|
* |
228
|
|
|
* @access public |
229
|
|
|
* @return void |
230
|
|
|
*/ |
231
|
|
|
public function testDebugFlagIsSetIfRunOptionsAreAlsoSet() |
232
|
|
|
{ |
233
|
|
|
$option = '--local-storage-path=/some/path'; |
234
|
|
|
|
235
|
|
|
$engine = $this->getEngine(); |
236
|
|
|
$engine->addOption($option); |
237
|
|
|
$engine->debug(true); |
238
|
|
|
|
239
|
|
|
$command = $engine->getCommand(); |
240
|
|
|
|
241
|
|
|
$this->assertContains($option, $command); |
242
|
|
|
$this->assertContains('--debug=true', $command); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** +++++++++++++++++++++++++++++++++++ **/ |
246
|
|
|
/** ++++++++++ TEST ENTITIES ++++++++++ **/ |
247
|
|
|
/** +++++++++++++++++++++++++++++++++++ **/ |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Get client instance |
251
|
|
|
* |
252
|
|
|
* @return \JonnyW\PhantomJs\Engine |
253
|
|
|
*/ |
254
|
|
|
protected function getEngine() |
255
|
|
|
{ |
256
|
|
|
$engine = new Engine(); |
257
|
|
|
|
258
|
|
|
return $engine; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|