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

ReplaceTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 76
c 2
b 0
f 0
dl 0
loc 103
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testReplaceScalar() 0 29 1
A testReplaceFilterPath() 0 35 1
A testReplace() 0 32 1
1
<?php
2
3
namespace Swaggest\JsonDiff\Tests;
4
5
6
use Swaggest\JsonDiff\JsonValueReplace;
7
8
class ReplaceTest extends \PHPUnit_Framework_TestCase
9
{
10
    public function testReplace()
11
    {
12
        $data = json_decode(<<<JSON
13
{
14
    "data": [
15
        {"a":"b","c":"d"},
16
        {"c":"d", "a":"b"},
17
        {"c":"d"}
18
    ],
19
    "o":{"a":"b","c":"d"}
20
}
21
JSON
22
        );
23
        $replace = new JsonValueReplace(
24
            json_decode('{"a":"b","c":"d"}'),
25
            json_decode('{"a":"b","c":"d","e":"f"}')
26
        );
27
28
        $result = $replace->process($data);
29
        $expected = json_decode(<<<JSON
30
{
31
    "data": [
32
        {"a":"b","c":"d","e":"f"},
33
        {"c":"d", "a":"b","e":"f"},
34
        {"c":"d"}
35
    ],
36
    "o":{"a":"b","c":"d","e":"f"}
37
}
38
JSON
39
        );
40
41
        $this->assertEquals($expected, $result);
42
    }
43
44
    public function testReplaceFilterPath()
45
    {
46
        $data = json_decode(<<<JSON
47
{
48
    "data": [
49
        {"a":"b","c":"d"},
50
        {"c":"d", "a":"b"},
51
        {"c":"d"}
52
    ],
53
    "o":{"a":"b","c":"d"}
54
}
55
JSON
56
        );
57
        $replace = new JsonValueReplace(
58
            json_decode('{"a":"b","c":"d"}'),
59
            json_decode('{"a":"b","c":"d","e":"f"}'),
60
            '~.*/data/.*~'
61
        );
62
63
        $result = $replace->process($data);
64
        $expected = json_decode(<<<JSON
65
{
66
    "data": [
67
        {"a":"b","c":"d","e":"f"},
68
        {"c":"d", "a":"b","e":"f"},
69
        {"c":"d"}
70
    ],
71
    "o":{"a":"b","c":"d"}
72
}
73
JSON
74
        );
75
76
        $this->assertEquals($expected, $result);
77
78
        $this->assertSame(array('/data/0', '/data/1'), $replace->affectedPaths);
79
    }
80
81
82
    public function testReplaceScalar()
83
    {
84
        $data = json_decode(<<<JSON
85
{
86
    "data": [
87
        {"a":"b","c":"d"},
88
        {"c":"d", "a":"b"},
89
        {"c":"d"}
90
    ],
91
    "o":{"a":"b","c":"d"}
92
}
93
JSON
94
        );
95
        $replace = new JsonValueReplace("b", "B");
96
97
        $result = $replace->process($data);
98
        $expected = json_decode(<<<JSON
99
{
100
    "data": [
101
        {"a":"B","c":"d"},
102
        {"c":"d", "a":"B"},
103
        {"c":"d"}
104
    ],
105
    "o":{"a":"B","c":"d"}
106
}
107
JSON
108
        );
109
110
        $this->assertEquals($expected, $result);
111
    }
112
113
}