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 array_key_exists;
13
use function is_array;
14
use function sort;
15
use function sprintf;
16
use function str_replace;
17
use function trim;
18
 
19
/**
20
 * Compares arrays for equality.
21
 *
22
 * Arrays are equal if they contain the same key-value pairs.
23
 * The order of the keys does not matter.
24
 * The types of key-value pairs do not matter.
25
 */
26
class ArrayComparator extends Comparator
27
{
28
    /**
29
     * Returns whether the comparator can compare two values.
30
     *
31
     * @param mixed $expected The first value to compare
32
     * @param mixed $actual   The second value to compare
33
     *
34
     * @return bool
35
     */
36
    public function accepts($expected, $actual)
37
    {
38
        return is_array($expected) && is_array($actual);
39
    }
40
 
41
    /**
42
     * Asserts that two arrays are equal.
43
     *
44
     * @param mixed $expected     First value to compare
45
     * @param mixed $actual       Second value to compare
46
     * @param float $delta        Allowed numerical distance between two values to consider them equal
47
     * @param bool  $canonicalize Arrays are sorted before comparison when set to true
48
     * @param bool  $ignoreCase   Case is ignored when set to true
49
     * @param array $processed    List of already processed elements (used to prevent infinite recursion)
50
     *
51
     * @throws ComparisonFailure
52
     */
53
    public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = [])/*: void*/
54
    {
55
        if ($canonicalize) {
56
            sort($expected);
57
            sort($actual);
58
        }
59
 
60
        $remaining        = $actual;
61
        $actualAsString   = "Array (\n";
62
        $expectedAsString = "Array (\n";
63
        $equal            = true;
64
 
65
        foreach ($expected as $key => $value) {
66
            unset($remaining[$key]);
67
 
68
            if (!array_key_exists($key, $actual)) {
69
                $expectedAsString .= sprintf(
70
                    "    %s => %s\n",
71
                    $this->exporter->export($key),
72
                    $this->exporter->shortenedExport($value)
73
                );
74
 
75
                $equal = false;
76
 
77
                continue;
78
            }
79
 
80
            try {
81
                $comparator = $this->factory->getComparatorFor($value, $actual[$key]);
82
                $comparator->assertEquals($value, $actual[$key], $delta, $canonicalize, $ignoreCase, $processed);
83
 
84
                $expectedAsString .= sprintf(
85
                    "    %s => %s\n",
86
                    $this->exporter->export($key),
87
                    $this->exporter->shortenedExport($value)
88
                );
89
 
90
                $actualAsString .= sprintf(
91
                    "    %s => %s\n",
92
                    $this->exporter->export($key),
93
                    $this->exporter->shortenedExport($actual[$key])
94
                );
95
            } catch (ComparisonFailure $e) {
96
                $expectedAsString .= sprintf(
97
                    "    %s => %s\n",
98
                    $this->exporter->export($key),
99
                    $e->getExpectedAsString() ? $this->indent($e->getExpectedAsString()) : $this->exporter->shortenedExport($e->getExpected())
100
                );
101
 
102
                $actualAsString .= sprintf(
103
                    "    %s => %s\n",
104
                    $this->exporter->export($key),
105
                    $e->getActualAsString() ? $this->indent($e->getActualAsString()) : $this->exporter->shortenedExport($e->getActual())
106
                );
107
 
108
                $equal = false;
109
            }
110
        }
111
 
112
        foreach ($remaining as $key => $value) {
113
            $actualAsString .= sprintf(
114
                "    %s => %s\n",
115
                $this->exporter->export($key),
116
                $this->exporter->shortenedExport($value)
117
            );
118
 
119
            $equal = false;
120
        }
121
 
122
        $expectedAsString .= ')';
123
        $actualAsString .= ')';
124
 
125
        if (!$equal) {
126
            throw new ComparisonFailure(
127
                $expected,
128
                $actual,
129
                $expectedAsString,
130
                $actualAsString,
131
                false,
132
                'Failed asserting that two arrays are equal.'
133
            );
134
        }
135
    }
136
 
137
    protected function indent($lines)
138
    {
139
        return trim(str_replace("\n", "\n    ", $lines));
140
    }
141
}