Skip to content

Commit e2913a6

Browse files
Initial work on #6423
1 parent fcbbb46 commit e2913a6

15 files changed

+3669
-0
lines changed

src/Framework/Assert.php

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use Countable;
2121
use Generator;
2222
use PHPUnit\Framework\Constraint\ArrayHasKey;
23+
use PHPUnit\Framework\Constraint\ArraysAreEqual;
24+
use PHPUnit\Framework\Constraint\ArraysAreIdentical;
2325
use PHPUnit\Framework\Constraint\Callback;
2426
use PHPUnit\Framework\Constraint\Constraint;
2527
use PHPUnit\Framework\Constraint\Count;
@@ -207,6 +209,154 @@ final public static function assertIsList(mixed $array, string $message = ''): v
207209
);
208210
}
209211

212+
/**
213+
* Assert that two arrays are identical.
214+
*
215+
* The (key, value) relationship matters, the order of the (key, value) pairs in the array matters, and keys as well as values are compared strictly.
216+
*
217+
* @param array<mixed> $expected
218+
* @param array<mixed> $actual
219+
*
220+
* @throws ExpectationFailedException
221+
*/
222+
final public static function assertArraysAreIdentical(array $expected, array $actual, string $message = ''): void
223+
{
224+
self::assertThat(
225+
$actual,
226+
new ArraysAreIdentical($expected, true, true),
227+
$message,
228+
);
229+
}
230+
231+
/**
232+
* Assert that two arrays are identical while ignoring the order of their values.
233+
*
234+
* The (key, value) relationship matters, the order of the (key, value) pairs in the array does not matter, and keys as well as values are compared strictly.
235+
*
236+
* @param array<mixed> $expected
237+
* @param array<mixed> $actual
238+
*
239+
* @throws ExpectationFailedException
240+
*/
241+
final public static function assertArraysAreIdenticalIgnoringOrder(array $expected, array $actual, string $message = ''): void
242+
{
243+
self::assertThat(
244+
$actual,
245+
new ArraysAreIdentical($expected, true, false),
246+
$message,
247+
);
248+
}
249+
250+
/**
251+
* Assert that two arrays have identical values.
252+
*
253+
* The (key, value) relationship does not matter, the order of the (key, value) pairs in the array matters, and values are compared strictly.
254+
*
255+
* @param array<mixed> $expected
256+
* @param array<mixed> $actual
257+
*
258+
* @throws ExpectationFailedException
259+
*/
260+
final public static function assertArraysHaveIdenticalValues(array $expected, array $actual, string $message = ''): void
261+
{
262+
self::assertThat(
263+
$actual,
264+
new ArraysAreIdentical($expected, false, true),
265+
$message,
266+
);
267+
}
268+
269+
/**
270+
* Assert that two arrays have identical values while ignoring the order of these values.
271+
*
272+
* The (key, value) relationship does not matter, the order of the (key, value) pairs in the array does not matter, and values are compared strictly.
273+
*
274+
* @param array<mixed> $expected
275+
* @param array<mixed> $actual
276+
*
277+
* @throws ExpectationFailedException
278+
*/
279+
final public static function assertArraysHaveIdenticalValuesIgnoringOrder(array $expected, array $actual, string $message = ''): void
280+
{
281+
self::assertThat(
282+
$actual,
283+
new ArraysAreIdentical($expected, false, false),
284+
$message,
285+
);
286+
}
287+
288+
/**
289+
* Assert that two arrays are equal.
290+
*
291+
* The (key, value) relationship matters, the order of the (key, value) pairs in the array matters, and keys as well as values are compared loosely.
292+
*
293+
* @param array<mixed> $expected
294+
* @param array<mixed> $actual
295+
*/
296+
final public static function assertArraysAreEqual(array $expected, array $actual, string $message = ''): void
297+
{
298+
self::assertThat(
299+
$actual,
300+
new ArraysAreEqual($expected, true, true),
301+
$message,
302+
);
303+
}
304+
305+
/**
306+
* Assert that two arrays are equal while ignoring the order of their values.
307+
*
308+
* The (key, value) relationship matters, the order of the (key, value) pairs in the array does not matter, and keys as well as values are compared loosely.
309+
*
310+
* @param array<mixed> $expected
311+
* @param array<mixed> $actual
312+
*
313+
* @throws ExpectationFailedException
314+
*/
315+
final public static function assertArraysAreEqualIgnoringOrder(array $expected, array $actual, string $message = ''): void
316+
{
317+
self::assertThat(
318+
$actual,
319+
new ArraysAreEqual($expected, true, false),
320+
$message,
321+
);
322+
}
323+
324+
/**
325+
* Assert that two arrays have equal values.
326+
*
327+
* The (key, value) relationship does not matter, the order of the (key, value) pairs in the array matters, and values are compared loosely.
328+
*
329+
* @param array<mixed> $expected
330+
* @param array<mixed> $actual
331+
*/
332+
final public static function assertArraysHaveEqualValues(array $expected, array $actual, string $message = ''): void
333+
{
334+
self::assertThat(
335+
$actual,
336+
new ArraysAreEqual($expected, false, true),
337+
$message,
338+
);
339+
}
340+
341+
/**
342+
* Assert that two arrays have equal values while ignoring the order of these values.
343+
*
344+
* The (key, value) relationship does not matter, the order of the (key, value) pairs in the array does not matter, and values are compared loosely.
345+
*
346+
* @param array<mixed> $expected
347+
* @param array<mixed> $actual
348+
*
349+
* @throws ExpectationFailedException
350+
*/
351+
final public static function assertArraysHaveEqualValuesIgnoringOrder(array $expected, array $actual, string $message = ''): void
352+
{
353+
self::assertThat(
354+
$actual,
355+
new ArraysAreEqual($expected, false, false),
356+
$message,
357+
);
358+
}
359+
210360
/**
211361
* Asserts that a haystack contains a needle.
212362
*

src/Framework/Assert/Functions.php

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,176 @@ function assertIsList(mixed $array, string $message = ''): void
201201
}
202202
}
203203

204+
if (!function_exists('PHPUnit\Framework\assertArraysAreIdentical')) {
205+
/**
206+
* Assert that two arrays are identical.
207+
*
208+
* The (key, value) relationship matters, the order of the (key, value) pairs in the array matters, and keys as well as values are compared strictly.
209+
* This is essentially an alias for assertSame().
210+
*
211+
* @param array<mixed> $expected
212+
* @param array<mixed> $actual
213+
*
214+
* @throws ExpectationFailedException
215+
*
216+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
217+
*
218+
* @see Assert::assertArraysAreIdentical
219+
*/
220+
function assertArraysAreIdentical(array $expected, array $actual, string $message = ''): void
221+
{
222+
Assert::assertArraysAreIdentical(...func_get_args());
223+
}
224+
}
225+
226+
if (!function_exists('PHPUnit\Framework\assertArraysAreEqual')) {
227+
/**
228+
* Assert that two arrays are equal.
229+
*
230+
* The (key, value) relationship matters, the order of the (key, value) pairs in the array matters, and keys as well as values are compared loosely.
231+
*
232+
* @param array<mixed> $expected
233+
* @param array<mixed> $actual
234+
*
235+
* @throws ExpectationFailedException
236+
*
237+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
238+
*
239+
* @see Assert::assertArraysAreEqual
240+
*/
241+
function assertArraysAreEqual(array $expected, array $actual, string $message = ''): void
242+
{
243+
Assert::assertArraysAreEqual(...func_get_args());
244+
}
245+
}
246+
247+
if (!function_exists('PHPUnit\Framework\assertArraysAreIdenticalIgnoringOrder')) {
248+
/**
249+
* Assert that two arrays are identical while ignoring the order of their values.
250+
*
251+
* The (key, value) relationship matters, the order of the (key, value) pairs in the array does not matter, and keys as well as values are compared strictly.
252+
*
253+
* @param array<mixed> $expected
254+
* @param array<mixed> $actual
255+
*
256+
* @throws ExpectationFailedException
257+
*
258+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
259+
*
260+
* @see Assert::assertArraysAreIdenticalIgnoringOrder
261+
*/
262+
function assertArraysAreIdenticalIgnoringOrder(array $expected, array $actual, string $message = ''): void
263+
{
264+
Assert::assertArraysAreIdenticalIgnoringOrder(...func_get_args());
265+
}
266+
}
267+
268+
if (!function_exists('PHPUnit\Framework\assertArraysAreEqualIgnoringOrder')) {
269+
/**
270+
* Assert that two arrays are equal while ignoring the order of their values.
271+
*
272+
* The (key, value) relationship matters, the order of the (key, value) pairs in the array does not matter, and keys as well as values are compared loosely.
273+
* This is essentially an alias for assertEquals().
274+
*
275+
* @param array<mixed> $expected
276+
* @param array<mixed> $actual
277+
*
278+
* @throws ExpectationFailedException
279+
*
280+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
281+
*
282+
* @see Assert::assertArraysAreEqualIgnoringOrder
283+
*/
284+
function assertArraysAreEqualIgnoringOrder(array $expected, array $actual, string $message = ''): void
285+
{
286+
Assert::assertArraysAreEqualIgnoringOrder(...func_get_args());
287+
}
288+
}
289+
290+
if (!function_exists('PHPUnit\Framework\assertArraysHaveIdenticalValues')) {
291+
/**
292+
* Assert that two arrays have identical values.
293+
*
294+
* The (key, value) relationship does not matter, the order of the (key, value) pairs in the array matters, and values are compared strictly.
295+
*
296+
* @param array<mixed> $expected
297+
* @param array<mixed> $actual
298+
*
299+
* @throws ExpectationFailedException
300+
*
301+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
302+
*
303+
* @see Assert::assertArraysHaveIdenticalValues
304+
*/
305+
function assertArraysHaveIdenticalValues(array $expected, array $actual, string $message = ''): void
306+
{
307+
Assert::assertArraysHaveIdenticalValues(...func_get_args());
308+
}
309+
}
310+
311+
if (!function_exists('PHPUnit\Framework\assertArraysHaveEqualValues')) {
312+
/**
313+
* Assert that two arrays have equal values.
314+
*
315+
* The (key, value) relationship does not matter, the order of the (key, value) pairs in the array matters, and values are compared loosely.
316+
*
317+
* @param array<mixed> $expected
318+
* @param array<mixed> $actual
319+
*
320+
* @throws ExpectationFailedException
321+
*
322+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
323+
*
324+
* @see Assert::assertArraysHaveEqualValues
325+
*/
326+
function assertArraysHaveEqualValues(array $expected, array $actual, string $message = ''): void
327+
{
328+
Assert::assertArraysHaveEqualValues(...func_get_args());
329+
}
330+
}
331+
332+
if (!function_exists('PHPUnit\Framework\assertArraysHaveIdenticalValuesIgnoringOrder')) {
333+
/**
334+
* Assert that two arrays have identical values while ignoring the order of these values.
335+
*
336+
* The (key, value) relationship does not matter, the order of the (key, value) pairs in the array does not matter, and values are compared strictly.
337+
*
338+
* @param array<mixed> $expected
339+
* @param array<mixed> $actual
340+
*
341+
* @throws ExpectationFailedException
342+
*
343+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
344+
*
345+
* @see Assert::assertArraysHaveIdenticalValuesIgnoringOrder
346+
*/
347+
function assertArraysHaveIdenticalValuesIgnoringOrder(array $expected, array $actual, string $message = ''): void
348+
{
349+
Assert::assertArraysHaveIdenticalValuesIgnoringOrder(...func_get_args());
350+
}
351+
}
352+
353+
if (!function_exists('PHPUnit\Framework\assertArraysHaveEqualValuesIgnoringOrder')) {
354+
/**
355+
* Assert that two arrays have equal values while ignoring the order of these values.
356+
*
357+
* The (key, value) relationship does not matter, the order of the (key, value) pairs in the array does not matter, and values are compared loosely.
358+
*
359+
* @param array<mixed> $expected
360+
* @param array<mixed> $actual
361+
*
362+
* @throws ExpectationFailedException
363+
*
364+
* @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
365+
*
366+
* @see Assert::assertArraysHaveEqualValuesIgnoringOrder
367+
*/
368+
function assertArraysHaveEqualValuesIgnoringOrder(array $expected, array $actual, string $message = ''): void
369+
{
370+
Assert::assertArraysHaveEqualValuesIgnoringOrder(...func_get_args());
371+
}
372+
}
373+
204374
if (!function_exists('PHPUnit\Framework\assertContains')) {
205375
/**
206376
* Asserts that a haystack contains a needle.

0 commit comments

Comments
 (0)