1
|
|
|
<?php |
2
|
|
|
namespace NeedleProject\Common\Helper; |
3
|
|
|
|
4
|
|
|
use PHPUnit_Framework_TestCase as TestCase; |
5
|
|
|
|
6
|
|
|
class ArrayHelperTest extends TestCase |
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* @dataProvider provideTrueScenarios |
10
|
|
|
* @param $array |
11
|
|
|
* @param $keys |
12
|
|
|
*/ |
13
|
|
|
public function testHasKey($array, $keys) |
14
|
|
|
{ |
15
|
|
|
$arrayHelper = new ArrayHelper(); |
16
|
|
|
$this->assertTrue( |
17
|
|
|
$arrayHelper->hasKeysInDepth($array, $keys) |
18
|
|
|
); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @dataProvider provideFalseScenarios |
23
|
|
|
* @param $array |
24
|
|
|
* @param $keys |
25
|
|
|
*/ |
26
|
|
|
public function testFailHasKey($array, $keys) |
27
|
|
|
{ |
28
|
|
|
$arrayHelper = new ArrayHelper(); |
29
|
|
|
$this->assertFalse( |
30
|
|
|
$arrayHelper->hasKeysInDepth($array, $keys) |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @expectedException \InvalidArgumentException |
36
|
|
|
* @dataProvider provideExceptionScenarios |
37
|
|
|
* @param $notArray |
38
|
|
|
*/ |
39
|
|
|
public function testHasKeyException($notArray) |
40
|
|
|
{ |
41
|
|
|
$arrayHelper = new ArrayHelper(); |
42
|
|
|
$arrayHelper->hasKeysInDepth($notArray, []); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @expectedException \InvalidArgumentException |
47
|
|
|
* @dataProvider provideExceptionScenarios |
48
|
|
|
* @param $notArray |
49
|
|
|
*/ |
50
|
|
|
public function testGetValueException($notArray) |
51
|
|
|
{ |
52
|
|
|
$arrayHelper = new ArrayHelper(); |
53
|
|
|
$arrayHelper->getValueFromDepth($notArray, []); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @dataProvider provideValueScenarios |
58
|
|
|
* @param array $array |
59
|
|
|
* @param array $keys |
60
|
|
|
* @param mixed $expectedValue |
61
|
|
|
*/ |
62
|
|
|
public function testGetValue($array, $keys, $expectedValue) |
63
|
|
|
{ |
64
|
|
|
$arrayHelper = new ArrayHelper(); |
65
|
|
|
$this->assertEquals( |
66
|
|
|
$expectedValue, |
67
|
|
|
$arrayHelper->getValueFromDepth($array, $keys) |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @dataProvider provideValueScenarios |
73
|
|
|
* @param array $array |
74
|
|
|
* @param array $keys |
75
|
|
|
*/ |
76
|
|
|
public function testFailGetValue($array, $keys) |
77
|
|
|
{ |
78
|
|
|
$arrayHelper = new ArrayHelper(); |
79
|
|
|
$arrayHelper->getValueFromDepth($array, $keys); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @expectedException \NeedleProject\Common\Exception\NotFoundException |
84
|
|
|
*/ |
85
|
|
|
public function testNotFoundGetValue() |
86
|
|
|
{ |
87
|
|
|
$arrayHelper = new ArrayHelper(); |
88
|
|
|
$arrayHelper->getValueFromDepth(['a' => 'b'], ['a', 'c']); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Tied to ::testHasKey |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public function provideTrueScenarios() |
96
|
|
|
{ |
97
|
|
|
return [ |
98
|
|
|
[ |
99
|
|
|
[ |
100
|
|
|
'foo' => [ |
101
|
|
|
'bar' => [ |
102
|
|
|
'baz' => [ |
103
|
|
|
'qux' => 'Lorem ipsum' |
104
|
|
|
] |
105
|
|
|
] |
106
|
|
|
] |
107
|
|
|
], |
108
|
|
|
['foo','bar','baz','qux'] |
109
|
|
|
], |
110
|
|
|
[ |
111
|
|
|
[[[['a' => ['bar']]]]], |
112
|
|
|
[0, 0, 0, 'a', 0] |
113
|
|
|
] |
114
|
|
|
]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Tied to ::testFailHasKey |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
public function provideFalseScenarios() |
122
|
|
|
{ |
123
|
|
|
return [ |
124
|
|
|
[ |
125
|
|
|
[ |
126
|
|
|
'foo' => [ |
127
|
|
|
'foo' => [ |
128
|
|
|
'foo' => [ |
129
|
|
|
'foo' => [ |
130
|
|
|
'bar' => 'Lorem ipsum' |
131
|
|
|
] |
132
|
|
|
] |
133
|
|
|
] |
134
|
|
|
] |
135
|
|
|
], |
136
|
|
|
['foo', 'foo', 'foo', 'foo', 'foo', 'bar'] |
137
|
|
|
], |
138
|
|
|
[ |
139
|
|
|
[ |
140
|
|
|
'foo' => [ |
141
|
|
|
'bar' => 'baz' |
142
|
|
|
] |
143
|
|
|
], |
144
|
|
|
['foo', 'bar', 'baz'] |
145
|
|
|
] |
146
|
|
|
]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Tied to ::testException |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
public function provideExceptionScenarios() |
154
|
|
|
{ |
155
|
|
|
return [ |
156
|
|
|
[1], |
157
|
|
|
['a'], |
158
|
|
|
[new \stdClass()], |
159
|
|
|
[0xFF], |
160
|
|
|
[1.2], |
161
|
|
|
[[]], |
162
|
|
|
[['a' => 'b']] |
163
|
|
|
]; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Tied to ::testGetValue |
168
|
|
|
* @return array |
169
|
|
|
*/ |
170
|
|
View Code Duplication |
public function provideValueScenarios() |
|
|
|
|
171
|
|
|
{ |
172
|
|
|
return [ |
173
|
|
|
// first scenario |
174
|
|
|
[ |
175
|
|
|
[ |
176
|
|
|
'foo' => [ |
177
|
|
|
'foo' => [ |
178
|
|
|
'foo' => [ |
179
|
|
|
'foo' => [ |
180
|
|
|
'bar' => 'Lorem ipsum' |
181
|
|
|
] |
182
|
|
|
] |
183
|
|
|
] |
184
|
|
|
] |
185
|
|
|
], |
186
|
|
|
['foo', 'foo', 'foo', 'foo', 'bar'], |
187
|
|
|
'Lorem ipsum' |
188
|
|
|
] |
189
|
|
|
]; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Tied to ::testFailGetValue |
195
|
|
|
* @return array |
196
|
|
|
*/ |
197
|
|
View Code Duplication |
public function provideFailValueScenarios() |
|
|
|
|
198
|
|
|
{ |
199
|
|
|
return [ |
200
|
|
|
// first scenario |
201
|
|
|
[ |
202
|
|
|
[ |
203
|
|
|
'foo' => [ |
204
|
|
|
'foo' => [ |
205
|
|
|
'foo' => [ |
206
|
|
|
'foo' => [ |
207
|
|
|
'bar' => 'Lorem ipsum' |
208
|
|
|
] |
209
|
|
|
] |
210
|
|
|
] |
211
|
|
|
] |
212
|
|
|
], |
213
|
|
|
['foo', 'foo', 'foo', 'foo', 'foo', 'bar'] |
214
|
|
|
] |
215
|
|
|
]; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
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.