Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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