Skip to content

Commit 9b263ed

Browse files
pfrenssensebastianbergmann
authored andcommitted
Test ArraySubset with indexed arrays.
1 parent 418c58a commit 9b263ed

File tree

2 files changed

+157
-4
lines changed

2 files changed

+157
-4
lines changed

tests/Framework/AssertTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,17 @@ public function testAssertArraySubset(): void
159159
'd' => ['a2' => ['a3' => 'item a3', 'b3' => 'item b3']]
160160
];
161161

162+
$this->assertArraySubset(['a' => 'item a'], $array);
162163
$this->assertArraySubset(['a' => 'item a', 'c' => ['a2' => 'item a2']], $array);
163164
$this->assertArraySubset(['a' => 'item a', 'd' => ['a2' => ['b3' => 'item b3']]], $array);
165+
$this->assertArraySubset(['b' => 'item b', 'd' => ['a2' => ['b3' => 'item b3']]], $array);
164166

165167
$arrayAccessData = new \ArrayObject($array);
166168

169+
$this->assertArraySubset(['a' => 'item a'], $arrayAccessData);
167170
$this->assertArraySubset(['a' => 'item a', 'c' => ['a2' => 'item a2']], $arrayAccessData);
168171
$this->assertArraySubset(['a' => 'item a', 'd' => ['a2' => ['b3' => 'item b3']]], $arrayAccessData);
172+
$this->assertArraySubset(['b' => 'item b', 'd' => ['a2' => ['b3' => 'item b3']]], $arrayAccessData);
169173

170174
try {
171175
$this->assertArraySubset(['a' => 'bad value'], $array);
@@ -181,6 +185,39 @@ public function testAssertArraySubset(): void
181185
$this->fail();
182186
}
183187

188+
public function testAssertArraySubsetWithIndexedArrays(): void
189+
{
190+
$array = [
191+
'item a',
192+
'item b',
193+
['a2' => 'item a2', 'b2' => 'item b2'],
194+
['a2' => ['a3' => 'item a3', 'b3' => 'item b3']]
195+
];
196+
197+
$this->assertArraySubset(['item a', ['a2' => 'item a2']], $array);
198+
$this->assertArraySubset(['item a', ['a2' => ['b3' => 'item b3']]], $array);
199+
$this->assertArraySubset(['item b', ['a2' => ['b3' => 'item b3']]], $array);
200+
201+
$arrayAccessData = new \ArrayObject($array);
202+
203+
$this->assertArraySubset(['item a', ['a2' => 'item a2']], $arrayAccessData);
204+
$this->assertArraySubset(['item a', ['a2' => ['b3' => 'item b3']]], $arrayAccessData);
205+
$this->assertArraySubset(['item b', ['a2' => ['b3' => 'item b3']]], $arrayAccessData);
206+
207+
try {
208+
$this->assertArraySubset(['bad value'], $array);
209+
} catch (AssertionFailedError $e) {
210+
}
211+
212+
try {
213+
$this->assertArraySubset([['a2' => ['bad index' => 'item b3']]], $array);
214+
} catch (AssertionFailedError $e) {
215+
return;
216+
}
217+
218+
$this->fail();
219+
}
220+
184221
public function testAssertArraySubsetWithDeepNestedArrays(): void
185222
{
186223
$array = [

tests/Framework/Constraint/ArraySubsetTest.php

Lines changed: 120 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,146 @@ class ArraySubsetTest extends ConstraintTestCase
1717
public static function evaluateDataProvider()
1818
{
1919
return [
20-
'loose array subset and array other' => [
20+
'loose associative array subset and array other' => [
2121
'expected' => true,
2222
'subset' => ['bar' => 0],
2323
'other' => ['foo' => '', 'bar' => '0'],
2424
'strict' => false
2525
],
26-
'strict array subset and array other' => [
26+
'strict associative array subset and array other' => [
2727
'expected' => false,
2828
'subset' => ['bar' => 0],
2929
'other' => ['foo' => '', 'bar' => '0'],
3030
'strict' => true
3131
],
32-
'loose array subset and ArrayObject other' => [
32+
'loose associative array subset and ArrayObject other' => [
3333
'expected' => true,
3434
'subset' => ['bar' => 0],
3535
'other' => new \ArrayObject(['foo' => '', 'bar' => '0']),
3636
'strict' => false
3737
],
38-
'strict ArrayObject subset and array other' => [
38+
'strict associative ArrayObject subset and array other' => [
3939
'expected' => true,
4040
'subset' => new \ArrayObject(['bar' => 0]),
4141
'other' => ['foo' => '', 'bar' => 0],
4242
'strict' => true
4343
],
44+
'loose indexed array subset and array other' => [
45+
'expected' => true,
46+
'subset' => [0],
47+
'other' => ['', '0'],
48+
'strict' => false
49+
],
50+
'strict indexed array subset and array other' => [
51+
'expected' => false,
52+
'subset' => [0],
53+
'other' => ['', '0'],
54+
'strict' => true
55+
],
56+
'loose indexed array subset and ArrayObject other' => [
57+
'expected' => true,
58+
'subset' => [0],
59+
'other' => new \ArrayObject(['', '0']),
60+
'strict' => false
61+
],
62+
'strict indexed ArrayObject subset and array other' => [
63+
'expected' => true,
64+
'subset' => new \ArrayObject([0]),
65+
'other' => ['', 0],
66+
'strict' => true
67+
],
68+
'loose unordered indexed array subset and array other' => [
69+
'expected' => true,
70+
'subset' => [0, '1'],
71+
'other' => ['1', '2', '0'],
72+
'strict' => false
73+
],
74+
'strict unordered indexed array subset and array other' => [
75+
'expected' => false,
76+
'subset' => [0, '1'],
77+
'other' => ['1', '2', '0'],
78+
'strict' => true
79+
],
80+
'loose unordered indexed array subset and ArrayObject other' => [
81+
'expected' => true,
82+
'subset' => [0, '1'],
83+
'other' => new \ArrayObject(['1', '2', '0']),
84+
'strict' => false
85+
],
86+
'strict unordered indexed ArrayObject subset and array other' => [
87+
'expected' => true,
88+
'subset' => new \ArrayObject([0, '1']),
89+
'other' => ['1', '2', 0],
90+
'strict' => true
91+
],
92+
'loose unordered multidimensional indexed array subset and array other' => [
93+
'expected' => true,
94+
'subset' => [
95+
[[3, 4], 2],
96+
'10',
97+
],
98+
'other' => [
99+
0 => '1',
100+
'a' => [
101+
'aa' => '2',
102+
'ab' => [5, 4, 3],
103+
'ac' => 10,
104+
],
105+
'b' => '10',
106+
],
107+
'strict' => false
108+
],
109+
'strict unordered multidimensional indexed array subset and array other' => [
110+
'expected' => false,
111+
'subset' => [
112+
[[3, 4], 2],
113+
'10',
114+
],
115+
'other' => [
116+
0 => '1',
117+
'a' => [
118+
'aa' => '2',
119+
'ab' => [5, 4, 3],
120+
'ac' => 10,
121+
],
122+
'b' => '10',
123+
],
124+
'strict' => true
125+
],
126+
'loose unordered multidimensional indexed array subset and ArrayObject other' => [
127+
'expected' => true,
128+
'subset' => [
129+
[[3, 4], 2],
130+
'10',
131+
],
132+
'other' => new \ArrayObject([
133+
0 => '1',
134+
'a' => [
135+
'aa' => '2',
136+
'ab' => [5, 4, 3],
137+
'ac' => 10,
138+
],
139+
'b' => '10',
140+
]),
141+
'strict' => false
142+
],
143+
'strict unordered multidimensional indexed ArrayObject subset and array other' => [
144+
'expected' => true,
145+
'subset' => new \ArrayObject([
146+
[[3, 4], '2'],
147+
'10',
148+
]),
149+
'other' => [
150+
0 => '1',
151+
'a' => [
152+
'aa' => '2',
153+
'ab' => [5, 4, 3],
154+
'ac' => 10,
155+
],
156+
'b' => '10',
157+
],
158+
'strict' => true
159+
],
44160
];
45161
}
46162

0 commit comments

Comments
 (0)