Passed
Pull Request — master (#42)
by Viacheslav
02:53 queued 10s
created

DiffTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 35
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testStopOnDiff() 0 6 1
A testSkipPatch() 0 19 1
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
}