/
Driver.php
209 lines (168 loc) · 6.06 KB
/
Driver.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* Driver for creating and running a PhantomJS session.
*/
class PHPUnit_Extensions_Phantom_Driver
{
private $tests;
// PHPUnit_Framework_TestCase
private $testcase;
public function __construct($testcase)
{
$this->testcase = $testcase;
$this->tests = array();
}
public function assertTrue($name, $condition)
{
$this->tests[] = new PHPUnit_Extensions_Phantom_Assertion($name, $condition);
}
public function assertEquals($name, $condition, $value)
{
$this->tests[] = new PHPUnit_Extensions_Phantom_Assertion($name, json_encode($value) . " == $condition");
}
public function assertEqualsNot($name, $condition, $value)
{
$this->tests[] = new PHPUnit_Extensions_Phantom_Assertion($name, json_encode($value) . " != $condition");
}
/*public function exec($name, $payloadToExecute)
{
$this->tests[] = new PHPUnit_Extensions_Phantom_PayloadExecute($name, $payloadToExecute);
}*/
/**
* Test uses Phantom's page.evaluate() for code evaluation.
* All assertions are already transformed into PhantomJS javascript code.
*
* @param type $page_content
*/
public function test($page_content)
{
$page_content = json_encode($page_content);
$output = <<<EOT
var page = require('webpage').create();
page.content = $page_content;
page.onLoadFinished = function (status) {
try {
if (status == 'success') {
var ua = page.evaluate(function () {
var tests = [];
EOT;
foreach ($this->tests as $test) {
$output .= "\n" . $test->getJavascript();
}
$output .= <<<EOT
return tests;
});
for (var i = 0, il = ua.length; i < il; i++) {
var result = ua[i];
console.log(result.name + ' [' + (result.pass ? 'OK' : 'FAIL') + ']');
}
} else {
console.log('all [FAIL]');
}
} catch (e) {
console.log(e + ' [FAIL]');
}
phantom.exit();
};
EOT;
//echo $output;
$testResult = $this->executeTemporaryTestFile($output);
$lines = explode("\n", $testResult);
foreach ($lines as $line) {
if (empty($line) === true) {
continue;
}
$found_key = false;
$number_of_tests = count($this->tests);
for ($i = 0; $i < $number_of_tests; $i++) {
$test = $this->tests[$i];
if (preg_match('/(.*) \[((?:OK)|(?:FAIL))\]/', $line, $matches)) {
if ($test->getName() == $matches[1]) {
$test->setSuccess($matches[2] == "OK");
$this->tests[$i] = $test;
$found_key = true;
}
}
}
if (!$found_key) {
$this->testcase->fail($line); // = PHPUnit_Framework_TestCase->fail()
}
}
foreach ($this->tests as $test) {
$this->testcase->assertTrue($test->isSuccess(), $test->getName());
}
}
/**
* Creates a temporary JS file, executes it with PhantomJS, then deletes it!
*
* @param string $content The JS Test Content.
* @return string Test Result (stdout) of PhantomJS Test Run.
*/
private function executeTemporaryTestFile($content)
{
$file = tempnam(sys_get_temp_dir(), "phantomjs_" . uniqid());
// @codeCoverageIgnoreStart
if (false === $file) {
throw new \RuntimeException('Could not create temp file. Check temp directory permissions.');
}
// @codeCoverageIgnoreEnd
file_put_contents($file, $content);
$stdout = self::executePhantomJS($file);
unlink($file);
return $stdout;
}
/**
* Executes a PhantomJS test file.
* The call invokes "phantomjs [options] [file] [arg1 [arg2 [...]]]".
*
* For linux system "phantomjs" is expected to be on path.
* You face no problems on Travis-CI, where it is preinstalled and on path.
*
* Find the list of CLI Options here:
* @link https://github.com/ariya/phantomjs/wiki/API-Reference#command-line-options
*
* @param string $testFile The PhantomJS file you wish to execute.
* @param type $args CLI Args
* @param type $options CLI Options
* @return string Test Result (stdout)
*/
public static function executePhantomJS($testFile, $args = null, $options = null)
{
// we are here "phpunit-headless/src/PHPUnit/Extensions/Phantom/Driver.php"
// and will go 4 folders up, to get to the root folder "phpunit-headless/"
$bin_dir = dirname(dirname(dirname(dirname(__DIR__))));
// determine PhantomJS binary, take windows into account
if (DIRECTORY_SEPARATOR === '\\') {
$cmd = $bin_dir . "\bin\phantomjs.exe";
} else {
$cmd = $bin_dir . "/bin/phantomjs";
}
// test existence of PhantomJS binary
if (is_file($cmd) === false) {
throw new Exception(
'The PhantomJS binary was not found! ' .
'Place it either on the environment path or into the /bin folder of your project.'
);
}
// test executable permission of PhantomJS binary
if (is_executable($cmd) === false) {
throw new Exception(
'The PhantomJS binary was found! But it is not executable. Check permission.'
);
}
/**
* Construct the PhantomJS command
*
* phantomjs [options] somescript.js [arg1 [arg2 [...]]]
*/
// options
$cmd .= (isset($options) === true) ? " " . escapeshellarg($options) : '';
// javascript filename to execute
$cmd .= " " . escapeshellarg($testFile);
// arguments
$cmd .= (isset($args) === true) ? " " . escapeshellarg($args) : '';
// execute cmd
$stdout = shell_exec($cmd);
return $stdout;
}
}