Chris@0
|
1 <?php
|
Chris@0
|
2 /*
|
Chris@14
|
3 * This file is part of sebastian/comparator.
|
Chris@0
|
4 *
|
Chris@0
|
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
|
Chris@0
|
6 *
|
Chris@0
|
7 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
8 * file that was distributed with this source code.
|
Chris@0
|
9 */
|
Chris@0
|
10
|
Chris@0
|
11 namespace SebastianBergmann\Comparator;
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Compares objects for equality.
|
Chris@0
|
15 */
|
Chris@0
|
16 class ObjectComparator extends ArrayComparator
|
Chris@0
|
17 {
|
Chris@0
|
18 /**
|
Chris@0
|
19 * Returns whether the comparator can compare two values.
|
Chris@0
|
20 *
|
Chris@14
|
21 * @param mixed $expected The first value to compare
|
Chris@14
|
22 * @param mixed $actual The second value to compare
|
Chris@14
|
23 *
|
Chris@0
|
24 * @return bool
|
Chris@0
|
25 */
|
Chris@0
|
26 public function accepts($expected, $actual)
|
Chris@0
|
27 {
|
Chris@14
|
28 return \is_object($expected) && \is_object($actual);
|
Chris@0
|
29 }
|
Chris@0
|
30
|
Chris@0
|
31 /**
|
Chris@0
|
32 * Asserts that two values are equal.
|
Chris@0
|
33 *
|
Chris@0
|
34 * @param mixed $expected First value to compare
|
Chris@0
|
35 * @param mixed $actual Second value to compare
|
Chris@0
|
36 * @param float $delta Allowed numerical distance between two values to consider them equal
|
Chris@0
|
37 * @param bool $canonicalize Arrays are sorted before comparison when set to true
|
Chris@0
|
38 * @param bool $ignoreCase Case is ignored when set to true
|
Chris@0
|
39 * @param array $processed List of already processed elements (used to prevent infinite recursion)
|
Chris@0
|
40 *
|
Chris@0
|
41 * @throws ComparisonFailure
|
Chris@0
|
42 */
|
Chris@14
|
43 public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = [])
|
Chris@0
|
44 {
|
Chris@14
|
45 if (\get_class($actual) !== \get_class($expected)) {
|
Chris@0
|
46 throw new ComparisonFailure(
|
Chris@0
|
47 $expected,
|
Chris@0
|
48 $actual,
|
Chris@0
|
49 $this->exporter->export($expected),
|
Chris@0
|
50 $this->exporter->export($actual),
|
Chris@0
|
51 false,
|
Chris@14
|
52 \sprintf(
|
Chris@0
|
53 '%s is not instance of expected class "%s".',
|
Chris@0
|
54 $this->exporter->export($actual),
|
Chris@14
|
55 \get_class($expected)
|
Chris@0
|
56 )
|
Chris@0
|
57 );
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 // don't compare twice to allow for cyclic dependencies
|
Chris@14
|
61 if (\in_array([$actual, $expected], $processed, true) ||
|
Chris@14
|
62 \in_array([$expected, $actual], $processed, true)) {
|
Chris@0
|
63 return;
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@14
|
66 $processed[] = [$actual, $expected];
|
Chris@0
|
67
|
Chris@0
|
68 // don't compare objects if they are identical
|
Chris@0
|
69 // this helps to avoid the error "maximum function nesting level reached"
|
Chris@0
|
70 // CAUTION: this conditional clause is not tested
|
Chris@0
|
71 if ($actual !== $expected) {
|
Chris@0
|
72 try {
|
Chris@0
|
73 parent::assertEquals(
|
Chris@0
|
74 $this->toArray($expected),
|
Chris@0
|
75 $this->toArray($actual),
|
Chris@0
|
76 $delta,
|
Chris@0
|
77 $canonicalize,
|
Chris@0
|
78 $ignoreCase,
|
Chris@0
|
79 $processed
|
Chris@0
|
80 );
|
Chris@0
|
81 } catch (ComparisonFailure $e) {
|
Chris@0
|
82 throw new ComparisonFailure(
|
Chris@0
|
83 $expected,
|
Chris@0
|
84 $actual,
|
Chris@0
|
85 // replace "Array" with "MyClass object"
|
Chris@14
|
86 \substr_replace($e->getExpectedAsString(), \get_class($expected) . ' Object', 0, 5),
|
Chris@14
|
87 \substr_replace($e->getActualAsString(), \get_class($actual) . ' Object', 0, 5),
|
Chris@0
|
88 false,
|
Chris@0
|
89 'Failed asserting that two objects are equal.'
|
Chris@0
|
90 );
|
Chris@0
|
91 }
|
Chris@0
|
92 }
|
Chris@0
|
93 }
|
Chris@0
|
94
|
Chris@0
|
95 /**
|
Chris@0
|
96 * Converts an object to an array containing all of its private, protected
|
Chris@0
|
97 * and public properties.
|
Chris@0
|
98 *
|
Chris@14
|
99 * @param object $object
|
Chris@14
|
100 *
|
Chris@0
|
101 * @return array
|
Chris@0
|
102 */
|
Chris@0
|
103 protected function toArray($object)
|
Chris@0
|
104 {
|
Chris@0
|
105 return $this->exporter->toArray($object);
|
Chris@0
|
106 }
|
Chris@0
|
107 }
|