|
1
|
|
|
<?php |
|
2
|
|
|
namespace Slince\Config\Tests; |
|
3
|
|
|
|
|
4
|
|
|
use PHPUnit\Framework\TestCase; |
|
5
|
|
|
use Slince\Config\Collection; |
|
6
|
|
|
|
|
7
|
|
|
class CollectionTest extends TestCase |
|
8
|
|
|
{ |
|
9
|
|
|
public function testToArray() |
|
10
|
|
|
{ |
|
11
|
|
|
$collection = new Collection([ |
|
12
|
|
|
'foo' => 'bar', |
|
13
|
|
|
'bar' => 'baz', |
|
14
|
|
|
]); |
|
15
|
|
|
$this->assertEquals([ |
|
16
|
|
|
'foo' => 'bar', |
|
17
|
|
|
'bar' => 'baz', |
|
18
|
|
|
], $collection->toArray()); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function testCount() |
|
22
|
|
|
{ |
|
23
|
|
|
$collection = new Collection([ |
|
24
|
|
|
'foo' => 'bar' |
|
25
|
|
|
]); |
|
26
|
|
|
$this->assertEquals(1, $collection->count()); |
|
27
|
|
|
$this->assertCount(1, $collection); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function testGet() |
|
31
|
|
|
{ |
|
32
|
|
|
$collection = new Collection([ |
|
33
|
|
|
'foo' => 'bar' |
|
34
|
|
|
]); |
|
35
|
|
|
$this->assertEquals('bar', $collection->get('foo')); |
|
36
|
|
|
$this->assertEquals('bar', $collection['foo']); |
|
37
|
|
|
|
|
38
|
|
|
$this->assertNull($collection->get('not_exists_key')); |
|
39
|
|
|
$this->assertEquals('default_value', $collection->get('not_exists_key', 'default_value')); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function testSet() |
|
43
|
|
|
{ |
|
44
|
|
|
$collection = new Collection(); |
|
45
|
|
|
$collection->set('foo', 'bar'); |
|
46
|
|
|
$this->assertEquals('bar', $collection->get('foo')); |
|
47
|
|
|
$collection['bar'] = 'baz'; |
|
48
|
|
|
$this->assertEquals('baz', $collection->get('bar')); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function testMerge() |
|
52
|
|
|
{ |
|
53
|
|
|
$collection = new Collection([ |
|
54
|
|
|
'foo' => 'bar' |
|
55
|
|
|
]); |
|
56
|
|
|
$collection->merge([ |
|
57
|
|
|
'foo' => 'baz' |
|
58
|
|
|
]); |
|
59
|
|
|
$this->assertEquals('baz', $collection->get('foo')); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testExists() |
|
63
|
|
|
{ |
|
64
|
|
|
$collection = new Collection([ |
|
65
|
|
|
'foo' => 'bar' |
|
66
|
|
|
]); |
|
67
|
|
|
$this->assertTrue($collection->exists('foo')); |
|
68
|
|
|
$this->assertTrue(isset($collection['foo'])); |
|
69
|
|
|
$this->assertFalse($collection->exists('bar')); |
|
70
|
|
|
$this->assertFalse(isset($collection['bar'])); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function testDelete() |
|
74
|
|
|
{ |
|
75
|
|
|
$collection = new Collection([ |
|
76
|
|
|
'foo' => 'bar', |
|
77
|
|
|
'bar' => 'baz', |
|
78
|
|
|
]); |
|
79
|
|
|
$this->assertCount(2, $collection); |
|
80
|
|
|
$collection->delete('foo'); |
|
81
|
|
|
$this->assertCount(1, $collection); |
|
82
|
|
|
unset($collection['bar']); |
|
83
|
|
|
$this->assertCount(0, $collection); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function testClear() |
|
87
|
|
|
{ |
|
88
|
|
|
$collection = new Collection([ |
|
89
|
|
|
'foo' => 'bar', |
|
90
|
|
|
'bar' => 'baz', |
|
91
|
|
|
]); |
|
92
|
|
|
$collection->clear(); |
|
93
|
|
|
$this->assertCount(0, $collection); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function testDeepAction() |
|
97
|
|
|
{ |
|
98
|
|
|
$collection = new Collection([ |
|
99
|
|
|
'foo' => [ |
|
100
|
|
|
'bar' => [ |
|
101
|
|
|
'baz' => [ |
|
102
|
|
|
'foo' => [ |
|
103
|
|
|
'bar' => 'baz' |
|
104
|
|
|
] |
|
105
|
|
|
] |
|
106
|
|
|
] |
|
107
|
|
|
] |
|
108
|
|
|
]); |
|
109
|
|
|
$this->assertEquals('baz', $collection['foo']['bar']['baz']['foo']['bar']); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function testIterator() |
|
113
|
|
|
{ |
|
114
|
|
|
$collection = new Collection([ |
|
115
|
|
|
'foo' => 'bar', |
|
116
|
|
|
'bar' => 'baz' |
|
117
|
|
|
]); |
|
118
|
|
|
foreach ($collection as $key => $value) { |
|
119
|
|
|
if ($key == 'foo') { |
|
120
|
|
|
$this->assertEquals('bar', $value); |
|
121
|
|
|
} elseif ($key == 'bar') { |
|
122
|
|
|
$this->assertEquals('baz', $value); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|