1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* apparat-dev |
5
|
|
|
* |
6
|
|
|
* @category Apparat |
7
|
|
|
* @package Apparat\Dev |
8
|
|
|
* @subpackage Apparat\Dev\Tests |
9
|
|
|
* @author Joschi Kuphal <[email protected]> / @jkphl |
10
|
|
|
* @copyright Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
11
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License (MIT) |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/*********************************************************************************** |
15
|
|
|
* The MIT License (MIT) |
16
|
|
|
* |
17
|
|
|
* Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl |
18
|
|
|
* |
19
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
20
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
21
|
|
|
* the Software without restriction, including without limitation the rights to |
22
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
23
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
24
|
|
|
* subject to the following conditions: |
25
|
|
|
* |
26
|
|
|
* The above copyright notice and this permission notice shall be included in all |
27
|
|
|
* copies or substantial portions of the Software. |
28
|
|
|
* |
29
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
30
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
31
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
32
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
33
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
34
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
35
|
|
|
***********************************************************************************/ |
36
|
|
|
|
37
|
|
|
namespace Apparat\Dev\Tests; |
38
|
|
|
|
39
|
|
|
use Apparat\Dev\Module; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Basic tests for generic files |
43
|
|
|
* |
44
|
|
|
* @package Apparat\Dev |
45
|
|
|
* @subpackage Apparat\Dev\Tests |
46
|
|
|
*/ |
47
|
|
|
abstract class AbstractTest extends \PHPUnit_Framework_TestCase |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* Temporary files |
51
|
|
|
* |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected $tmpFiles = []; |
55
|
|
|
/** |
56
|
|
|
* Temporary directories |
57
|
|
|
* |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
protected $tmpDirectories = []; |
61
|
|
|
/** |
62
|
|
|
* This method is called before the first test of this test class is run. |
63
|
|
|
*/ |
64
|
|
|
public static function setUpBeforeClass() |
65
|
|
|
{ |
66
|
|
|
Module::autorun(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Tests if two arrays equal in their keys and values |
71
|
|
|
* |
72
|
|
|
* @param array $expected Expected result |
73
|
|
|
* @param array $actual Actual result |
74
|
|
|
* @param string $message Message |
75
|
|
|
*/ |
76
|
|
|
public function assertArrayEquals(array $expected, array $actual, $message = '') |
77
|
|
|
{ |
78
|
|
|
$this->assertEquals( |
79
|
|
|
$this->sortArrayForComparison($expected), |
80
|
|
|
$this->sortArrayForComparison($actual), |
81
|
|
|
$message |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Recursively sort an array for comparison with another array |
87
|
|
|
* |
88
|
|
|
* @param array $array Array |
89
|
|
|
* @return array Sorted array |
90
|
|
|
*/ |
91
|
|
|
protected function sortArrayForComparison(array $array) |
92
|
|
|
{ |
93
|
|
|
// Tests if all array keys are numeric |
94
|
|
|
$allNumeric = true; |
95
|
|
|
foreach (array_keys($array) as $key) { |
96
|
|
|
if (!is_numeric($key)) { |
97
|
|
|
$allNumeric = false; |
98
|
|
|
break; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// If not all keys are numeric: Sort the array by key |
103
|
|
|
if (!$allNumeric) { |
104
|
|
|
ksort($array, SORT_STRING); |
105
|
|
|
return $this->sortArrayRecursive($array); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Sort them by data type and value |
109
|
|
|
$array = $this->sortArrayRecursive($array); |
110
|
|
|
usort( |
111
|
|
|
$array, |
112
|
|
|
function ( |
113
|
|
|
$first, |
114
|
|
|
$second |
115
|
|
|
) { |
116
|
|
|
$aType = gettype($first); |
117
|
|
|
$bType = gettype($second); |
118
|
|
|
if ($aType === $bType) { |
119
|
|
|
switch ($aType) { |
120
|
|
|
case 'array': |
121
|
|
|
return strcmp(implode('', array_keys($first)), implode('', array_keys($second))); |
122
|
|
|
case 'object': |
123
|
|
|
return strcmp(spl_object_hash($first), spl_object_hash($second)); |
124
|
|
|
default: |
125
|
|
|
return strcmp(strval($first), strval($second)); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return strcmp($aType, $bType); |
130
|
|
|
} |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
return $array; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Recursively sort an array for comparison |
138
|
|
|
* |
139
|
|
|
* @param array $array Original array |
140
|
|
|
* @return array Sorted array |
141
|
|
|
*/ |
142
|
|
|
protected function sortArrayRecursive(array $array) |
143
|
|
|
{ |
144
|
|
|
|
145
|
|
|
// Run through all elements and sort them recursively if they are an array |
146
|
|
|
reset($array); |
147
|
|
|
while (list($key, $value) = each($array)) { |
148
|
|
|
if (is_array($value)) { |
149
|
|
|
$array[$key] = $this->sortArrayForComparison($value); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $array; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Tears down the fixture |
158
|
|
|
*/ |
159
|
|
|
protected function tearDown() |
160
|
|
|
{ |
161
|
|
|
foreach ($this->tmpDirectories as $tmpDirectory) { |
162
|
|
|
$this->scanTemporaryDirectory($tmpDirectory); |
163
|
|
|
} |
164
|
|
|
foreach (array_reverse($this->tmpFiles) as $tmpFile) { |
165
|
|
|
@is_file($tmpFile) ? @unlink($tmpFile) : @rmdir($tmpFile); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Prepare and register a temporary file name |
171
|
|
|
* |
172
|
|
|
* @return string Temporary file name |
173
|
|
|
*/ |
174
|
|
|
protected function createTemporaryFileName() |
175
|
|
|
{ |
176
|
|
|
$tempFileName = $this->createTemporaryFile(); |
177
|
|
|
unlink($tempFileName); |
178
|
|
|
return $tempFileName; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Prepare and register a temporary file |
183
|
|
|
* |
184
|
|
|
* @return string Temporary file name |
185
|
|
|
*/ |
186
|
|
|
protected function createTemporaryFile() |
187
|
|
|
{ |
188
|
|
|
return $this->tmpFiles[] = tempnam(sys_get_temp_dir(), 'apparat_test_'); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Register a temporary directory that needs to be deleted recursively on shutdown |
193
|
|
|
* |
194
|
|
|
* @param string $directory Directory |
195
|
|
|
* @return string Directory |
196
|
|
|
*/ |
197
|
|
|
protected function registerTemporaryDirectory($directory) |
198
|
|
|
{ |
199
|
|
|
return $this->tmpDirectories[] = $this->tmpFiles[] = $directory; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Scan a temporary directory and register all files and subdirectories (recursively) |
204
|
|
|
* |
205
|
|
|
* @param string $directory Directory |
206
|
|
|
*/ |
207
|
|
|
protected function scanTemporaryDirectory($directory) |
208
|
|
|
{ |
209
|
|
|
foreach (scandir($directory) as $fileOrDirectory) { |
210
|
|
|
if ($fileOrDirectory !== '.' && $fileOrDirectory !== '..' && !is_link($fileOrDirectory)) { |
211
|
|
|
$fileOrDirectory = $directory.DIRECTORY_SEPARATOR.$fileOrDirectory; |
212
|
|
|
$this->tmpFiles[] = $fileOrDirectory; |
213
|
|
|
if (is_dir($fileOrDirectory)) { |
214
|
|
|
$this->scanTemporaryDirectory($fileOrDirectory); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Normalize HTML contents |
222
|
|
|
* |
223
|
|
|
* @param string $html Original HTML |
224
|
|
|
* @return string Normalized HTML |
225
|
|
|
*/ |
226
|
|
|
protected function normalizeHtml($html) |
227
|
|
|
{ |
228
|
|
|
$htmlDom = new \DOMDocument(); |
229
|
|
|
$htmlDom->preserveWhiteSpace = false; |
230
|
|
|
$htmlDom->formatOutput = false; |
231
|
|
|
$htmlDom->loadXML("<html><head><title>apparat</title></head><body>$html</body></html>"); |
232
|
|
|
return $htmlDom->saveXML(); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|