master

laravel/framework

Last updated at: 29/12/2023 09:25

Assert.php

TLDR

The Assert class in the file src/Illuminate/Testing/Assert.php provides a method assertArraySubset that asserts if an array has a specified subset.

Methods

assertArraySubset

This method asserts if an array has a specified subset. It takes the following parameters:

  • $subset (ArrayAccess|array): The subset to be checked.
  • $array (ArrayAccess|array): The main array to check against.
  • $checkForIdentity (bool, optional): Specifies whether to perform an identity check (default is false).
  • $msg (string, optional): A custom error message to display (default is empty string).

This method throws an InvalidArgumentException if either $subset or $array is not an array or ArrayAccess. It uses the ArraySubset constraint to perform the assertion.

<?php

namespace Illuminate\Testing;

use ArrayAccess;
use Illuminate\Testing\Constraints\ArraySubset;
use Illuminate\Testing\Exceptions\InvalidArgumentException;
use PHPUnit\Framework\Assert as PHPUnit;

/**
 * @internal This class is not meant to be used or overwritten outside the framework itself.
 */
abstract class Assert extends PHPUnit
{
    /**
     * Asserts that an array has a specified subset.
     *
     * @param  \ArrayAccess|array  $subset
     * @param  \ArrayAccess|array  $array
     * @param  bool  $checkForIdentity
     * @param  string  $msg
     * @return void
     */
    public static function assertArraySubset($subset, $array, bool $checkForIdentity = false, string $msg = ''): void
    {
        if (! (is_array($subset) || $subset instanceof ArrayAccess)) {
            throw InvalidArgumentException::create(1, 'array or ArrayAccess');
        }

        if (! (is_array($array) || $array instanceof ArrayAccess)) {
            throw InvalidArgumentException::create(2, 'array or ArrayAccess');
        }

        $constraint = new ArraySubset($subset, $checkForIdentity);

        PHPUnit::assertThat($array, $constraint, $msg);
    }
}