Skip to content

Commit ddf73f3

Browse files
committed
test: Add array helper tests
1 parent ecad354 commit ddf73f3

1 file changed

Lines changed: 238 additions & 0 deletions

File tree

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Commands\TranslationLocalizationFinder;
13+
14+
use CodeIgniter\Commands\Translation\LocalizationFinder\ArrayHelper;
15+
use CodeIgniter\Test\CIUnitTestCase;
16+
17+
/**
18+
* @group Others
19+
*
20+
* @internal
21+
*/
22+
final class ArrayHelperTest extends CIUnitTestCase
23+
{
24+
private array $compareWith;
25+
26+
protected function setUp(): void
27+
{
28+
$this->compareWith = [
29+
'a' => [
30+
'b' => [
31+
'c' => [
32+
'd' => 'value1',
33+
],
34+
],
35+
'e' => 'value2',
36+
'f' => [
37+
'g' => 'value3',
38+
'h' => 'value4',
39+
'i' => [
40+
'j' => 'value5',
41+
],
42+
],
43+
'k' => null,
44+
'l' => [],
45+
'm' => '',
46+
],
47+
];
48+
}
49+
50+
public function testRecursiveDiffCopy(): void
51+
{
52+
$this->assertSame([], ArrayHelper::recursiveDiff($this->compareWith, $this->compareWith));
53+
}
54+
55+
public function testRecursiveDiffShuffleCopy(): void
56+
{
57+
$original = [
58+
'a' => [
59+
'l' => [],
60+
'k' => null,
61+
'e' => 'value2',
62+
'f' => [
63+
'i' => [
64+
'j' => 'value5',
65+
],
66+
'h' => 'value4',
67+
'g' => 'value3',
68+
],
69+
'm' => '',
70+
'b' => [
71+
'c' => [
72+
'd' => 'value1',
73+
],
74+
],
75+
],
76+
];
77+
78+
$this->assertSame([], ArrayHelper::recursiveDiff($original, $this->compareWith));
79+
}
80+
81+
public function testRecursiveDiffCopyWithAnotherValues(): void
82+
{
83+
$original = [
84+
'a' => [
85+
'b' => [
86+
'c' => [
87+
'd' => 'value1_1',
88+
],
89+
],
90+
'e' => 'value2_2',
91+
'f' => [
92+
'g' => 'value3_3',
93+
'h' => 'value4_4',
94+
'i' => [
95+
'j' => 'value5_5',
96+
],
97+
],
98+
'k' => [],
99+
'l' => null,
100+
'm' => 'value6_6',
101+
],
102+
];
103+
104+
$this->assertSame([], ArrayHelper::recursiveDiff($original, $this->compareWith));
105+
}
106+
107+
public function testRecursiveDiffEmptyCompare(): void
108+
{
109+
$this->assertSame($this->compareWith, ArrayHelper::recursiveDiff($this->compareWith, []));
110+
}
111+
112+
public function testRecursiveDiffEmptyOriginal(): void
113+
{
114+
$this->assertSame([], ArrayHelper::recursiveDiff([], $this->compareWith));
115+
}
116+
117+
public function testRecursiveDiffCompletelyDifferent(): void
118+
{
119+
$original = [
120+
'new_a' => [
121+
'new_b' => [
122+
'new_c' => [
123+
'new_d' => 'value1_1',
124+
],
125+
],
126+
'new_e' => 'value2_2',
127+
'new_f' => [
128+
'new_g' => 'value3_3',
129+
'new_h' => 'value4_4',
130+
'new_i' => [
131+
'new_j' => 'value5_5',
132+
],
133+
],
134+
'new_k' => [],
135+
'new_l' => null,
136+
'new_m' => '',
137+
],
138+
];
139+
140+
$this->assertSame($original, ArrayHelper::recursiveDiff($original, $this->compareWith));
141+
}
142+
143+
public function testRecursiveDiffPartlyDifferent(): void
144+
{
145+
$original = [
146+
'a' => [
147+
'b' => [
148+
'new_c' => [
149+
'd' => 'value1',
150+
],
151+
],
152+
'e' => 'value2',
153+
'f' => [
154+
'g' => 'value3',
155+
'new_h' => 'value4',
156+
'i' => [
157+
'new_j' => 'value5',
158+
],
159+
],
160+
'k' => null,
161+
'new_l' => [],
162+
'm' => [
163+
'new_n' => '',
164+
],
165+
],
166+
];
167+
168+
$diff = [
169+
'a' => [
170+
'b' => [
171+
'new_c' => [
172+
'd' => 'value1',
173+
],
174+
],
175+
'f' => [
176+
'new_h' => 'value4',
177+
'i' => [
178+
'new_j' => 'value5',
179+
],
180+
],
181+
'm' => [
182+
'new_n' => '',
183+
],
184+
],
185+
];
186+
187+
$this->assertSame($diff, ArrayHelper::recursiveDiff($original, $this->compareWith));
188+
}
189+
190+
public function testRecursiveCountSimple(): void
191+
{
192+
$array = [
193+
'a' => 'value1',
194+
'b' => 'value2',
195+
'c' => 'value3',
196+
];
197+
198+
$this->assertSame(3, ArrayHelper::recursiveCount($array));
199+
}
200+
201+
public function testRecursiveCountNested(): void
202+
{
203+
$array = [
204+
'a' => 'value1',
205+
'b' => [
206+
'c' => 'value2',
207+
],
208+
'd' => 'value3',
209+
'e' => [
210+
'f' => [
211+
'g' => 'value4',
212+
'h' => [
213+
'i' => 'value5',
214+
],
215+
],
216+
],
217+
'j' => [],
218+
'k' => null,
219+
];
220+
221+
$this->assertSame(11, ArrayHelper::recursiveCount($array));
222+
}
223+
224+
public function testRecursiveCountEqualEmpty(): void
225+
{
226+
$array = [
227+
'root' => [
228+
'a' => [],
229+
'b' => false,
230+
'c' => null,
231+
'd' => '',
232+
'e' => 0,
233+
],
234+
];
235+
236+
$this->assertSame(6, ArrayHelper::recursiveCount($array));
237+
}
238+
}

0 commit comments

Comments
 (0)