Skip to content

Commit 23f6131

Browse files
committed
Add assert and unit tests
1 parent 0d32959 commit 23f6131

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

src/Framework/Assert.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
use PHPUnit\Framework\Constraint\TraversableContainsEqual;
6666
use PHPUnit\Framework\Constraint\TraversableContainsIdentical;
6767
use PHPUnit\Framework\Constraint\TraversableContainsOnly;
68+
use PHPUnit\Framework\Constraint\Dictionary\IsIdenticalKeysValues;
6869
use PHPUnit\Util\Xml\Loader as XmlLoader;
6970
use PHPUnit\Util\Xml\XmlException;
7071

@@ -1763,6 +1764,21 @@ final public static function assertNotSame(mixed $expected, mixed $actual, strin
17631764
);
17641765
}
17651766

1767+
/**
1768+
* Assert that two arrays have the same keys and values for those keys.
1769+
* The order of the keys is ignored.
1770+
*
1771+
* @throws ExpectationFailedException
1772+
*/
1773+
final public static function assertSameDictionaryKeysValues(array $expected, array $actual, string $message = ''): void
1774+
{
1775+
self::assertThat(
1776+
$actual,
1777+
new IsIdenticalKeysValues($expected),
1778+
$message
1779+
);
1780+
}
1781+
17661782
/**
17671783
* Asserts that a variable is of a given type.
17681784
*
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace PHPUnit\Framework;
4+
5+
use PHPUnit\Framework\Attributes\CoversMethod;
6+
use PHPUnit\Framework\Attributes\DataProvider;
7+
use PHPUnit\Framework\Attributes\Small;
8+
use PHPUnit\Framework\Attributes\TestDox;
9+
use stdClass;
10+
11+
#[CoversMethod(Assert::class, 'assertSameDictionaryKeysValuesTest')]
12+
#[TestDox('assertSameDictionaryKeysValuesTest()')]
13+
#[Small]
14+
final class assertSameDictionaryKeysValuesTest extends TestCase
15+
{
16+
/**
17+
* @return non-empty-list<array{0: mixed, 1: mixed}>
18+
*/
19+
public static function successProvider(): array
20+
{
21+
return [
22+
[
23+
[
24+
'string' => 'string',
25+
true => true,
26+
1 => 1,
27+
2 => 2.5,
28+
'object' => new stdClass(),
29+
'array' => [1, 2, 3],
30+
'dictionary' => [
31+
'string' => 'string',
32+
true => true,
33+
1 => 1,
34+
2 => 2.5,
35+
'object' => new stdClass(),
36+
'array' => [1, 2, 3],
37+
],
38+
],
39+
[
40+
'dictionary' => [
41+
'object' => new stdClass(),
42+
'array' => [1, 2, 3],
43+
'string' => 'string',
44+
true => true,
45+
1 => 1,
46+
2 => 2.5,
47+
],
48+
'string' => 'string',
49+
true => true,
50+
1 => 1,
51+
2 => 2.5,
52+
'object' => new stdClass(),
53+
'array' => [1, 2, 3],
54+
],
55+
],
56+
];
57+
}
58+
59+
/**
60+
* @return non-empty-list<array{0: mixed, 1: mixed}>
61+
*/
62+
public static function failureProvider(): array
63+
{
64+
return [
65+
[
66+
[
67+
'string' => 'string',
68+
true => true,
69+
1 => 1,
70+
2 => 2.5,
71+
'object' => new stdClass(),
72+
'array' => [1, 2, 3],
73+
'dictionary' => [
74+
'string' => 'string',
75+
true => true,
76+
1 => 1,
77+
2 => 2.5,
78+
'object' => new stdClass(),
79+
'array' => [1, 2, 3],
80+
],
81+
],
82+
[
83+
'string' => 'string',
84+
true => true,
85+
1 => 1,
86+
],
87+
],
88+
];
89+
}
90+
91+
#[DataProvider('successProvider')]
92+
public function testSucceedsWhenConstraintEvaluatesToTrue(mixed $expected, mixed $actual): void
93+
{
94+
$this->assertSameDictionaryKeysValues($expected, $actual);
95+
}
96+
97+
#[DataProvider('failureProvider')]
98+
public function testFailsWhenConstraintEvaluatesToFalse(mixed $expected, mixed $actual): void
99+
{
100+
$this->expectException(AssertionFailedError::class);
101+
102+
$this->assertSameDictionaryKeysValues($expected, $actual);
103+
}
104+
}

0 commit comments

Comments
 (0)