Skip to content

Commit a8257ea

Browse files
committed
add \Astrotomic\PhpunitAssertions\ArrayAssertions::assertContainsAll()
1 parent 4456a77 commit a8257ea

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ This will prevent any method name conflicts with core, your custom or other trai
3939
\Astrotomic\PhpunitAssertions\ArrayAssertions::assertAssociative(['foo' => 'bar']);
4040
\Astrotomic\PhpunitAssertions\ArrayAssertions::assertEquals(['foo', 'bar'], ['bar', 'foo']);
4141
\Astrotomic\PhpunitAssertions\ArrayAssertions::assertSubset(['foo' => 'bar'], ['baz' => 'foo', 'foo' => 'bar']);
42+
\Astrotomic\PhpunitAssertions\ArrayAssertions::assertContainsAll(['foo', 'bar'], ['baz', 'foo', 'lorem', 'ipsum', 'bar']);
4243
```
4344

4445
### Country

src/ArrayAssertions.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,13 @@ public static function assertSubset(array $expected, $actual): void
4848
PHPUnit::assertEquals($value, $actual[$key]);
4949
}
5050
}
51+
52+
public static function assertContainsAll(array $expected, $actual): void
53+
{
54+
PHPUnit::assertIsArray($actual);
55+
56+
foreach ($expected as $value) {
57+
PHPUnit::assertContains($value, $actual);
58+
}
59+
}
5160
}

tests/ArrayAssertionsTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,34 @@ public static function it_can_validate_associative_array_subset(): void
7272

7373
ArrayAssertions::assertSubset($expected, $actual);
7474
}
75+
76+
/**
77+
* @test
78+
* @dataProvider hundredTimes
79+
*/
80+
public static function it_can_validate_indexed_array_contains_all(): void
81+
{
82+
$array = self::randomArray(self::randomInt(5, 20), false);
83+
84+
$actual = $array;
85+
$expected = array_slice($array, 0, ceil(count($array) / self::randomInt(2, 4)), true);
86+
shuffle($actual);
87+
88+
ArrayAssertions::assertContainsAll($expected, $actual);
89+
}
90+
91+
/**
92+
* @test
93+
* @dataProvider hundredTimes
94+
*/
95+
public static function it_can_validate_associative_array_contains_all(): void
96+
{
97+
$array = self::randomArray(self::randomInt(5, 20), true);
98+
99+
$actual = $array;
100+
$expected = array_slice($array, 0, ceil(count($array) / self::randomInt(2, 4)), true);
101+
uksort($actual, fn (): int => self::randomInt() <=> self::randomInt());
102+
103+
ArrayAssertions::assertContainsAll($expected, $actual);
104+
}
75105
}

0 commit comments

Comments
 (0)