comparison vendor/sebastian/comparator/src/ArrayComparator.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
1 <?php 1 <?php
2 /* 2 /*
3 * This file is part of the Comparator package. 3 * This file is part of sebastian/comparator.
4 * 4 *
5 * (c) Sebastian Bergmann <sebastian@phpunit.de> 5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6 * 6 *
7 * For the full copyright and license information, please view the LICENSE 7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code. 8 * file that was distributed with this source code.
16 class ArrayComparator extends Comparator 16 class ArrayComparator extends Comparator
17 { 17 {
18 /** 18 /**
19 * Returns whether the comparator can compare two values. 19 * Returns whether the comparator can compare two values.
20 * 20 *
21 * @param mixed $expected The first value to compare 21 * @param mixed $expected The first value to compare
22 * @param mixed $actual The second value to compare 22 * @param mixed $actual The second value to compare
23 *
23 * @return bool 24 * @return bool
24 */ 25 */
25 public function accepts($expected, $actual) 26 public function accepts($expected, $actual)
26 { 27 {
27 return is_array($expected) && is_array($actual); 28 return \is_array($expected) && \is_array($actual);
28 } 29 }
29 30
30 /** 31 /**
31 * Asserts that two values are equal. 32 * Asserts that two values are equal.
32 * 33 *
37 * @param bool $ignoreCase Case is ignored when set to true 38 * @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 * @param array $processed List of already processed elements (used to prevent infinite recursion)
39 * 40 *
40 * @throws ComparisonFailure 41 * @throws ComparisonFailure
41 */ 42 */
42 public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = array()) 43 public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = [])
43 { 44 {
44 if ($canonicalize) { 45 if ($canonicalize) {
45 sort($expected); 46 \sort($expected);
46 sort($actual); 47 \sort($actual);
47 } 48 }
48 49
49 $remaining = $actual; 50 $remaining = $actual;
50 $expString = $actString = "Array (\n"; 51 $actualAsString = "Array (\n";
51 $equal = true; 52 $expectedAsString = "Array (\n";
53 $equal = true;
52 54
53 foreach ($expected as $key => $value) { 55 foreach ($expected as $key => $value) {
54 unset($remaining[$key]); 56 unset($remaining[$key]);
55 57
56 if (!array_key_exists($key, $actual)) { 58 if (!\array_key_exists($key, $actual)) {
57 $expString .= sprintf( 59 $expectedAsString .= \sprintf(
58 " %s => %s\n", 60 " %s => %s\n",
59 $this->exporter->export($key), 61 $this->exporter->export($key),
60 $this->exporter->shortenedExport($value) 62 $this->exporter->shortenedExport($value)
61 ); 63 );
62 64
67 69
68 try { 70 try {
69 $comparator = $this->factory->getComparatorFor($value, $actual[$key]); 71 $comparator = $this->factory->getComparatorFor($value, $actual[$key]);
70 $comparator->assertEquals($value, $actual[$key], $delta, $canonicalize, $ignoreCase, $processed); 72 $comparator->assertEquals($value, $actual[$key], $delta, $canonicalize, $ignoreCase, $processed);
71 73
72 $expString .= sprintf( 74 $expectedAsString .= \sprintf(
73 " %s => %s\n", 75 " %s => %s\n",
74 $this->exporter->export($key), 76 $this->exporter->export($key),
75 $this->exporter->shortenedExport($value) 77 $this->exporter->shortenedExport($value)
76 ); 78 );
77 $actString .= sprintf( 79
80 $actualAsString .= \sprintf(
78 " %s => %s\n", 81 " %s => %s\n",
79 $this->exporter->export($key), 82 $this->exporter->export($key),
80 $this->exporter->shortenedExport($actual[$key]) 83 $this->exporter->shortenedExport($actual[$key])
81 ); 84 );
82 } catch (ComparisonFailure $e) { 85 } catch (ComparisonFailure $e) {
83 $expString .= sprintf( 86 $expectedAsString .= \sprintf(
84 " %s => %s\n", 87 " %s => %s\n",
85 $this->exporter->export($key), 88 $this->exporter->export($key),
86 $e->getExpectedAsString() 89 $e->getExpectedAsString() ? $this->indent($e->getExpectedAsString()) : $this->exporter->shortenedExport($e->getExpected())
87 ? $this->indent($e->getExpectedAsString())
88 : $this->exporter->shortenedExport($e->getExpected())
89 ); 90 );
90 91
91 $actString .= sprintf( 92 $actualAsString .= \sprintf(
92 " %s => %s\n", 93 " %s => %s\n",
93 $this->exporter->export($key), 94 $this->exporter->export($key),
94 $e->getActualAsString() 95 $e->getActualAsString() ? $this->indent($e->getActualAsString()) : $this->exporter->shortenedExport($e->getActual())
95 ? $this->indent($e->getActualAsString())
96 : $this->exporter->shortenedExport($e->getActual())
97 ); 96 );
98 97
99 $equal = false; 98 $equal = false;
100 } 99 }
101 } 100 }
102 101
103 foreach ($remaining as $key => $value) { 102 foreach ($remaining as $key => $value) {
104 $actString .= sprintf( 103 $actualAsString .= \sprintf(
105 " %s => %s\n", 104 " %s => %s\n",
106 $this->exporter->export($key), 105 $this->exporter->export($key),
107 $this->exporter->shortenedExport($value) 106 $this->exporter->shortenedExport($value)
108 ); 107 );
109 108
110 $equal = false; 109 $equal = false;
111 } 110 }
112 111
113 $expString .= ')'; 112 $expectedAsString .= ')';
114 $actString .= ')'; 113 $actualAsString .= ')';
115 114
116 if (!$equal) { 115 if (!$equal) {
117 throw new ComparisonFailure( 116 throw new ComparisonFailure(
118 $expected, 117 $expected,
119 $actual, 118 $actual,
120 $expString, 119 $expectedAsString,
121 $actString, 120 $actualAsString,
122 false, 121 false,
123 'Failed asserting that two arrays are equal.' 122 'Failed asserting that two arrays are equal.'
124 ); 123 );
125 } 124 }
126 } 125 }
127 126
128 protected function indent($lines) 127 protected function indent($lines)
129 { 128 {
130 return trim(str_replace("\n", "\n ", $lines)); 129 return \trim(\str_replace("\n", "\n ", $lines));
131 } 130 }
132 } 131 }