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