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

Sample::__unset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Swaggest\JsonDiff\Tests;
4
5
6
use Swaggest\JsonDiff\Exception;
7
use Swaggest\JsonDiff\JsonPointer;
8
9
class JsonPointerTest extends \PHPUnit_Framework_TestCase
10
{
11
    /**
12
     * @throws Exception
13
     */
14
    public function testProcess()
15
    {
16
        $json = new \stdClass();
17
        JsonPointer::add($json, ['l1', 'l2', 'l3'], 'hello!');
18
        $this->assertSame('{"l1":{"l2":{"l3":"hello!"}}}', json_encode($json));
19
20
        JsonPointer::add($json, ['l1', 'l2', 'l3'], 'hello again!', JsonPointer::SKIP_IF_ISSET);
21
        $this->assertSame('{"l1":{"l2":{"l3":"hello!"}}}', json_encode($json));
22
23
        JsonPointer::add($json, ['l1', 'l2', 'l3'], 'hello again!');
24
        $this->assertSame('{"l1":{"l2":{"l3":"hello again!"}}}', json_encode($json));
25
26
        JsonPointer::add($json, ['l1', 'l2', 'l3'], 'hello!');
27
        $this->assertSame('{"l1":{"l2":{"l3":"hello!"}}}', json_encode($json));
28
29
        $this->assertSame('{"l3":"hello!"}', json_encode(JsonPointer::get($json, JsonPointer::splitPath('/l1/l2'))));
30
31
        try {
32
            $this->assertSame('null', json_encode(JsonPointer::get($json, JsonPointer::splitPath('/l1/l2/non-existent'))));
33
        } catch (Exception $exception) {
34
            $this->assertSame('Key not found: non-existent', $exception->getMessage());
35
        }
36
37
        JsonPointer::remove($json, ['l1', 'l2']);
38
        $this->assertSame('{"l1":{}}', json_encode($json));
39
40
        JsonPointer::add($json, JsonPointer::splitPath('/l1/l2/0/0'), 0);
41
        JsonPointer::add($json, JsonPointer::splitPath('#/l1/l2/1/1'), 1);
42
43
        $this->assertSame('{"l1":{"l2":[[0],{"1":1}]}}', json_encode($json));
44
45
        $this->assertSame(1, JsonPointer::get($json, JsonPointer::splitPath('/l1/l2/1/1')));
46
        $this->assertSame(1, JsonPointer::getByPointer($json, '/l1/l2/1/1'));
47
    }
48
49
    /**
50
     * @throws Exception
51
     */
52
    public function testNumericKey()
53
    {
54
        $json = json_decode('{"l1":{"200":1}}');
55
        $this->assertSame(1, JsonPointer::get($json, JsonPointer::splitPath('/l1/200')));
56
    }
57
58
59
    public function testEscapeSegment()
60
    {
61
        $segment = '/project/{username}/{project}';
62
        $this->assertSame('~1project~1%7Busername%7D~1%7Bproject%7D', JsonPointer::escapeSegment($segment, true));
63
    }
64
65
    public function testBuildPath()
66
    {
67
        $pathItems = ['key1', '/project/{username}/{project}', 'key2'];
68
69
        $this->assertSame('/key1/~1project~1{username}~1{project}/key2',
70
            JsonPointer::buildPath($pathItems));
71
        $this->assertSame('#/key1/~1project~1%7Busername%7D~1%7Bproject%7D/key2',
72
            JsonPointer::buildPath($pathItems, true));
73
    }
74
75
    public function testGetSetDeleteObject()
76
    {
77
        $s = new Sample();
78
        $s->one = new Sample();
0 ignored issues
show
Bug Best Practice introduced by
The property one does not exist on Swaggest\JsonDiff\Tests\Sample. Since you implemented __set, consider adding a @property annotation.
Loading history...
79
        $s->one->two = 2;
0 ignored issues
show
Bug Best Practice introduced by
The property two does not exist on Swaggest\JsonDiff\Tests\Sample. Since you implemented __set, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property one does not exist on Swaggest\JsonDiff\Tests\Sample. Since you implemented __get, consider adding a @property annotation.
Loading history...
80
81
        $this->assertEquals(2, JsonPointer::get($s, ['one', 'two']));
82
83
84
        JsonPointer::add($s, ['one', 'two'], 22);
85
        $this->assertEquals(22, JsonPointer::get($s, ['one', 'two']));
86
        $this->assertEquals(22, $s->one->two);
0 ignored issues
show
Bug Best Practice introduced by
The property two does not exist on Swaggest\JsonDiff\Tests\Sample. Since you implemented __get, consider adding a @property annotation.
Loading history...
87
88
        JsonPointer::remove($s, ['one', 'two']);
89
        try {
90
            JsonPointer::get($s, ['one', 'two']);
91
            $this->fail('Exception expected');
92
        } catch (Exception $e) {
93
            $this->assertEquals('Key not found: two', $e->getMessage());
94
        }
95
        $this->assertEquals(null, $s->one->two);
96
    }
97
98
}
99
100
class Sample
101
{
102
    public $declared;
103
104
    private $_data = [];
105
106
    public function __isset($name)
107
    {
108
        return isset($this->_data[$name]);
109
    }
110
111
    public function &__get($name)
112
    {
113
        if (array_key_exists($name, $this->_data)) {
114
            return $this->_data[$name];
115
        } else {
116
            $tmp = null;
117
            return $tmp;;
118
        }
119
    }
120
121
    public function __set($name, $value)
122
    {
123
        $this->_data[$name] = $value;
124
    }
125
126
    public function __unset($name)
127
    {
128
        unset($this->_data[$name]);
129
    }
130
131
}
132