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
|
|
|
/** |
13
|
|
|
* Test case for testing against actual JSON data stored in dedicated files. |
14
|
|
|
* Format examples can be found in the tests/Data/constraints directory. |
15
|
|
|
*/ |
16
|
|
|
abstract class DataTestCase extends BaseTestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @codeCoverageIgnore (data provider is executed before test is launched) |
20
|
|
|
* |
21
|
|
|
* Data provider collecting test cases from JSON files. |
22
|
|
|
*/ |
23
|
|
|
public function fileDataProvider() |
24
|
|
|
{ |
25
|
|
|
$caseDir = realpath($this->getDataDirectory()); |
26
|
|
|
$caseNames = $this->getCaseFileNames(); |
27
|
|
|
$caseExt = $caseNames ? '.json' : ''; |
28
|
|
|
$caseNames = $caseNames ?: array_diff(scandir($caseDir), ['..', '.']); |
29
|
|
|
$tests = []; |
30
|
|
|
|
31
|
|
|
foreach ($caseNames as $caseName) { |
32
|
|
|
$caseFile = "{$caseDir}/{$caseName}{$caseExt}"; |
33
|
|
|
$case = $this->loadJsonFromFile($caseFile); |
34
|
|
|
|
35
|
|
|
foreach ($case->tests as $test) { |
36
|
|
|
if (!isset($test->valid) && !isset($test->invalid)) { |
37
|
|
|
throw new \Exception(sprintf( |
38
|
|
|
'Test case "%s %s" has neither "valid" or "invalid" data (file: %s)', |
39
|
|
|
$case->title, |
40
|
|
|
$test->title, |
41
|
|
|
$caseFile |
42
|
|
|
)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (isset($test->valid)) { |
46
|
|
|
foreach ($test->valid as $i => $instance) { |
47
|
|
|
$tests[] = [ |
48
|
|
|
$caseFile, |
49
|
|
|
"{$case->title} {$test->title}, valid instance #{$i}", |
50
|
|
|
$instance, |
51
|
|
|
clone $test->schema, |
52
|
|
|
true, |
53
|
|
|
[], |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (isset($test->invalid)) { |
59
|
|
|
foreach ($test->invalid as $i => $set) { |
60
|
|
|
if (!isset($set->violations)) { |
61
|
|
|
throw new \Exception(sprintf( |
62
|
|
|
'Invalid test must have a "violations" property in %s', |
63
|
|
|
$caseFile |
64
|
|
|
)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$tests[] = [ |
68
|
|
|
$caseFile, |
69
|
|
|
"{$case->title} {$test->title}, invalid instance #{$i}", |
70
|
|
|
$set->instance, |
71
|
|
|
$test->schema, |
72
|
|
|
false, |
73
|
|
|
array_map(function ($violation) { |
74
|
|
|
return (array) $violation; |
75
|
|
|
}, $set->violations), |
76
|
|
|
]; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $tests; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns the path the test data directory. |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
abstract protected function getDataDirectory(); |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns the names of the test cases to be loaded. If no names are |
94
|
|
|
* returned, all the cases found in the data directory will be loaded. |
95
|
|
|
* |
96
|
|
|
* @return mixed |
97
|
|
|
*/ |
98
|
|
|
abstract protected function getCaseFileNames(); |
99
|
|
|
} |
100
|
|
|
|