ImmutableFlatContainerTest::testHasIsFalse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Graze\DataStructure\Container;
4
5
use PHPUnit_Framework_TestCase as TestCase;
6
7
class ImmutableFlatContainerTest extends TestCase
8
{
9
    public function testInterface()
10
    {
11
        $cont = new ImmutableFlatContainer();
12
13
        $this->assertInstanceOf('Graze\DataStructure\Container\ContainerInterface', $cont);
14
        $this->assertInstanceOf('Serializable', $cont);
15
    }
16
17 View Code Duplication
    public function testConstructor()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
18
    {
19
        $params = ['foo' => 'a', 'bar' => 'b', 'baz' => 'c'];
20
        $cont = new ImmutableFlatContainer($params);
21
22
        $this->assertEquals($params, $cont->getAll());
23
    }
24
25 View Code Duplication
    public function testAdd()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
26
    {
27
        $cont = new ImmutableFlatContainer(['foo' => 'a', 'bar' => 'b']);
28
        $result = $cont->add('baz', 'c');
29
30
        $this->assertEquals(['foo' => 'a', 'bar' => 'b'], $cont->getAll());
31
        $this->assertEquals(['foo' => 'a', 'bar' => 'b', 'baz' => 'c'], $result->getAll());
32
        $this->assertNotSame($cont, $result);
33
        $this->assertInstanceOf(ImmutableFlatContainer::class, $result);
34
    }
35
36
    /**
37
     * @expectedException \Graze\DataStructure\Exception\RegisteredKeyException
38
     */
39
    public function testAddDuplicate()
40
    {
41
        $cont = new ImmutableFlatContainer(['foo' => 'a', 'bar' => 'b', 'baz' => 'c']);
42
43
        $cont->add('baz', 'd');
44
    }
45
46
    public function testForAll()
47
    {
48
        $params = ['foo' => 'a', 'bar' => 'b', 'baz' => 'c'];
49
        $seen = [];
50
51
        $cont = new ImmutableFlatContainer($params);
52
        $cont->forAll(function ($value, $key) use (&$seen) {
53
            $seen[$key] = $value;
54
        });
55
56
        $this->assertEquals($params, $seen);
57
    }
58
59
    public function testGet()
60
    {
61
        $cont = new ImmutableFlatContainer(['foo' => ['child' => 'a'], 'bar' => 'b', 'baz' => 'c']);
62
63
        $this->assertEquals('a', $cont->get('foo.child'));
64
    }
65
66
    public function testGetMissing()
67
    {
68
        $cont = new ImmutableFlatContainer();
69
70
        $this->assertNull($cont->get('foo'));
71
    }
72
73
    public function testGetIterator()
74
    {
75
        $cont = new ImmutableFlatContainer();
76
77
        $this->assertInstanceOf('Iterator', $cont->getIterator());
78
    }
79
80
    public function testHasIsTrue()
81
    {
82
        $cont = new ImmutableFlatContainer(['foo' => 'a', 'bar' => 'b', 'baz' => 'c']);
83
84
        $this->assertTrue($cont->has('foo'));
85
    }
86
87
    public function testHasIsFalse()
88
    {
89
        $cont = new ImmutableFlatContainer(['FOO' => 'a', 'bar' => 'b', 'baz' => 'c']);
90
91
        $this->assertFalse($cont->has('foo'));
92
    }
93
94
    public function testRemove()
95
    {
96
        $cont = new ImmutableFlatContainer(['foo' => 'a', 'bar' => ['b' => 'c', 'd' => 'e'], 'baz' => 'c']);
97
        $result = $cont->remove('bar.b');
98
99
        $this->assertEquals(['foo' => 'a', 'bar' => ['b' => 'c', 'd' => 'e'], 'baz' => 'c'], $cont->getAll());
100
        $this->assertEquals(['foo' => 'a', 'bar' => ['d' => 'e'], 'baz' => 'c'], $result->getAll());
101
        $this->assertNotSame($cont, $result);
102
        $this->assertInstanceOf(ImmutableFlatContainer::class, $result);
103
    }
104
105 View Code Duplication
    public function testRemoveMissing()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
    {
107
        $cont = new ImmutableFlatContainer(['foo' => 'a', 'bar' => 'b']);
108
        $result = $cont->remove('baz');
109
110
        $this->assertEquals(['foo' => 'a', 'bar' => 'b'], $cont->getAll());
111
        $this->assertEquals(['foo' => 'a', 'bar' => 'b'], $result->getAll());
112
        $this->assertSame($cont, $result);
113
        $this->assertInstanceOf(ImmutableFlatContainer::class, $result);
114
    }
115
116
    public function testSet()
117
    {
118
        $cont = new ImmutableFlatContainer(['foo' => 'a', 'bar' => ['b' => 'c']]);
119
        $result = $cont->set('bar.d', 'e');
120
121
        $this->assertEquals(['foo' => 'a', 'bar' => ['b' => 'c']], $cont->getAll());
122
        $this->assertEquals(['foo' => 'a', 'bar' => ['b' => 'c', 'd' => 'e']], $result->getAll());
123
        $this->assertNotSame($cont, $result);
124
        $this->assertInstanceOf(ImmutableFlatContainer::class, $result);
125
    }
126
127
    public function testSetDuplicate()
128
    {
129
        $cont = new ImmutableFlatContainer(['foo' => 'a', 'bar' => ['b' => 'c', 'd' => 'e']]);
130
        $result = $cont->set('bar.d', 'f');
131
132
        $this->assertEquals(['foo' => 'a', 'bar' => ['b' => 'c', 'd' => 'e']], $cont->getAll());
133
        $this->assertEquals(['foo' => 'a', 'bar' => ['b' => 'c', 'd' => 'f']], $result->getAll());
134
        $this->assertNotSame($cont, $result);
135
        $this->assertInstanceOf(ImmutableFlatContainer::class, $result);
136
    }
137
138 View Code Duplication
    public function testSerialize()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
    {
140
        $cont = new ImmutableFlatContainer(['foo' => 'a', 'bar' => 'b', 'baz' => 'c']);
141
142
        $this->assertEquals(
143
            'C:52:"Graze\DataStructure\Container\ImmutableFlatContainer":60:{a:3:{s:3:"foo";s:1:"a";s:3:"bar";s:1:"b";s:3:"baz";s:1:"c";}}',
144
            serialize($cont)
145
        );
146
    }
147
148
    public function testUnserialize()
149
    {
150
        $cont = unserialize('C:52:"Graze\DataStructure\Container\ImmutableFlatContainer":60:{a:3:{s:3:"foo";s:1:"a";s:3:"bar";s:1:"b";s:3:"baz";s:1:"c";}}');
151
152
        $this->assertEquals(['foo' => 'a', 'bar' => 'b', 'baz' => 'c'], $cont->getAll());
153
    }
154
155 View Code Duplication
    public function testArrayAccessUnset()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156
    {
157
        $cont = new ImmutableFlatContainer(['foo' => 'a', 'bar' => 'b', 'baz' => 'c']);
158
159
        unset($cont['baz']);
160
        $this->assertTrue($cont->has('baz'));
161
    }
162
163 View Code Duplication
    public function testArrayAccessSet()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
164
    {
165
        $cont = new ImmutableFlatContainer(['foo' => 'a', 'bar' => 'b', 'baz' => 'c']);
166
167
        $cont['baz'] = 'd';
168
        $this->assertEquals('c', $cont->get('baz'));
169
    }
170
171
    public function testImmutableChildren()
172
    {
173
        $cont = new ImmutableFlatContainer(
174
            ['a' => 'b', 'c' => new Container(['d' => 'e', 'f' => new Container(['g' => 'h'])])]
175
        );
176
177
        $output = $cont->set('c.f.g', 'i');
178
179
        $this->assertEquals(
180
            ['a' => 'b', 'c' => new Container(['d' => 'e', 'f' => new Container(['g' => 'h'])])],
181
            $cont->getAll()
182
        );
183
        $this->assertEquals(
184
            ['a' => 'b', 'c' => new Container(['d' => 'e', 'f' => new Container(['g' => 'i'])])],
185
            $output->getAll()
186
        );
187
    }
188
189 View Code Duplication
    public function testImmutableChildReferences()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
190
    {
191
        $child = new Container(['a' => 'b', 'c' => 'd']);
192
        $container = new ImmutableFlatContainer(['child' => $child]);
193
194
        $child->set('c', 'e');
195
196
        $this->assertEquals(
197
            ['child' => new Container(['a' => 'b', 'c' => 'd'])],
198
            $container->getAll()
199
        );
200
    }
201
202 View Code Duplication
    public function testExtractedChildDoesNotModifyParent()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
203
    {
204
        $cont = new ImmutableFlatContainer(['a' => 'b', 'c' => new Container(['d' => 'e'])]);
205
206
        $child = $cont->get('c');
207
208
        $child->set('d', 'f');
209
210
        $this->assertEquals(
211
            ['a' => 'b', 'c' => new Container(['d' => 'e'])],
212
            $cont->getAll(),
213
            'modifying a child object should not modify the parent container'
214
        );
215
    }
216
}
217