1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Swaggest\JsonDiff\Tests; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Swaggest\JsonDiff\JsonDiff; |
7
|
|
|
|
8
|
|
|
class DiffTest extends \PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @throws \Swaggest\JsonDiff\Exception |
12
|
|
|
*/ |
13
|
|
|
public function testStopOnDiff() |
14
|
|
|
{ |
15
|
|
|
$original = array(1, 2, 3, 4); |
16
|
|
|
$new = array(2, 4); |
17
|
|
|
$diff = new JsonDiff($original, $new, JsonDiff::STOP_ON_DIFF); |
18
|
|
|
$this->assertSame(1, $diff->getDiffCnt()); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @throws \Swaggest\JsonDiff\Exception |
23
|
|
|
*/ |
24
|
|
|
public function testSkipPatch() |
25
|
|
|
{ |
26
|
|
|
$original = (object)(array("root" => (object)array("a" => 1, "b" => 2))); |
27
|
|
|
$new = (object)(array("root" => (object)array("b" => 3, "c" => 4))); |
28
|
|
|
|
29
|
|
|
$diff = new JsonDiff($original, $new, JsonDiff::SKIP_JSON_PATCH); |
30
|
|
|
$this->assertSame(null, $diff->getPatch()); |
31
|
|
|
$this->assertSame(3, $diff->getDiffCnt()); |
32
|
|
|
$this->assertSame(1, $diff->getAddedCnt()); |
33
|
|
|
$this->assertSame(1, $diff->getRemovedCnt()); |
34
|
|
|
$this->assertSame(1, $diff->getModifiedCnt()); |
35
|
|
|
|
36
|
|
|
$diff = new JsonDiff($original, $new, JsonDiff::REARRANGE_ARRAYS); |
37
|
|
|
$this->assertJsonStringEqualsJsonString('[{"op":"remove","path":"/root/a"},{"value":2,"op":"test","path":"/root/b"},{"value":3,"op":"replace","path":"/root/b"},{"value":4,"op":"add","path":"/root/c"}]', |
38
|
|
|
json_encode($diff->getPatch(), JSON_UNESCAPED_SLASHES)); |
39
|
|
|
$this->assertSame(3, $diff->getDiffCnt()); |
40
|
|
|
$this->assertSame(1, $diff->getAddedCnt()); |
41
|
|
|
$this->assertSame(1, $diff->getRemovedCnt()); |
42
|
|
|
$this->assertSame(1, $diff->getModifiedCnt()); |
43
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
} |