Assert.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Testing;
  12. use ArrayAccess;
  13. use Hyperf\Testing\Constraint\ArraySubset;
  14. use Hyperf\Testing\Exception\InvalidArgumentException;
  15. use PHPUnit\Framework\Assert as PHPUnit;
  16. /**
  17. * @internal this class is not meant to be used or overwritten outside the framework itself
  18. */
  19. abstract class Assert extends PHPUnit
  20. {
  21. /**
  22. * Asserts that an array has a specified subset.
  23. *
  24. * @param array|ArrayAccess $subset
  25. * @param array|ArrayAccess $array
  26. */
  27. public static function assertArraySubset($subset, $array, bool $checkForIdentity = false, string $msg = ''): void
  28. {
  29. if (! (is_array($subset) || $subset instanceof ArrayAccess)) { /* @phpstan-ignore-line */
  30. throw InvalidArgumentException::create(1, 'array or ArrayAccess');
  31. }
  32. if (! (is_array($array) || $array instanceof ArrayAccess)) { /* @phpstan-ignore-line */
  33. throw InvalidArgumentException::create(2, 'array or ArrayAccess');
  34. }
  35. $constraint = new ArraySubset($subset, $checkForIdentity);
  36. PHPUnit::assertThat($array, $constraint, $msg);
  37. }
  38. }