SplObjectStorageComparator.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 assert;
  12. use SebastianBergmann\Exporter\Exporter;
  13. use SplObjectStorage;
  14. final class SplObjectStorageComparator extends Comparator
  15. {
  16. public function accepts(mixed $expected, mixed $actual): bool
  17. {
  18. return $expected instanceof SplObjectStorage && $actual instanceof SplObjectStorage;
  19. }
  20. /**
  21. * @throws ComparisonFailure
  22. */
  23. public function assertEquals(mixed $expected, mixed $actual, float $delta = 0.0, bool $canonicalize = false, bool $ignoreCase = false): void
  24. {
  25. assert($expected instanceof SplObjectStorage);
  26. assert($actual instanceof SplObjectStorage);
  27. $exporter = new Exporter;
  28. foreach ($actual as $object) {
  29. if (!$expected->contains($object)) {
  30. throw new ComparisonFailure(
  31. $expected,
  32. $actual,
  33. $exporter->export($expected),
  34. $exporter->export($actual),
  35. 'Failed asserting that two objects are equal.'
  36. );
  37. }
  38. }
  39. foreach ($expected as $object) {
  40. if (!$actual->contains($object)) {
  41. throw new ComparisonFailure(
  42. $expected,
  43. $actual,
  44. $exporter->export($expected),
  45. $exporter->export($actual),
  46. 'Failed asserting that two objects are equal.'
  47. );
  48. }
  49. }
  50. }
  51. }