Total Complexity | 11 |
Total Lines | 206 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
49 | #[CoversClass(Arrays::class)] |
||
50 | class ArraysTest extends TestCase |
||
51 | { |
||
52 | /** |
||
53 | * Test Arrays::isAssociative(). |
||
54 | */ |
||
55 | public function testIsAssociative(): void |
||
56 | { |
||
57 | $array = [0, 1, 2, 3, 4]; |
||
58 | $arrayTwo = ['test' => 'testing', 'testing' => 'what']; |
||
59 | |||
60 | self::assertFalse(Arrays::isAssociative($array)); |
||
61 | self::assertTrue(Arrays::isAssociative($arrayTwo)); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Test Arrays::get(). |
||
66 | */ |
||
67 | public function testGet(): void |
||
68 | { |
||
69 | $array = ['this' => 'is', 'a' => 'test']; |
||
70 | |||
71 | self::assertSame('is', Arrays::get($array, 'this')); |
||
72 | self::assertSame('test', Arrays::get($array, 'a')); |
||
73 | self::assertNull(Arrays::get($array, 'notexist')); |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Test Arrays::set(). |
||
78 | */ |
||
79 | public function testSet(): void |
||
80 | { |
||
81 | $array = ['this' => 1, 'is' => 2, 'a' => 3, 'test' => 4]; |
||
82 | $newArray = ['that' => 4, 'was' => 3, 'a' => 2, 'test' => 1]; |
||
83 | |||
84 | Arrays::set($array, 'test', 5); |
||
85 | self::assertSame(5, Arrays::get($array, 'test')); |
||
86 | |||
87 | Arrays::set($array, null, $newArray); |
||
88 | self::assertSame(4, Arrays::get($array, 'that')); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Test Arrays::keyExists(). |
||
93 | */ |
||
94 | public function testKeyExists(): void |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Test Arrays::valueExists(). |
||
110 | */ |
||
111 | public function testValueExists(): void |
||
120 | } |
||
121 | |||
122 | public function testExistsDeprecation(): void |
||
123 | { |
||
124 | $array = ['test' => 1]; |
||
125 | |||
126 | $arrayAccess = new TestArrayAccess(); |
||
127 | $arrayAccess['test'] = 1; |
||
128 | |||
129 | $this->expectUserDeprecationMessage('Esi\Utility\Arrays::exists is deprecated and will be removed in v2.1.0, use Esi\Utility\Arrays::keyExists instead.'); |
||
130 | self::assertTrue(Arrays::exists($array, 'test')); |
||
1 ignored issue
–
show
|
|||
131 | self::assertFalse(Arrays::exists($array, 'this')); |
||
1 ignored issue
–
show
|
|||
132 | |||
133 | self::assertTrue(Arrays::exists($arrayAccess, 'test')); |
||
1 ignored issue
–
show
|
|||
134 | self::assertFalse(Arrays::exists($arrayAccess, 'this')); |
||
1 ignored issue
–
show
|
|||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Test Arrays::flatten(). |
||
139 | */ |
||
140 | public function testFlatten(): void |
||
141 | { |
||
142 | self::assertSame([ |
||
143 | 0 => 'a', |
||
144 | 1 => 'b', |
||
145 | 2 => 'c', |
||
146 | 3 => 'd', |
||
147 | '4.first' => 'e', |
||
148 | '4.0' => 'f', |
||
149 | '4.second' => 'g', |
||
150 | '4.1.0' => 'h', |
||
151 | '4.1.third' => 'i', |
||
152 | ], Arrays::flatten([ |
||
153 | 'a', 'b', 'c', 'd', ['first' => 'e', 'f', 'second' => 'g', ['h', 'third' => 'i']], |
||
154 | ])); |
||
155 | |||
156 | self::assertSame( |
||
157 | [0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd', '4.0' => 'e', '4.1' => 'f', '4.2' => 'g'], |
||
158 | Arrays::flatten(['a', 'b', 'c', 'd', ['e', 'f', 'g']]) |
||
159 | ); |
||
160 | |||
161 | self::assertSame( |
||
162 | ['k0' => 'a', 'k1' => 'b', 'k2' => 'c', 'k3' => 'd', 'k4.0' => 'e', 'k4.1' => 'f', 'k4.2' => 'g'], |
||
163 | Arrays::flatten(['a', 'b', 'c', 'd', ['e', 'f', 'g']], '.', 'k') |
||
164 | ); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Test Arrays::mapDeep(). |
||
169 | */ |
||
170 | public function testMapDeep(): void |
||
171 | { |
||
172 | self::assertSame([ |
||
173 | '<', |
||
174 | 'abc', |
||
175 | '>', |
||
176 | 'def', |
||
177 | ['&', 'test', '123'], |
||
178 | ], Arrays::mapDeep([ |
||
179 | '<', |
||
180 | 'abc', |
||
181 | '>', |
||
182 | 'def', |
||
183 | ['&', 'test', '123'], |
||
184 | ], 'htmlentities')); |
||
185 | |||
186 | $var = new stdClass(); |
||
187 | $var->test = ['test' => '>']; |
||
188 | $var->what = '<'; |
||
189 | |||
190 | $var2 = new stdClass(); |
||
191 | $var2->test = ['test' => '>']; |
||
192 | $var2->what = '<'; |
||
193 | |||
194 | self::assertEquals($var2, Arrays::mapDeep($var, 'htmlentities')); |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Test Arrays::interlace(). |
||
199 | */ |
||
200 | public function testInterlace(): void |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Test Arrays::groupBy(). |
||
216 | */ |
||
217 | public function testGroupBy(): void |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Test Arrays::groupBy() with non-existent/invalid key. |
||
242 | */ |
||
243 | public function testGroupByInvalidKey(): void |
||
316 |