Skip to content

Commit 0bacb37

Browse files
committed
add ArrayAssertions
1 parent 5cc0db7 commit 0bacb37

File tree

4 files changed

+131
-2
lines changed

4 files changed

+131
-2
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ This will prevent any method name conflicts with core, your custom or other trai
2222

2323
## Assertions
2424

25+
### Array
26+
27+
```php
28+
\Astrotomic\PhpunitAssertions\ArrayAssertions::assertIndexed(['foo', 'bar']);
29+
\Astrotomic\PhpunitAssertions\ArrayAssertions::assertAssociative(['foo' => 'bar']);
30+
\Astrotomic\PhpunitAssertions\ArrayAssertions::assertEquals(['foo', 'bar'], ['bar', 'foo']);
31+
```
32+
2533
### Country
2634

2735
`composer require --dev league/iso3166:^3.0`

src/ArrayAssertions.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Astrotomic\PhpunitAssertions;
4+
5+
use PHPUnit\Framework\Assert as PHPUnit;
6+
7+
trait ArrayAssertions
8+
{
9+
public static function assertIndexed($actual): void
10+
{
11+
PHPUnit::assertIsArray($actual);
12+
PHPUnit::assertEquals(
13+
range(0, count($actual) - 1),
14+
array_keys($actual)
15+
);
16+
}
17+
18+
public static function assertAssociative($actual): void
19+
{
20+
PHPUnit::assertIsArray($actual);
21+
PHPUnit::assertNotEquals(
22+
range(0, count($actual) - 1),
23+
array_keys($actual)
24+
);
25+
}
26+
27+
public static function assertEquals(array $expected, $actual): void
28+
{
29+
PHPUnit::assertIsArray($actual);
30+
31+
if(array_keys($expected) === range(0, count($expected) - 1)) {
32+
sort($expected);
33+
sort($actual);
34+
} else {
35+
ksort($expected);
36+
ksort($actual);
37+
}
38+
39+
PHPUnit::assertEquals($expected, $actual);
40+
}
41+
}

tests/ArrayAssertionsTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Astrotomic\PhpunitAssertions\Tests;
4+
5+
use Astrotomic\PhpunitAssertions\ArrayAssertions;
6+
7+
final class ArrayAssertionsTest extends TestCase
8+
{
9+
/**
10+
* @test
11+
* @dataProvider hundredTimes
12+
*/
13+
public static function it_can_validate_array_is_indexed(): void
14+
{
15+
$actual = self::randomArray(self::randomInt(5, 20), false);
16+
17+
ArrayAssertions::assertIndexed($actual);
18+
}
19+
20+
/**
21+
* @test
22+
* @dataProvider hundredTimes
23+
*/
24+
public static function it_can_validate_array_is_associative(): void
25+
{
26+
$actual = self::randomArray(self::randomInt(5, 20), true);
27+
28+
ArrayAssertions::assertAssociative($actual);
29+
}
30+
31+
/**
32+
* @test
33+
* @dataProvider hundredTimes
34+
*/
35+
public static function it_can_validate_indexed_array_equality(): void
36+
{
37+
$array = self::randomArray(self::randomInt(5, 20), false);
38+
39+
$actual = $array;
40+
$expected = $array;
41+
shuffle($actual);
42+
43+
ArrayAssertions::assertEquals($expected, $actual);
44+
}
45+
46+
/**
47+
* @test
48+
* @dataProvider hundredTimes
49+
*/
50+
public static function it_can_validate_associative_array_equality(): void
51+
{
52+
$array = self::randomArray(self::randomInt(5, 20), true);
53+
54+
$actual = $array;
55+
$expected = $array;
56+
uksort($actual, fn(): int => self::randomInt() <=> self::randomInt());
57+
58+
ArrayAssertions::assertEquals($expected, $actual);
59+
}
60+
}

tests/Utils/Randomize.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ protected static function randomString(int $length = 16): string
1616

1717
protected static function randomBool(): bool
1818
{
19-
return boolval(mt_rand(0, 1));
19+
return (bool)random_int(0, 1);
2020
}
2121

2222
protected static function randomInt(int $min = PHP_INT_MIN, int $max = PHP_INT_MAX): int
2323
{
24-
return mt_rand($min, $max);
24+
return random_int($min, $max);
2525
}
2626

2727
protected static function randomFloat(float $min = PHP_INT_MIN, float $max = PHP_INT_MAX): float
@@ -33,4 +33,24 @@ protected static function randomElement(array $array)
3333
{
3434
return $array[array_rand($array)];
3535
}
36+
37+
protected static function randomArray(int $count, bool $assoc = false): array
38+
{
39+
$array = array_fill(0, $count, null);
40+
41+
$array = array_map(static function (): string {
42+
return self::randomString(self::randomInt(4, 64));
43+
}, $array);
44+
45+
if ($assoc) {
46+
$values = $array;
47+
$array = [];
48+
49+
foreach ($values as $value) {
50+
$array[self::randomString(self::randomInt(4, 64))] = $value;
51+
}
52+
}
53+
54+
return $array;
55+
}
3656
}

0 commit comments

Comments
 (0)