|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the JVal package. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace JVal\Testing; |
|
11
|
|
|
|
|
12
|
|
|
use JVal\Utils; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Provides common methods for dealing with JSON data (loading, assertions, |
|
16
|
|
|
* etc.). |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class BaseTestCase extends \PHPUnit_Framework_TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
private $expectException = false; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Wraps the default #runTest() method to provide an exception hook. |
|
24
|
|
|
* |
|
25
|
|
|
* @return mixed|null |
|
26
|
|
|
* |
|
27
|
|
|
* @throws \Exception |
|
28
|
|
|
*/ |
|
29
|
617 |
|
protected function runTest() |
|
30
|
|
|
{ |
|
31
|
|
|
try { |
|
32
|
617 |
|
$result = parent::runTest(); |
|
33
|
617 |
|
} catch (\Exception $ex) { |
|
34
|
61 |
|
$this->exceptionHook($ex); |
|
35
|
|
|
|
|
36
|
61 |
|
return; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// @codeCoverageIgnoreStart |
|
40
|
|
|
if ($this->expectException) { |
|
41
|
|
|
$this->fail('An exception was expected but none has been thrown.'); |
|
42
|
|
|
} |
|
43
|
|
|
// @codeCoverageIgnoreEnd |
|
44
|
|
|
|
|
45
|
556 |
|
return $result; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Sets the flag indicating that an exception is expected. |
|
50
|
|
|
*/ |
|
51
|
61 |
|
protected function expectException() |
|
52
|
|
|
{ |
|
53
|
61 |
|
$this->expectException = true; |
|
54
|
61 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @codeCoverageIgnore (shouldn't happen in a green test suite) |
|
58
|
|
|
* |
|
59
|
|
|
* Hook called when an unexpected exception is thrown. |
|
60
|
|
|
* |
|
61
|
|
|
* Override this hook to make custom assertions on exceptions. |
|
62
|
|
|
* |
|
63
|
|
|
* @param \Exception $ex |
|
64
|
|
|
* |
|
65
|
|
|
* @throws \Exception |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function exceptionHook(\Exception $ex) |
|
68
|
|
|
{ |
|
69
|
|
|
throw $ex; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Returns the JSON-decoded content of a file. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $file |
|
76
|
|
|
* |
|
77
|
|
|
* @return mixed |
|
78
|
|
|
*/ |
|
79
|
93 |
|
protected function loadJsonFromFile($file) |
|
80
|
|
|
{ |
|
81
|
93 |
|
return Utils::loadJsonFromFile($file); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Returns a JSON-decoded schema from tests/Data/schemas. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $name Name of the file without the extension |
|
88
|
|
|
* |
|
89
|
|
|
* @return mixed |
|
90
|
|
|
*/ |
|
91
|
93 |
|
protected function loadSchema($name) |
|
92
|
|
|
{ |
|
93
|
93 |
|
$schemaDir = realpath(__DIR__.'/../../tests/Data/schemas'); |
|
94
|
|
|
|
|
95
|
93 |
|
return $this->loadJsonFromFile("{$schemaDir}/{$name}.json"); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Asserts the validation results equal the expected one and make |
|
100
|
|
|
* a full report otherwise. |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $file |
|
103
|
|
|
* @param string $title |
|
104
|
|
|
* @param mixed $instance |
|
105
|
|
|
* @param \stdClass $schema |
|
106
|
|
|
* @param bool $isInstanceValid |
|
107
|
|
|
* @param array $expectedErrors |
|
108
|
|
|
* @param array $actualErrors |
|
109
|
|
|
*/ |
|
110
|
478 |
|
protected function assertValidationResult( |
|
111
|
|
|
$file, |
|
112
|
|
|
$title, |
|
113
|
|
|
$instance, |
|
114
|
|
|
\stdClass $schema, |
|
115
|
|
|
$isInstanceValid, |
|
116
|
|
|
array $expectedErrors, |
|
117
|
|
|
array $actualErrors |
|
118
|
|
|
) { |
|
119
|
|
|
$reportParameters = array( |
|
120
|
478 |
|
$file, |
|
121
|
478 |
|
$title, |
|
122
|
478 |
|
$this->dump($schema), |
|
123
|
478 |
|
$this->dump($instance), |
|
124
|
478 |
|
count($expectedErrors) > 0 ? $this->dump($expectedErrors) : 'no error', |
|
125
|
478 |
|
count($actualErrors) > 0 ? $this->dump($actualErrors) : 'no error', |
|
126
|
478 |
|
); |
|
127
|
|
|
|
|
128
|
478 |
|
if (!$isInstanceValid && count($expectedErrors) === 0) { |
|
129
|
153 |
|
$reportParameters[4] = 'at least one error'; |
|
130
|
153 |
|
$this->assertHasError($actualErrors, $reportParameters); |
|
131
|
153 |
|
} else { |
|
132
|
325 |
|
$this->assertErrorsAreEqual($expectedErrors, $actualErrors, $reportParameters); |
|
133
|
|
|
} |
|
134
|
478 |
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Returns a mock object, bypassing original constructor. |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $class |
|
140
|
|
|
* |
|
141
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject |
|
142
|
|
|
*/ |
|
143
|
83 |
|
protected function mock($class) |
|
144
|
|
|
{ |
|
145
|
83 |
|
return $this->getMockBuilder($class) |
|
146
|
83 |
|
->disableOriginalConstructor() |
|
147
|
83 |
|
->getMock(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Returns URI to local file system. |
|
152
|
|
|
* |
|
153
|
|
|
* @param string $path |
|
154
|
|
|
* |
|
155
|
|
|
* @return string |
|
156
|
|
|
*/ |
|
157
|
298 |
|
protected function getLocalUri($path) |
|
158
|
|
|
{ |
|
159
|
298 |
|
if (preg_match('/^[A-Z]:/', $path)) { |
|
160
|
|
|
$path = '/' . strtr($path, '\\', '/'); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
298 |
|
return 'file://' . $path; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @codeCoverageIgnore (cannot cover code whose behaviour depends on PHP version) |
|
168
|
|
|
* |
|
169
|
|
|
* @param mixed $variable |
|
170
|
|
|
* |
|
171
|
|
|
* @return string |
|
172
|
|
|
*/ |
|
173
|
|
|
private function dump($variable) |
|
174
|
|
|
{ |
|
175
|
|
|
if (defined('JSON_PRETTY_PRINT')) { |
|
176
|
|
|
$options = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES; |
|
177
|
|
|
|
|
178
|
|
|
if (defined('JSON_PRESERVE_ZERO_FRACTION')) { |
|
179
|
|
|
$options |= JSON_PRESERVE_ZERO_FRACTION; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
$output = @json_encode($variable, $options); |
|
183
|
|
|
|
|
184
|
|
|
if (json_last_error() === JSON_ERROR_NONE) { |
|
185
|
|
|
return $output; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return print_r($variable, true); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
153 |
|
private function assertHasError(array $errors, array $reportParameters) |
|
193
|
|
|
{ |
|
194
|
|
|
// @codeCoverageIgnoreStart |
|
195
|
|
|
if (count($errors) === 0) { |
|
196
|
|
|
$this->fail(vsprintf($this->getFailureReportMask(), $reportParameters)); |
|
197
|
|
|
} |
|
198
|
|
|
// @codeCoverageIgnoreEnd |
|
199
|
153 |
|
} |
|
200
|
|
|
|
|
201
|
325 |
|
private function assertErrorsAreEqual(array $actual, array $expected, array $reportParameters) |
|
202
|
|
|
{ |
|
203
|
325 |
|
$report = vsprintf($this->getFailureReportMask(), $reportParameters); |
|
204
|
|
|
|
|
205
|
|
|
// @codeCoverageIgnoreStart |
|
206
|
|
|
if (count($actual) !== count($expected)) { |
|
207
|
|
|
$this->fail($report); |
|
208
|
|
|
} |
|
209
|
|
|
// @codeCoverageIgnoreEnd |
|
210
|
|
|
|
|
211
|
325 |
|
foreach ($expected as $error) { |
|
212
|
|
|
// @codeCoverageIgnoreStart |
|
213
|
|
|
if (!in_array($error, $actual)) { |
|
214
|
|
|
$this->fail($report); |
|
215
|
|
|
} |
|
216
|
|
|
// @codeCoverageIgnoreEnd |
|
217
|
325 |
|
} |
|
218
|
325 |
|
} |
|
219
|
|
|
|
|
220
|
325 |
|
private function getFailureReportMask() |
|
221
|
|
|
{ |
|
222
|
|
|
return <<<MSG |
|
223
|
|
|
********************************************************************** |
|
224
|
|
|
|
|
225
|
|
|
File: %s |
|
226
|
|
|
|
|
227
|
|
|
Test: %s |
|
228
|
|
|
|
|
229
|
|
|
Schema: %s |
|
230
|
|
|
|
|
231
|
|
|
Instance: %s |
|
232
|
|
|
|
|
233
|
|
|
Expected: %s |
|
234
|
|
|
|
|
235
|
|
|
Actual: %s |
|
236
|
|
|
|
|
237
|
|
|
********************************************************************** |
|
238
|
325 |
|
MSG; |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|