|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Swaggest\JsonDiff\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Swaggest\JsonDiff\Exception; |
|
6
|
|
|
use Swaggest\JsonDiff\JsonPatch; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @see https://github.com/json-patch/json-patch-tests |
|
10
|
|
|
*/ |
|
11
|
|
|
class SpecTest extends \PHPUnit_Framework_TestCase |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @dataProvider specTestsProvider |
|
15
|
|
|
*/ |
|
16
|
|
|
public function testSpecTests($case) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->doTest($case); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @dataProvider testsProvider |
|
23
|
|
|
*/ |
|
24
|
|
|
public function testTests($case) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->doTest($case); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
public function testsProvider() |
|
31
|
|
|
{ |
|
32
|
|
|
return $this->provider(__DIR__ . '/../assets/tests.json'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function specTestsProvider() |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->provider(__DIR__ . '/../assets/spec-tests.json'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
protected function provider($path) |
|
41
|
|
|
{ |
|
42
|
|
|
$cases = json_decode(file_get_contents($path)); |
|
43
|
|
|
|
|
44
|
|
|
$testCases = array(); |
|
45
|
|
|
foreach ($cases as $i => $case) { |
|
46
|
|
|
if (!isset($case->comment)) { |
|
47
|
|
|
$comment = 'unknown' . $i; |
|
48
|
|
|
} else { |
|
49
|
|
|
$comment = $case->comment; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$testCases[$comment] = array( |
|
53
|
|
|
'case' => $case, |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
return $testCases; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
protected function doTest($case) |
|
60
|
|
|
{ |
|
61
|
|
|
$case = clone $case; |
|
62
|
|
|
|
|
63
|
|
|
if (isset($case->disabled) && $case->disabled) { |
|
64
|
|
|
$this->markTestSkipped('test is disabled'); |
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (!is_object($case->doc)) { |
|
69
|
|
|
$doc = $case->doc; |
|
70
|
|
|
} else { |
|
71
|
|
|
$doc = clone $case->doc; |
|
72
|
|
|
} |
|
73
|
|
|
$patch = $case->patch; |
|
74
|
|
|
$hasExpected = array_key_exists('expected', (array)$case); |
|
75
|
|
|
$expected = isset($case->expected) ? $case->expected : null; |
|
76
|
|
|
$error = isset($case->error) ? $case->error : null; |
|
77
|
|
|
$jsonOptions = JSON_UNESCAPED_SLASHES + JSON_PRETTY_PRINT; |
|
78
|
|
|
|
|
79
|
|
|
try { |
|
80
|
|
|
$patch = JsonPatch::import($patch); |
|
81
|
|
|
$patch->apply($doc); |
|
82
|
|
|
if ($error !== null) { |
|
83
|
|
|
$this->fail('Error expected: ' . $error |
|
84
|
|
|
. "\n" . json_encode($case, $jsonOptions)); |
|
85
|
|
|
} |
|
86
|
|
|
if ($hasExpected) { |
|
87
|
|
|
$this->assertEquals($expected, $doc, json_encode($case, $jsonOptions) |
|
88
|
|
|
. "\n" . json_encode($doc, $jsonOptions)); |
|
89
|
|
|
} |
|
90
|
|
|
} catch (Exception $e) { |
|
91
|
|
|
if ($e->getCode() === Exception::EMPTY_PROPERTY_NAME_UNSUPPORTED) { |
|
92
|
|
|
$this->markTestSkipped('Empty property name unsupported in PHP <7.1'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if ($error === null) { |
|
96
|
|
|
$this->fail($e->getMessage() |
|
97
|
|
|
. "\n" . json_encode($case, $jsonOptions)); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
} |