ObjectComparator.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 objects for equality.
  13. */
  14. class ObjectComparator extends ArrayComparator
  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 \is_object($expected) && \is_object($actual);
  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. * @param array $processed List of already processed elements (used to prevent infinite recursion)
  37. *
  38. * @throws ComparisonFailure
  39. */
  40. public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = [])
  41. {
  42. if (\get_class($actual) !== \get_class($expected)) {
  43. throw new ComparisonFailure(
  44. $expected,
  45. $actual,
  46. $this->exporter->export($expected),
  47. $this->exporter->export($actual),
  48. false,
  49. \sprintf(
  50. '%s is not instance of expected class "%s".',
  51. $this->exporter->export($actual),
  52. \get_class($expected)
  53. )
  54. );
  55. }
  56. // don't compare twice to allow for cyclic dependencies
  57. if (\in_array([$actual, $expected], $processed, true) ||
  58. \in_array([$expected, $actual], $processed, true)) {
  59. return;
  60. }
  61. $processed[] = [$actual, $expected];
  62. // don't compare objects if they are identical
  63. // this helps to avoid the error "maximum function nesting level reached"
  64. // CAUTION: this conditional clause is not tested
  65. if ($actual !== $expected) {
  66. try {
  67. parent::assertEquals(
  68. $this->toArray($expected),
  69. $this->toArray($actual),
  70. $delta,
  71. $canonicalize,
  72. $ignoreCase,
  73. $processed
  74. );
  75. } catch (ComparisonFailure $e) {
  76. throw new ComparisonFailure(
  77. $expected,
  78. $actual,
  79. // replace "Array" with "MyClass object"
  80. \substr_replace($e->getExpectedAsString(), \get_class($expected) . ' Object', 0, 5),
  81. \substr_replace($e->getActualAsString(), \get_class($actual) . ' Object', 0, 5),
  82. false,
  83. 'Failed asserting that two objects are equal.'
  84. );
  85. }
  86. }
  87. }
  88. /**
  89. * Converts an object to an array containing all of its private, protected
  90. * and public properties.
  91. *
  92. * @param object $object
  93. *
  94. * @return array
  95. */
  96. protected function toArray($object)
  97. {
  98. return $this->exporter->toArray($object);
  99. }
  100. }