Skip to content

Commit 16b0224

Browse files
committed
Tests from json-patch/json-patch-tests used.
1 parent 976fecc commit 16b0224

File tree

3 files changed

+738
-122
lines changed

3 files changed

+738
-122
lines changed

tests/PatchTest.php

Lines changed: 118 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ class PatchTest extends \PHPUnit_Framework_TestCase
1414
* @param mixed $data
1515
* @param array $patchData
1616
* @param mixed $expectedData
17-
* @dataProvider providerValidPatch_Result
17+
* @dataProvider providerValidSpecPatch_Result
1818
*/
19-
public function testApply_ValidPatch_Applied($data, array $patchData, $expectedData)
19+
public function testApply_ValidSpecPatch_Applied($data, array $patchData, $expectedData)
2020
{
2121
$dataWriter = new RawSelectableWriter($data);
2222
$patchDataReader = new RawSelectableReader($patchData);
@@ -25,124 +25,100 @@ public function testApply_ValidPatch_Applied($data, array $patchData, $expectedD
2525
}
2626

2727

28-
public function providerValidPatch_Result(): array
28+
public function providerValidSpecPatch_Result(): array
2929
{
30-
return [
31-
'A.1' => [
32-
(object) ['foo' => 'bar'],
33-
[
34-
(object) ['op' => 'add', 'path' => '/baz', 'value' => 'qux'],
35-
],
36-
(object) ['baz' => 'qux', 'foo' => 'bar'],
37-
],
38-
'A.2' => [
39-
(object) ['foo' => ['bar', 'baz']],
40-
[
41-
(object) ['op' => 'add', 'path' => '/foo/1', 'value' => 'qux'],
42-
],
43-
(object) ['foo' => ['bar', 'qux', 'baz']],
44-
],
45-
'A.3' => [
46-
(object) ['baz' => 'qux', 'foo' => 'bar'],
47-
[
48-
(object) ['op' => 'remove', 'path' => '/baz'],
49-
],
50-
(object) ['foo' => 'bar'],
51-
],
52-
'A.4' => [
53-
(object) ['foo' => ['bar', 'qux', 'baz']],
54-
[
55-
(object) ['op' => 'remove', 'path' => '/foo/1'],
56-
],
57-
(object) ['foo' => ['bar', 'baz']],
58-
],
59-
'A.5' => [
60-
(object) ['baz' => 'qux', 'foo' => 'bar'],
61-
[
62-
(object) ['op' => 'replace', 'path' => '/baz', 'value' => 'boo'],
63-
],
64-
(object) ['baz' => 'boo', 'foo' => 'bar'],
65-
],
66-
'A.6' => [
67-
(object) [
68-
'foo' => (object) ['bar' => 'baz', 'waldo' => 'fred'],
69-
'qux' => (object) ['corge' => 'grault'],
70-
],
71-
[
72-
(object) ['op' => 'move', 'from' => '/foo/waldo', 'path' => '/qux/thud'],
73-
],
74-
(object) [
75-
'foo' => (object) ['bar' => 'baz'],
76-
'qux' => (object) ['corge' => 'grault', 'thud' => 'fred'],
77-
],
78-
],
79-
'A.7' => [
80-
(object) ['foo' => ['all', 'grass', 'cows', 'eat']],
81-
[
82-
(object) ['op' => 'move', 'from' => '/foo/1', 'path' => '/foo/3'],
83-
],
84-
(object) ['foo' => ['all', 'cows', 'eat', 'grass']],
85-
],
86-
'A.10' => [
87-
(object) ['foo' => 'bar'],
88-
[
89-
(object) ['op' => 'add', 'path' => '/child', 'value' => (object) ['grandchild' => (object) []]],
90-
],
91-
(object) ['foo' => 'bar', 'child' => (object) ['grandchild' => (object) []]],
92-
],
93-
'A.11' => [
94-
(object) ['foo' => 'bar'],
95-
[
96-
(object) ['op' => 'add', 'path' => '/baz', 'value' => 'qux', 'xyz' => 123],
97-
],
98-
(object) ['foo' => 'bar', 'baz' => 'qux'],
99-
],
100-
'A.16' => [
101-
(object) ['foo' => ['bar']],
102-
[
103-
(object) ['op' => 'add', 'path' => '/foo/-', 'value' => ['abc', 'def']],
104-
],
105-
(object) ['foo' => ['bar', ['abc', 'def']]],
106-
],
107-
];
30+
$dataSetList = [];
31+
foreach ($this->getSpecTests() as $testInfo) {
32+
if (isset($testInfo->error) || !isset($testInfo->expected)) {
33+
continue;
34+
}
35+
$dataSet = [
36+
$testInfo->doc,
37+
$testInfo->patch,
38+
$testInfo->expected,
39+
];
40+
if (isset($testInfo->comment)) {
41+
$dataSetList[$testInfo->comment] = $dataSet;
42+
} else {
43+
$dataSetList[] = $dataSet;
44+
}
45+
}
46+
return $dataSetList;
10847
}
10948

11049

11150
/**
112-
* @param $data
51+
* @param mixed $data
11352
* @param array $patchData
114-
* @dataProvider providerValidPatch_Success
53+
* @dataProvider providerInvalidPatch
54+
* @expectedException \Exception
11555
*/
116-
public function testApply_ValidPatch_Success($data, array $patchData)
56+
public function testApply_InvalidSpecPatch_ExceptionThrown($data, array $patchData)
11757
{
11858
$dataWriter = new RawSelectableWriter($data);
11959
$patchDataReader = new RawSelectableReader($patchData);
12060
(new Patch($dataWriter))->apply($patchDataReader);
12161
}
12262

12363

124-
public function providerValidPatch_Success(): array
64+
public function providerInvalidSpecPatch(): array
12565
{
126-
return [
127-
'A.8' => [
128-
(object) ['baz' => 'qux', 'foo' => ["a", 2, "c"]],
129-
[
130-
(object) ['op' => 'test', 'path' => '/baz', 'value' => 'qux'],
131-
(object) ['op' => 'test', 'path' => '/foo/1', 'value' => 2],
132-
],
133-
],
134-
'A.14' => [
135-
(object) ['/' => 9, '~1' => 10],
136-
[
137-
(object) ['op' => 'test', 'path' => '/~01', 'value' => 10],
138-
],
139-
],
140-
];
66+
$dataSetList = [];
67+
foreach ($this->getSpecTests() as $testInfo) {
68+
if (!isset($testInfo->error)) {
69+
continue;
70+
}
71+
$dataSet = [
72+
$testInfo->doc,
73+
$testInfo->patch,
74+
];
75+
if (isset($testInfo->comment)) {
76+
$dataSetList[$testInfo->comment] = $dataSet;
77+
} else {
78+
$dataSetList[] = $dataSet;
79+
}
80+
}
81+
return $dataSetList;
14182
}
14283

84+
/**
85+
* @param mixed $data
86+
* @param array $patchData
87+
* @param mixed $expectedData
88+
* @dataProvider providerValidPatch_Result
89+
*/
90+
public function testApply_ValidPatch_Applied($data, array $patchData, $expectedData)
91+
{
92+
$dataWriter = new RawSelectableWriter($data);
93+
$patchDataReader = new RawSelectableReader($patchData);
94+
(new Patch($dataWriter))->apply($patchDataReader);
95+
$this->assertEquals($expectedData, $data);
96+
}
97+
98+
99+
public function providerValidPatch_Result(): array
100+
{
101+
$dataSetList = [];
102+
foreach ($this->getTests() as $testInfo) {
103+
if (isset($testInfo->error) || !isset($testInfo->expected)) {
104+
continue;
105+
}
106+
$dataSet = [
107+
$testInfo->doc,
108+
$testInfo->patch,
109+
$testInfo->expected,
110+
];
111+
if (isset($testInfo->comment)) {
112+
$dataSetList[$testInfo->comment] = $dataSet;
113+
} else {
114+
$dataSetList[] = $dataSet;
115+
}
116+
}
117+
return $dataSetList;
118+
}
143119

144120
/**
145-
* @param $data
121+
* @param mixed $data
146122
* @param array $patchData
147123
* @dataProvider providerInvalidPatch
148124
* @expectedException \Exception
@@ -157,26 +133,46 @@ public function testApply_InvalidPatch_ExceptionThrown($data, array $patchData)
157133

158134
public function providerInvalidPatch(): array
159135
{
160-
return [
161-
'A.9' => [
162-
(object) ['baz' => 'qux'],
163-
[
164-
(object) ['op' => 'test', 'path' => '/baz', 'value' => 'bar'],
165-
],
166-
],
167-
'A.12' => [
168-
(object) ['foo' => 'bar'],
169-
[
170-
(object) ['op' => 'add', 'path' => '/baz/bat', 'value' => 'qux'],
171-
],
172-
],
173-
// A.13 cannot be tested.
174-
'A.14' => [
175-
(object) ['/' => 9, '~1' => 10],
176-
[
177-
(object) ['op' => 'test', 'path' => '/~01', 'value' => "10"],
178-
],
179-
],
180-
];
136+
$dataSetList = [];
137+
foreach ($this->getTests() as $testInfo) {
138+
if (!isset($testInfo->error)) {
139+
continue;
140+
}
141+
$dataSet = [
142+
$testInfo->doc,
143+
$testInfo->patch,
144+
];
145+
if (isset($testInfo->comment)) {
146+
$dataSetList[$testInfo->comment] = $dataSet;
147+
} else {
148+
$dataSetList[] = $dataSet;
149+
}
150+
}
151+
return $dataSetList;
152+
}
153+
154+
155+
private function getSpecTests(): array
156+
{
157+
$testsFile = dirname(realpath(PHPUNIT_COMPOSER_INSTALL)) . "/../tests/data/spec_tests.json";
158+
return $this->getTestInfoList($testsFile);
159+
}
160+
161+
162+
private function getTests(): array
163+
{
164+
$testsFile = dirname(realpath(PHPUNIT_COMPOSER_INSTALL)) . "/../tests/data//tests.json";
165+
return $this->getTestInfoList($testsFile);
166+
}
167+
168+
169+
private function getTestInfoList($fileName): array
170+
{
171+
$testInfoListJSON = file_get_contents($fileName);
172+
$testInfoList = json_decode($testInfoListJSON);
173+
$isNotDisabledTest = function(\stdClass $testInfo) {
174+
return !(isset($testInfo->disabled) && $testInfo->disabled);
175+
};
176+
return array_filter($testInfoList, $isNotDisabledTest);
181177
}
182178
}

0 commit comments

Comments
 (0)