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