Completed
Push — master ( 3a7482...36f3d7 )
by Jim
08:32
created

TestArrayAccessImplementation   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 22
c 0
b 0
f 0
rs 10
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: lenovo
5
 * Date: 6/14/2018
6
 * Time: 1:07 AM
7
 */
8
9
namespace TimSDK\Tests\Support;
10
11
use ArrayAccess;
12
use JsonSerializable;
13
use ReflectionClass;
14
use TimSDK\Support\Collection;
15
use TimSDK\Tests\TestCase;
16
17
class CollectionTest extends TestCase
18
{
19
    public function testCollectionIsConstructed()
20
    {
21
        $collection = new Collection('foo');
22
        $this->assertSame(['foo'], $collection->all());
23
24
        $collection = new Collection('{"foo": "bar"}');
25
        $this->assertSame(['foo' => 'bar'], $collection->all());
26
27
        $collection = new Collection(2);
28
        $this->assertSame([2], $collection->all());
29
30
        $collection = new Collection(false);
31
        $this->assertSame([false], $collection->all());
32
33
        $collection = new Collection(null);
34
        $this->assertSame([], $collection->all());
35
36
        $collection = new Collection;
37
        $this->assertSame([], $collection->all());
38
    }
39
40
    public function testOffsetAccess()
41
    {
42
        $c = new Collection(['name' => 'TimSDK']);
43
        $this->assertEquals('TimSDK', $c['name']);
44
45
        $c['name'] = 'dayle';
46
        $this->assertEquals('dayle', $c['name']);
47
        $this->assertTrue(isset($c['name']));
48
49
        unset($c['name']);
50
        $this->assertFalse(isset($c['name']));
51
52
        $c[] = 'jason';
53
        $this->assertEquals('jason', $c[0]);
54
    }
55
56
    public function testArrayAccessOffsetExists()
57
    {
58
        $c = new Collection(['foo', 'bar']);
59
        $this->assertTrue($c->offsetExists(0));
60
        $this->assertTrue($c->offsetExists(1));
61
        $this->assertFalse($c->offsetExists(1000));
62
    }
63
64
    public function testArrayAccessOffsetGet()
65
    {
66
        $c = new Collection(['foo', 'bar']);
67
        $this->assertEquals('foo', $c->offsetGet(0));
68
        $this->assertEquals('bar', $c->offsetGet(1));
69
    }
70
71
    public function testArrayAccessOffsetSet()
72
    {
73
        $c = new Collection(['foo', 'foo']);
74
        $c->offsetSet(1, 'bar');
75
        $this->assertEquals('bar', $c[1]);
76
        $c->offsetSet(null, 'qux');
77
        $this->assertEquals('qux', $c[2]);
78
    }
79
80
    public function testArrayAccessOffsetUnset()
81
    {
82
        $c = new Collection(['foo', 'bar']);
83
        $c->offsetUnset(1);
84
        $this->assertFalse(isset($c[1]));
85
    }
86
87
    public function testForgetArrayOfKeys()
88
    {
89
        $c = new Collection(['foo', 'bar', 'baz']);
90
        $c->forget([0, 2]);
91
        $this->assertFalse(isset($c[0]));
92
        $this->assertFalse(isset($c[2]));
93
        $this->assertTrue(isset($c[1]));
94
95
        $c = new Collection(['name' => 'taylor', 'foo' => 'bar', 'baz' => 'qux']);
96
        $c->forget(['foo', 'baz']);
97
        $this->assertFalse(isset($c['foo']));
98
        $this->assertFalse(isset($c['baz']));
99
        $this->assertTrue(isset($c['name']));
100
101
        $c = new Collection(['name' => 'taylor', 'foo' => 'bar', 'baz' => 'qux']);
102
        $c->forget('foo');
103
        $this->assertFalse(isset($c['foo']));
104
    }
105
106
    public function testCountable()
107
    {
108
        $c = new Collection(['foo', 'bar']);
109
        $this->assertCount(2, $c);
110
    }
111
112
    public function testIterable()
113
    {
114
        $c = new Collection(['foo']);
115
        $this->assertInstanceOf('ArrayIterator', $c->getIterator());
116
        $this->assertEquals(['foo'], $c->getIterator()->getArrayCopy());
117
    }
118
119
    public function testGetArrayableItems()
120
    {
121
        $collection = new Collection;
122
        $class = new ReflectionClass($collection);
123
        $method = $class->getMethod('getArrayableItems');
124
        $method->setAccessible(true);
125
126
        $items = new TestJsonSerializeObject;
127
        $array = $method->invokeArgs($collection, [$items]);
128
        $this->assertSame(['foo' => 'bar'], $array);
129
130
        $items = new Collection(['foo' => 'bar']);
131
        $array = $method->invokeArgs($collection, [$items]);
132
        $this->assertSame(['foo' => 'bar'], $array);
133
134
        $items = ['foo' => 'bar'];
135
        $array = $method->invokeArgs($collection, [$items]);
136
        $this->assertSame(['foo' => 'bar'], $array);
137
    }
138
139
    public function testSerializeAndUnserialize()
140
    {
141
        $c = new Collection(['foo' => 'bar']);
142
        $serialize = $c->serialize();
143
        $this->assertInternalType('string', $serialize);
144
145
        $c->unserialize($serialize);
146
        $this->assertSame(['foo' => 'bar'], $c->all());
147
    }
148
149
    public function testMerge()
150
    {
151
        $c = new Collection(['foo' => 'bar']);
152
        $c->merge(['key' => 'value']);
153
154
        $this->assertSame(['foo' => 'bar', 'key' => 'value'], $c->all());
155
    }
156
}
157
158
class TestArrayAccessImplementation implements ArrayAccess
159
{
160
    private $arr;
161
    public function __construct($arr)
162
    {
163
        $this->arr = $arr;
164
    }
165
    public function offsetExists($offset)
166
    {
167
        return isset($this->arr[$offset]);
168
    }
169
    public function offsetGet($offset)
170
    {
171
        return $this->arr[$offset];
172
    }
173
    public function offsetSet($offset, $value)
174
    {
175
        $this->arr[$offset] = $value;
176
    }
177
    public function offsetUnset($offset)
178
    {
179
        unset($this->arr[$offset]);
180
    }
181
}
182
183
class TestJsonSerializeObject implements JsonSerializable
184
{
185
    public function jsonSerialize()
186
    {
187
        return ['foo' => 'bar'];
188
    }
189
}
190