|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Glenn\Config\Test; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
|
6
|
|
|
|
|
7
|
|
|
use Glenn\Config\Manager; |
|
8
|
|
|
|
|
9
|
|
|
class ManagerTest extends PHPUnit_Framework_TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
protected $sampleConfigData = [ |
|
12
|
|
|
'name' => 'Glenn', |
|
13
|
|
|
'age' => 18, |
|
14
|
|
|
'favourites' => [ |
|
15
|
|
|
'food' => 'pizza', |
|
16
|
|
|
'drink' => 'coke', |
|
17
|
|
|
'colour' => 'black', |
|
18
|
|
|
], |
|
19
|
|
|
]; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Test the @has method. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Glenn McEwan <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
public function testHasMethod() |
|
27
|
|
|
{ |
|
28
|
|
|
$config = new Manager($this->sampleConfigData); |
|
29
|
|
|
|
|
30
|
|
|
$this->assertTrue($config->has('name')); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Test the has method works successfully for nested querying. |
|
35
|
|
|
* Checking if sub-level nodes in the config exist using dot notation. |
|
36
|
|
|
* |
|
37
|
|
|
* @author Glenn McEwan <[email protected]> |
|
38
|
|
|
*/ |
|
39
|
|
|
public function testNestedHasMethod() |
|
40
|
|
|
{ |
|
41
|
|
|
$config = new Manager($this->sampleConfigData); |
|
42
|
|
|
|
|
43
|
|
|
$this->assertTrue($config->has('favourites.food')); |
|
44
|
|
|
$this->assertTrue($config->has('favourites.drink')); |
|
45
|
|
|
$this->assertTrue($config->has('favourites.colour')); |
|
46
|
|
|
$this->assertFalse($config->has('favourites.dog')); |
|
47
|
|
|
$this->assertFalse($config->has('wrong.name')); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Test the @get method. |
|
52
|
|
|
* |
|
53
|
|
|
* @author Glenn McEwan <[email protected]> |
|
54
|
|
|
*/ |
|
55
|
|
|
public function testGetMethod() |
|
56
|
|
|
{ |
|
57
|
|
|
$config = new Manager($this->sampleConfigData); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertEquals($config->get('name'), 'Glenn'); |
|
60
|
|
|
$this->assertSame($config->get('age'), 18); |
|
61
|
|
|
$this->assertNotSame($config->get('age'), '18'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Test the @set method. |
|
66
|
|
|
* |
|
67
|
|
|
* @author Glenn McEwan <[email protected]> |
|
68
|
|
|
*/ |
|
69
|
|
|
public function testSetMethod() |
|
70
|
|
|
{ |
|
71
|
|
|
$config = new Manager($this->sampleConfigData); |
|
72
|
|
|
|
|
73
|
|
|
$config->set('gender', 'male'); |
|
74
|
|
|
|
|
75
|
|
|
$this->assertTrue($config->has('gender')); |
|
76
|
|
|
$this->assertSame($config->get('gender'), 'male'); |
|
77
|
|
|
|
|
78
|
|
|
$config->set('gender', 'female'); |
|
79
|
|
|
|
|
80
|
|
|
$this->assertSame($config->get('gender'), 'female'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Test the @get method works properly when passing a default. |
|
85
|
|
|
* |
|
86
|
|
|
* @author Glenn McEwan <[email protected]> |
|
87
|
|
|
*/ |
|
88
|
|
|
public function testGetDefaultMethod() |
|
89
|
|
|
{ |
|
90
|
|
|
$config = new Manager($this->sampleConfigData); |
|
91
|
|
|
|
|
92
|
|
|
$this->assertEquals($config->get('gender', 'male'), 'male'); |
|
93
|
|
|
$this->assertEquals($config->get('gender', 'female'), 'female'); |
|
94
|
|
|
$this->assertNull($config->get('gender')); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Test the @all method. |
|
99
|
|
|
* |
|
100
|
|
|
* @author Glenn McEwan <[email protected]> |
|
101
|
|
|
*/ |
|
102
|
|
|
public function testAllMethod() |
|
103
|
|
|
{ |
|
104
|
|
|
$sampleData = $this->sampleConfigData; |
|
105
|
|
|
|
|
106
|
|
|
$config = new Manager($sampleData); |
|
107
|
|
|
|
|
108
|
|
|
$this->assertSame($config->all(), $sampleData); |
|
109
|
|
|
|
|
110
|
|
|
$sampleData['gender'] = 'male'; |
|
111
|
|
|
|
|
112
|
|
|
$config->set('gender', 'male'); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertSame($config->all(), $sampleData); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Test the @set method sets a config entry, |
|
119
|
|
|
* and is retrievable with @get. |
|
120
|
|
|
* |
|
121
|
|
|
* @get with a default parameter should still return null if the |
|
122
|
|
|
* original @set call had a null value. |
|
123
|
|
|
* |
|
124
|
|
|
* @author Glenn McEwan <[email protected]> |
|
125
|
|
|
*/ |
|
126
|
|
|
public function testSetNullMethod() |
|
127
|
|
|
{ |
|
128
|
|
|
$config = new Manager($this->sampleConfigData); |
|
129
|
|
|
|
|
130
|
|
|
$config->set('gender'); |
|
131
|
|
|
|
|
132
|
|
|
$this->assertTrue($config->has('gender')); |
|
133
|
|
|
$this->assertNull($config->get('gender')); |
|
134
|
|
|
$this->assertNull($config->get('gender', 'male')); |
|
135
|
|
|
|
|
136
|
|
|
$config->set('gender', ''); |
|
137
|
|
|
|
|
138
|
|
|
$this->assertSame($config->get('gender'), ''); |
|
139
|
|
|
|
|
140
|
|
|
$config->set('gender', 'female'); |
|
141
|
|
|
|
|
142
|
|
|
$this->assertSame($config->get('gender'), 'female'); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Test the set array method |
|
147
|
|
|
* |
|
148
|
|
|
* @author Glenn McEwan <[email protected]> |
|
149
|
|
|
*/ |
|
150
|
|
View Code Duplication |
public function testSetArrayMethod() |
|
|
|
|
|
|
151
|
|
|
{ |
|
152
|
|
|
$config = new Manager($this->sampleConfigData); |
|
153
|
|
|
|
|
154
|
|
|
$config->set( |
|
155
|
|
|
'languages', |
|
156
|
|
|
[ |
|
157
|
|
|
'first' => 'English', |
|
158
|
|
|
'second' => 'Spanish', |
|
159
|
|
|
] |
|
160
|
|
|
); |
|
161
|
|
|
|
|
162
|
|
|
$config->setArray([ |
|
163
|
|
|
'languages' => [ |
|
164
|
|
|
'third' => 'French', |
|
165
|
|
|
], |
|
166
|
|
|
]); |
|
167
|
|
|
|
|
168
|
|
|
$this->assertSame( |
|
169
|
|
|
$config->get('languages'), |
|
170
|
|
|
[ |
|
171
|
|
|
'third' => 'French', |
|
172
|
|
|
] |
|
173
|
|
|
); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Test the set array method with a key passed in as a second parameter. |
|
178
|
|
|
* |
|
179
|
|
|
* @author Glenn McEwan <[email protected]> |
|
180
|
|
|
*/ |
|
181
|
|
View Code Duplication |
public function testSetArrayMethodWithKey() |
|
|
|
|
|
|
182
|
|
|
{ |
|
183
|
|
|
$config = new Manager($this->sampleConfigData); |
|
184
|
|
|
|
|
185
|
|
|
$config->set( |
|
186
|
|
|
'languages', |
|
187
|
|
|
[ |
|
188
|
|
|
'first' => 'English', |
|
189
|
|
|
'second' => 'Spanish', |
|
190
|
|
|
] |
|
191
|
|
|
); |
|
192
|
|
|
|
|
193
|
|
|
$config->setArray( |
|
194
|
|
|
[ |
|
195
|
|
|
'third' => 'French', |
|
196
|
|
|
], |
|
197
|
|
|
'languages' |
|
198
|
|
|
); |
|
199
|
|
|
|
|
200
|
|
|
$this->assertSame( |
|
201
|
|
|
$config->get('languages'), |
|
202
|
|
|
[ |
|
203
|
|
|
'third' => 'French', |
|
204
|
|
|
] |
|
205
|
|
|
); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
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.