SplObjectStorageComparator.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  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. /**
  12. * Compares \SplObjectStorage instances for equality.
  13. */
  14. class SplObjectStorageComparator extends Comparator
  15. {
  16. /**
  17. * Returns whether the comparator can compare two values.
  18. *
  19. * @param mixed $expected The first value to compare
  20. * @param mixed $actual The second value to compare
  21. *
  22. * @return bool
  23. */
  24. public function accepts($expected, $actual)
  25. {
  26. return $expected instanceof \SplObjectStorage && $actual instanceof \SplObjectStorage;
  27. }
  28. /**
  29. * Asserts that two values are equal.
  30. *
  31. * @param mixed $expected First value to compare
  32. * @param mixed $actual Second value to compare
  33. * @param float $delta Allowed numerical distance between two values to consider them equal
  34. * @param bool $canonicalize Arrays are sorted before comparison when set to true
  35. * @param bool $ignoreCase Case is ignored when set to true
  36. *
  37. * @throws ComparisonFailure
  38. */
  39. public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)
  40. {
  41. foreach ($actual as $object) {
  42. if (!$expected->contains($object)) {
  43. throw new ComparisonFailure(
  44. $expected,
  45. $actual,
  46. $this->exporter->export($expected),
  47. $this->exporter->export($actual),
  48. false,
  49. 'Failed asserting that two objects are equal.'
  50. );
  51. }
  52. }
  53. foreach ($expected as $object) {
  54. if (!$actual->contains($object)) {
  55. throw new ComparisonFailure(
  56. $expected,
  57. $actual,
  58. $this->exporter->export($expected),
  59. $this->exporter->export($actual),
  60. false,
  61. 'Failed asserting that two objects are equal.'
  62. );
  63. }
  64. }
  65. }
  66. }