exceptions
1 year ago
ArrayComparator.php
1 year ago
Comparator.php
1 year ago
ComparisonFailure.php
1 year ago
DOMNodeComparator.php
1 year ago
DateTimeComparator.php
1 year ago
DoubleComparator.php
1 year ago
ExceptionComparator.php
1 year ago
Factory.php
1 year ago
MockObjectComparator.php
1 year ago
NumericComparator.php
1 year ago
ObjectComparator.php
1 year ago
ResourceComparator.php
1 year ago
ScalarComparator.php
1 year ago
SplObjectStorageComparator.php
1 year ago
TypeComparator.php
1 year ago
ScalarComparator.php
100 lines
| 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 is_bool; |
| 13 | use function is_object; |
| 14 | use function is_scalar; |
| 15 | use function is_string; |
| 16 | use function method_exists; |
| 17 | use function sprintf; |
| 18 | use function strtolower; |
| 19 | |
| 20 | /** |
| 21 | * Compares scalar or NULL values for equality. |
| 22 | */ |
| 23 | class ScalarComparator extends Comparator |
| 24 | { |
| 25 | /** |
| 26 | * Returns whether the comparator can compare two values. |
| 27 | * |
| 28 | * @param mixed $expected The first value to compare |
| 29 | * @param mixed $actual The second value to compare |
| 30 | * |
| 31 | * @return bool |
| 32 | * |
| 33 | * @since Method available since Release 3.6.0 |
| 34 | */ |
| 35 | public function accepts($expected, $actual) |
| 36 | { |
| 37 | return ((is_scalar($expected) xor null === $expected) && |
| 38 | (is_scalar($actual) xor null === $actual)) |
| 39 | // allow comparison between strings and objects featuring __toString() |
| 40 | || (is_string($expected) && is_object($actual) && method_exists($actual, '__toString')) |
| 41 | || (is_object($expected) && method_exists($expected, '__toString') && is_string($actual)); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Asserts that two values are equal. |
| 46 | * |
| 47 | * @param mixed $expected First value to compare |
| 48 | * @param mixed $actual Second value to compare |
| 49 | * @param float $delta Allowed numerical distance between two values to consider them equal |
| 50 | * @param bool $canonicalize Arrays are sorted before comparison when set to true |
| 51 | * @param bool $ignoreCase Case is ignored when set to true |
| 52 | * |
| 53 | * @throws ComparisonFailure |
| 54 | */ |
| 55 | public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)/*: void*/ |
| 56 | { |
| 57 | $expectedToCompare = $expected; |
| 58 | $actualToCompare = $actual; |
| 59 | |
| 60 | // always compare as strings to avoid strange behaviour |
| 61 | // otherwise 0 == 'Foobar' |
| 62 | if ((is_string($expected) && !is_bool($actual)) || (is_string($actual) && !is_bool($expected))) { |
| 63 | $expectedToCompare = (string) $expectedToCompare; |
| 64 | $actualToCompare = (string) $actualToCompare; |
| 65 | |
| 66 | if ($ignoreCase) { |
| 67 | $expectedToCompare = strtolower($expectedToCompare); |
| 68 | $actualToCompare = strtolower($actualToCompare); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | if ($expectedToCompare !== $actualToCompare && is_string($expected) && is_string($actual)) { |
| 73 | throw new ComparisonFailure( |
| 74 | $expected, |
| 75 | $actual, |
| 76 | $this->exporter->export($expected), |
| 77 | $this->exporter->export($actual), |
| 78 | false, |
| 79 | 'Failed asserting that two strings are equal.' |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | if ($expectedToCompare != $actualToCompare) { |
| 84 | throw new ComparisonFailure( |
| 85 | $expected, |
| 86 | $actual, |
| 87 | // no diff is required |
| 88 | '', |
| 89 | '', |
| 90 | false, |
| 91 | sprintf( |
| 92 | 'Failed asserting that %s matches expected %s.', |
| 93 | $this->exporter->export($actual), |
| 94 | $this->exporter->export($expected) |
| 95 | ) |
| 96 | ); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 |