Skip to content

Commit 564c349

Browse files
committed
Test suite was refactored and tests about atomic capabilities of the tested libraries was added as part of the JsonPatchCompliance.php
1 parent b6446ae commit 564c349

8 files changed

+224
-29
lines changed

tests/Blancks_fast_jsonpatchTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<?php declare(strict_types=1);
22

33
namespace blancks\JsonPatchBenchmarkTests;
4-
54
use PHPUnit\Framework\Attributes\DataProvider;
65

7-
class Blancks_fast_jsonpatchTest extends JsonPatchComplianceTest
6+
class Blancks_fast_jsonpatchTest extends JsonPatchCompliance
87
{
98
#[DataProvider('validOperationsProvider')]
109
public function testJsonPatchCompliance(string $json, string $patch, string $expected): void
@@ -14,4 +13,21 @@ public function testJsonPatchCompliance(string $json, string $patch, string $exp
1413
\blancks\JsonPatch\FastJsonPatch::apply($json, $patch)
1514
);
1615
}
16+
17+
#[DataProvider('atomicOperationsProvider')]
18+
public function testAtomicOperations(string $json, string $patch, string $expected): void
19+
{
20+
$document = json_decode($json);
21+
22+
try {
23+
\blancks\JsonPatch\FastJsonPatch::applyByReference($document, json_decode($patch));
24+
} catch (\Throwable) {
25+
// expecting some error
26+
}
27+
28+
$this->assertSame(
29+
$this->normalizeJson($expected),
30+
$this->normalizeJson(json_encode($document))
31+
);
32+
}
1733
}

tests/Gamringer_php_json_patchTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use PHPUnit\Framework\Attributes\DataProvider;
66

7-
class Gamringer_php_json_patchTest extends JsonPatchComplianceTest
7+
class Gamringer_php_json_patchTest extends JsonPatchCompliance
88
{
99
#[DataProvider('validOperationsProvider')]
1010
public function testJsonPatchCompliance(string $json, string $patch, string $expected): void
@@ -18,4 +18,22 @@ public function testJsonPatchCompliance(string $json, string $patch, string $exp
1818
$this->normalizeJson(json_encode($document))
1919
);
2020
}
21+
22+
#[DataProvider('atomicOperationsProvider')]
23+
public function testAtomicOperations(string $json, string $patch, string $expected): void
24+
{
25+
$document = json_decode($json);
26+
27+
try {
28+
$Patch = \gamringer\JSONPatch\Patch::fromJSON($patch);
29+
$Patch->apply($document);
30+
} catch (\Throwable) {
31+
// expecting some error
32+
}
33+
34+
$this->assertSame(
35+
$this->normalizeJson($expected),
36+
$this->normalizeJson(json_encode($document))
37+
);
38+
}
2139
}

tests/JsonPatchComplianceTest.php renamed to tests/JsonPatchCompliance.php

Lines changed: 83 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,93 @@
44

55
use PHPUnit\Framework\TestCase;
66

7-
abstract class JsonPatchComplianceTest extends TestCase
7+
abstract class JsonPatchCompliance extends TestCase
88
{
9+
public static function atomicOperationsProvider(): array
10+
{
11+
return [
12+
'Atomic Operations, object: ADD' => [
13+
'{"foo":"Hello"}',
14+
'[{"op": "add", "path": "/bar", "value": "World"}, {"op": "test", "path": "/a", "value": ""}]',
15+
'{"foo":"Hello"}',
16+
],
17+
'Atomic Operations, object: REPLACE' => [
18+
'{"foo":"Hello"}',
19+
'[{"op": "replace", "path": "/foo", "value": "Hello World"}, {"op": "test", "path": "/a", "value": ""}]',
20+
'{"foo":"Hello"}',
21+
],
22+
'Atomic Operations, object: COPY' => [
23+
'{"foo":"Hello"}',
24+
'[{"op": "copy", "from": "/foo", "path": "/bar"}, {"op": "test", "path": "/a", "value": ""}]',
25+
'{"foo":"Hello"}',
26+
],
27+
'Atomic Operations, object: MOVE' => [
28+
'{"foo":"Hello"}',
29+
'[{"op": "move", "from": "/foo", "path": "/bar"}, {"op": "test", "path": "/a", "value": ""}]',
30+
'{"foo":"Hello"}',
31+
],
32+
'Atomic Operations, object: REMOVE' => [
33+
'{"foo":"Hello"}',
34+
'[{"op": "remove", "path": "/foo"}, {"op": "test", "path": "/a", "value": ""}]',
35+
'{"foo":"Hello"}',
36+
],
37+
'Atomic Operations, array: ADD' => [
38+
'[]',
39+
'[{"op": "add", "path": "/-", "value": "World"}, {"op": "test", "path": "/a", "value": ""}]',
40+
'[]',
41+
],
42+
'Atomic Operations, array: REPLACE' => [
43+
'["Hello"]',
44+
'[{"op": "replace", "path": "/0", "value": "Hello World"}, {"op": "test", "path": "/a", "value": ""}]',
45+
'["Hello"]',
46+
],
47+
'Atomic Operations, array: COPY' => [
48+
'["Hello"]',
49+
'[{"op": "copy", "from": "/0", "path": "/1"}, {"op": "test", "path": "/a", "value": ""}]',
50+
'["Hello"]',
51+
],
52+
'Atomic Operations, array: MOVE' => [
53+
'[["Hello"],[]]',
54+
'[{"op": "move", "from": "/0/0", "path": "/1/0"}, {"op": "test", "path": "/a", "value": ""}]',
55+
'[["Hello"],[]]',
56+
],
57+
'Atomic Operations, array: REMOVE' => [
58+
'["Hello"]',
59+
'[{"op": "remove", "path": "/0"}, {"op": "test", "path": "/a", "value": ""}]',
60+
'["Hello"]',
61+
],
62+
'Atomic operations, mixed' => [
63+
'{"foo":[{"bar":"start"}]}',
64+
'[{"op": "add", "path": "/foo/-", "value": "Hello"},
65+
{"op": "add", "path": "/foo/0/foobar", "value": "Hello"},
66+
{"op": "add", "path": "/foo/0/bar", "value": "World"},
67+
{"op": "remove", "path": "/foo/0/foobar"},
68+
{"op": "copy", "from": "/foo/0", "path": "/foo/-"},
69+
{"op": "replace", "path": "/foo", "value": "Hello World"},
70+
{"op": "test", "path": "/bar", "value": "Hello World"}]',
71+
'{"foo":[{"bar":"start"}]}'
72+
],
73+
];
74+
}
75+
976
public static function validOperationsProvider(): array
1077
{
1178
return [
12-
/*
13-
* Some classes handles empty patch by throwing an exception.
14-
* I'll avoid these test cases to be as fair as possible
15-
*
16-
* 'Empty patch against empty document' => [
17-
* '{}',
18-
* '[]',
19-
* '{}'
20-
* ],
21-
* 'Empty patch against non-empty document' => [
22-
* '{"foo": 1}',
23-
* '[]',
24-
* '{"foo": 1}'
25-
* ],
26-
* 'Empty patch against top-level array document' => [
27-
* '["foo"]',
28-
* '[]',
29-
* '["foo"]'
30-
* ],
31-
*/
79+
'Empty patch against empty document' => [
80+
'{}',
81+
'[]',
82+
'{}'
83+
],
84+
'Empty patch against non-empty document' => [
85+
'{"foo": 1}',
86+
'[]',
87+
'{"foo": 1}'
88+
],
89+
'Empty patch against top-level array document' => [
90+
'["foo"]',
91+
'[]',
92+
'["foo"]'
93+
],
3294
'Add patch replaces existing value' => [
3395
'{"foo": 1}',
3496
'[{"op": "add", "path": "/foo", "value": "Hello World"}]',

tests/Mikemccabe_json_patch_phpTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use PHPUnit\Framework\Attributes\DataProvider;
66

7-
class Mikemccabe_json_patch_phpTest extends JsonPatchComplianceTest
7+
class Mikemccabe_json_patch_phpTest extends JsonPatchCompliance
88
{
99
#[DataProvider('validOperationsProvider')]
1010
public function testJsonPatchCompliance(string $json, string $patch, string $expected): void
@@ -18,4 +18,22 @@ public function testJsonPatchCompliance(string $json, string $patch, string $exp
1818
$this->normalizeJson(json_encode($document))
1919
);
2020
}
21+
22+
#[DataProvider('atomicOperationsProvider')]
23+
public function testAtomicOperations(string $json, string $patch, string $expected): void
24+
{
25+
$document = json_decode($json, true);
26+
$patch = json_decode($patch, true);
27+
28+
try {
29+
$document = \mikemccabe\JsonPatch\JsonPatch::patch($json, $patch);
30+
} catch (\Throwable) {
31+
// expecting some error
32+
}
33+
34+
$this->assertSame(
35+
$this->normalizeJson($expected),
36+
$this->normalizeJson(json_encode($document))
37+
);
38+
}
2139
}

tests/Php_jsonpatch_php_jsonpatchTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use PHPUnit\Framework\Attributes\DataProvider;
66

7-
class Php_jsonpatch_php_jsonpatchTest extends JsonPatchComplianceTest
7+
class Php_jsonpatch_php_jsonpatchTest extends JsonPatchCompliance
88
{
99
#[DataProvider('validOperationsProvider')]
1010
public function testJsonPatchCompliance(string $json, string $patch, string $expected): void
@@ -17,4 +17,22 @@ public function testJsonPatchCompliance(string $json, string $patch, string $exp
1717
$this->normalizeJson(json_encode($document))
1818
);
1919
}
20+
21+
#[DataProvider('atomicOperationsProvider')]
22+
public function testAtomicOperations(string $json, string $patch, string $expected): void
23+
{
24+
$document = json_decode($json, true);
25+
26+
try {
27+
$Patch = new \Rs\Json\Patch($json, $patch);
28+
$document = json_decode($Patch->apply(), false, 512, JSON_THROW_ON_ERROR);
29+
} catch (\Throwable) {
30+
// expecting some error
31+
}
32+
33+
$this->assertSame(
34+
$this->normalizeJson($expected),
35+
$this->normalizeJson(json_encode($document))
36+
);
37+
}
2038
}

tests/Remorhaz_php_json_patchTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use PHPUnit\Framework\Attributes\DataProvider;
66

7-
class Remorhaz_php_json_patchTest extends JsonPatchComplianceTest
7+
class Remorhaz_php_json_patchTest extends JsonPatchCompliance
88
{
99
#[DataProvider('validOperationsProvider')]
1010
public function testJsonPatchCompliance(string $json, string $patch, string $expected): void
@@ -24,4 +24,29 @@ public function testJsonPatchCompliance(string $json, string $patch, string $exp
2424
$this->normalizeJson($documentString)
2525
);
2626
}
27+
28+
#[DataProvider('atomicOperationsProvider')]
29+
public function testAtomicOperations(string $json, string $patch, string $expected): void
30+
{
31+
$documentString = $json;
32+
33+
try {
34+
$encodedValueFactory = \Remorhaz\JSON\Data\Value\EncodedJson\NodeValueFactory::create();
35+
$queryFactory = \Remorhaz\JSON\Patch\Query\QueryFactory::create();
36+
$processor = \Remorhaz\JSON\Patch\Processor\Processor::create();
37+
38+
$patch = $encodedValueFactory->createValue($patch);
39+
$query = $queryFactory->createQuery($patch);
40+
$document = $encodedValueFactory->createValue($json);
41+
$result = $processor->apply($query, $document);
42+
$documentString = $result->encode();
43+
} catch (\Throwable) {
44+
// expecting some error
45+
}
46+
47+
$this->assertSame(
48+
$this->normalizeJson($expected),
49+
$this->normalizeJson($documentString)
50+
);
51+
}
2752
}

tests/Swaggest_json_diffTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use PHPUnit\Framework\Attributes\DataProvider;
66

7-
class Swaggest_json_diffTest extends JsonPatchComplianceTest
7+
class Swaggest_json_diffTest extends JsonPatchCompliance
88
{
99
#[DataProvider('validOperationsProvider')]
1010
public function testJsonPatchCompliance(string $json, string $patch, string $expected): void
@@ -19,4 +19,23 @@ public function testJsonPatchCompliance(string $json, string $patch, string $exp
1919
$this->normalizeJson(json_encode($document))
2020
);
2121
}
22+
23+
#[DataProvider('atomicOperationsProvider')]
24+
public function testAtomicOperations(string $json, string $patch, string $expected): void
25+
{
26+
$document = json_decode($json);
27+
$patch = json_decode($patch);
28+
29+
try {
30+
$patch = \Swaggest\JsonDiff\JsonPatch::import($patch);
31+
$patch->apply($document);
32+
} catch (\Throwable) {
33+
// expecting some error
34+
}
35+
36+
$this->assertSame(
37+
$this->normalizeJson($expected),
38+
$this->normalizeJson(json_encode($document))
39+
);
40+
}
2241
}

tests/Xp_forge_json_patchTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use PHPUnit\Framework\Attributes\DataProvider;
66

7-
class Xp_forge_json_patchTest extends JsonPatchComplianceTest
7+
class Xp_forge_json_patchTest extends JsonPatchCompliance
88
{
99
#[DataProvider('validOperationsProvider')]
1010
public function testJsonPatchCompliance(string $json, string $patch, string $expected): void
@@ -20,4 +20,23 @@ public function testJsonPatchCompliance(string $json, string $patch, string $exp
2020
$this->normalizeJson(json_encode($document))
2121
);
2222
}
23+
24+
#[DataProvider('atomicOperationsProvider')]
25+
public function testAtomicOperations(string $json, string $patch, string $expected): void
26+
{
27+
$document = json_decode($json, true);
28+
$patch = json_decode($patch, true);
29+
30+
try {
31+
$Changes = new \text\json\patch\Changes(...$patch);
32+
$document = $Changes->apply($document)->value();
33+
} catch (\Throwable) {
34+
// expecting some error
35+
}
36+
37+
$this->assertSame(
38+
$this->normalizeJson($expected),
39+
$this->normalizeJson(json_encode($document))
40+
);
41+
}
2342
}

0 commit comments

Comments
 (0)