Skip to content

Commit 0291685

Browse files
authored
Improve test coverage (#11)
* Improve test coverage * Update README
1 parent e2f4027 commit 0291685

File tree

3 files changed

+166
-8
lines changed

3 files changed

+166
-8
lines changed

.mddoc.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<section title="Quorum Array Functions">
55
<badge-poser type="version"/>
66
<badge-poser type="license"/>
7-
<badge-github-action name="QuorumCollection/ArrayFunctions" workflow="CI"/>
7+
<badge-github-action name="QuorumCollection/ArrayFunctions" workflow-file="ci.yml"/>
88

99
<text>A collection of global `array_*` functions appending to the native PHP set.</text>
1010

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Latest Stable Version](https://poser.pugx.org/quorum/array-functions/version)](https://packagist.org/packages/quorum/array-functions)
44
[![License](https://poser.pugx.org/quorum/array-functions/license)](https://packagist.org/packages/quorum/array-functions)
5-
[![CI](https://github.com/QuorumCollection/ArrayFunctions/workflows/CI/badge.svg?)](https://github.com/QuorumCollection/ArrayFunctions/actions?query=workflow%3ACI)
5+
[![ci.yml](https://github.com/QuorumCollection/ArrayFunctions/actions/workflows/ci.yml/badge.svg)](https://github.com/QuorumCollection/ArrayFunctions/actions/workflows/ci.yml)
66

77

88
A collection of global `array_*` functions appending to the native PHP set.

test/ArrayFunctionsTest.php

Lines changed: 164 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public function testArrayFlatten() {
1818
array_flatten(array(
1919
'bbq' => array(
2020
'soda' => array( 1, 2, 4, 'fish fry' ),
21-
3
22-
)
21+
3,
22+
),
2323
))
2424
);
2525

@@ -28,27 +28,185 @@ public function testArrayFlatten() {
2828
array_flatten(array(
2929
'bbq' => array(
3030
'soda' => array( 1, 2, 4, 'fish fry' ),
31-
3
32-
)
31+
3,
32+
),
3333
), true)
3434
);
35-
3635
}
3736

3837
public function testArrayBlend() {
39-
//@todo: I'm not entirely sure how to test this
38+
// Test blending arrays without specifying keys
39+
$arrays = array(
40+
'fruits' => array( 'apple', 'banana', 'cherry' ),
41+
'vegetables' => array( 'carrot', 'broccoli', 'spinach' ),
42+
'meats' => array( 'chicken', 'beef', 'pork' ),
43+
);
44+
45+
$expected = array( 'apple', 'banana', 'cherry', 'carrot', 'broccoli', 'spinach', 'chicken', 'beef', 'pork' );
46+
$this->assertEquals($expected, array_blend($arrays));
47+
48+
// Test blending arrays with specific keys
49+
$keys = array( 'fruits', 'meats' );
50+
$expected = array( 'apple', 'banana', 'cherry', 'chicken', 'beef', 'pork' );
51+
$this->assertEquals($expected, array_blend($arrays, $keys));
52+
53+
// Test with empty arrays
54+
$arrays = array(
55+
'fruits' => array(),
56+
'vegetables' => array( 'carrot', 'broccoli' ),
57+
'empty' => array(),
58+
);
59+
60+
$expected = array( 'carrot', 'broccoli' );
61+
$this->assertEquals($expected, array_blend($arrays));
62+
63+
// Test with non-array values (should be skipped)
64+
$arrays = array(
65+
'fruits' => array( 'apple', 'banana' ),
66+
'number' => 42,
67+
'vegetables' => array( 'carrot' ),
68+
);
69+
70+
$expected = array( 'apple', 'banana', 'carrot' );
71+
$this->assertEquals($expected, array_blend($arrays));
4072
}
4173

4274
public function testArrayKeyArray() {
75+
// Test with numeric keys
76+
$arrays = array(
77+
array( 'id' => 1, 'name' => 'John', 'role' => 'Developer' ),
78+
array( 'id' => 2, 'name' => 'Jane', 'role' => 'Designer' ),
79+
array( 'id' => 3, 'name' => 'Bob', 'role' => 'Manager' ),
80+
);
4381

82+
$expected = array( 1, 2, 3 );
83+
$this->assertEquals($expected, array_key_array($arrays, 'id'));
84+
85+
// Test with string keys
86+
$expected = array( 'John', 'Jane', 'Bob' );
87+
$this->assertEquals($expected, array_key_array($arrays, 'name'));
88+
89+
// Test with associative array keys
90+
$arrays = array(
91+
'user1' => array( 'id' => 101, 'name' => 'Alice' ),
92+
'user2' => array( 'id' => 102, 'name' => 'Charlie' ),
93+
'user3' => array( 'id' => 103, 'name' => 'Dave' ),
94+
);
95+
96+
$expected = array( 'user1' => 101, 'user2' => 102, 'user3' => 103 );
97+
$this->assertEquals($expected, array_key_array($arrays, 'id'));
98+
99+
// Test with mixed content
100+
$arrays = array(
101+
array( 'id' => 201, 'data' => 'value1' ),
102+
array( 'id' => 202, 'data' => array( 'nested' => 'value2' ) ),
103+
array( 'id' => 203, 'data' => 42 ),
104+
);
105+
106+
$expected = array( 'value1', array( 'nested' => 'value2' ), 42 );
107+
$this->assertEquals($expected, array_key_array($arrays, 'data'));
44108
}
45109

46110
public function testArrayKeysArray() {
111+
// Test with multiple keys
112+
$arrays = array(
113+
array( 'id' => 1, 'name' => 'John', 'role' => 'Developer', 'age' => 30 ),
114+
array( 'id' => 2, 'name' => 'Jane', 'role' => 'Designer', 'age' => 28 ),
115+
array( 'id' => 3, 'name' => 'Bob', 'role' => 'Manager', 'age' => 35 ),
116+
);
117+
118+
$keys = array( 'id', 'name' );
119+
$expected = array(
120+
array( 'id' => 1, 'name' => 'John' ),
121+
array( 'id' => 2, 'name' => 'Jane' ),
122+
array( 'id' => 3, 'name' => 'Bob' ),
123+
);
124+
$this->assertEquals($expected, array_keys_array($arrays, $keys));
47125

126+
// Test with a single key (passed as string)
127+
$expected = array(
128+
array( 'id' => 1 ),
129+
array( 'id' => 2 ),
130+
array( 'id' => 3 ),
131+
);
132+
$this->assertEquals($expected, array_keys_array($arrays, 'id'));
133+
134+
// Test with a single key (passed as array)
135+
$this->assertEquals($expected, array_keys_array($arrays, array( 'id' )));
136+
137+
// Test with associative array keys
138+
$arrays = array(
139+
'user1' => array( 'id' => 101, 'name' => 'Alice', 'dept' => 'Engineering' ),
140+
'user2' => array( 'id' => 102, 'name' => 'Charlie', 'dept' => 'Marketing' ),
141+
'user3' => array( 'id' => 103, 'name' => 'Dave', 'dept' => 'Sales' ),
142+
);
143+
144+
$keys = array( 'id', 'dept' );
145+
$expected = array(
146+
'user1' => array( 'id' => 101, 'dept' => 'Engineering' ),
147+
'user2' => array( 'id' => 102, 'dept' => 'Marketing' ),
148+
'user3' => array( 'id' => 103, 'dept' => 'Sales' ),
149+
);
150+
$this->assertEquals($expected, array_keys_array($arrays, $keys));
48151
}
49152

50153
public function testArrayKeyRefill() {
154+
// Test with missing keys and default empty array fill
155+
$array = array(
156+
'key1' => 'value1',
157+
'key3' => 'value3',
158+
);
159+
160+
$keys = array( 'key1', 'key2', 'key3', 'key4' );
161+
$expected = array(
162+
'key1' => 'value1',
163+
'key2' => array(),
164+
'key3' => 'value3',
165+
'key4' => array(),
166+
);
167+
168+
$this->assertEquals($expected, array_key_refill($array, $keys));
169+
170+
// Test with custom fill value (string)
171+
$expected = array(
172+
'key1' => 'value1',
173+
'key2' => 'default',
174+
'key3' => 'value3',
175+
'key4' => 'default',
176+
);
177+
178+
$this->assertEquals($expected, array_key_refill($array, $keys, 'default'));
179+
180+
// Test with custom fill value (number)
181+
$expected = array(
182+
'key1' => 'value1',
183+
'key2' => 0,
184+
'key3' => 'value3',
185+
'key4' => 0,
186+
);
187+
188+
$this->assertEquals($expected, array_key_refill($array, $keys, 0));
189+
190+
// Test with custom fill value (array)
191+
$fillArray = array( 'status' => 'empty' );
192+
$expected = array(
193+
'key1' => 'value1',
194+
'key2' => $fillArray,
195+
'key3' => 'value3',
196+
'key4' => $fillArray,
197+
);
198+
199+
$this->assertEquals($expected, array_key_refill($array, $keys, $fillArray));
200+
201+
// Test with no missing keys
202+
$array = array(
203+
'a' => 1,
204+
'b' => 2,
205+
'c' => 3,
206+
);
51207

208+
$keys = array( 'a', 'b', 'c' );
209+
$this->assertEquals($array, array_key_refill($array, $keys, 'unused'));
52210
}
53211

54212

0 commit comments

Comments
 (0)