MockObjectComparator.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of sebastian/comparator.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\Comparator;
  11. use function array_keys;
  12. use function assert;
  13. use function str_starts_with;
  14. use PHPUnit\Framework\MockObject\Stub;
  15. /**
  16. * Compares PHPUnit\Framework\MockObject\MockObject instances for equality.
  17. */
  18. final class MockObjectComparator extends ObjectComparator
  19. {
  20. public function accepts(mixed $expected, mixed $actual): bool
  21. {
  22. return $expected instanceof Stub && $actual instanceof Stub;
  23. }
  24. protected function toArray(object $object): array
  25. {
  26. assert($object instanceof Stub);
  27. $array = parent::toArray($object);
  28. foreach (array_keys($array) as $key) {
  29. if (!str_starts_with($key, '__phpunit_')) {
  30. continue;
  31. }
  32. unset($array[$key]);
  33. }
  34. return $array;
  35. }
  36. }