Upgrade framework

This commit is contained in:
2023-11-14 16:54:35 +01:00
parent 1648a5cd42
commit 4fcf6fffcc
10548 changed files with 693138 additions and 466698 deletions

View File

@@ -1,25 +1,36 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the Comparator package.
* This file is part of sebastian/comparator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\Comparator;
use function array_key_exists;
use function is_array;
use function sort;
use function sprintf;
use function str_replace;
use function trim;
/**
* Compares arrays for equality.
*
* Arrays are equal if they contain the same key-value pairs.
* The order of the keys does not matter.
* The types of key-value pairs do not matter.
*/
class ArrayComparator extends Comparator
{
/**
* Returns whether the comparator can compare two values.
*
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
*
* @return bool
*/
public function accepts($expected, $actual)
@@ -28,7 +39,7 @@ class ArrayComparator extends Comparator
}
/**
* Asserts that two values are equal.
* Asserts that two arrays are equal.
*
* @param mixed $expected First value to compare
* @param mixed $actual Second value to compare
@@ -39,22 +50,23 @@ class ArrayComparator extends Comparator
*
* @throws ComparisonFailure
*/
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = array())
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = [])/*: void*/
{
if ($canonicalize) {
sort($expected);
sort($actual);
}
$remaining = $actual;
$expString = $actString = "Array (\n";
$equal = true;
$remaining = $actual;
$actualAsString = "Array (\n";
$expectedAsString = "Array (\n";
$equal = true;
foreach ($expected as $key => $value) {
unset($remaining[$key]);
if (!array_key_exists($key, $actual)) {
$expString .= sprintf(
$expectedAsString .= sprintf(
" %s => %s\n",
$this->exporter->export($key),
$this->exporter->shortenedExport($value)
@@ -69,31 +81,28 @@ class ArrayComparator extends Comparator
$comparator = $this->factory->getComparatorFor($value, $actual[$key]);
$comparator->assertEquals($value, $actual[$key], $delta, $canonicalize, $ignoreCase, $processed);
$expString .= sprintf(
$expectedAsString .= sprintf(
" %s => %s\n",
$this->exporter->export($key),
$this->exporter->shortenedExport($value)
);
$actString .= sprintf(
$actualAsString .= sprintf(
" %s => %s\n",
$this->exporter->export($key),
$this->exporter->shortenedExport($actual[$key])
);
} catch (ComparisonFailure $e) {
$expString .= sprintf(
$expectedAsString .= sprintf(
" %s => %s\n",
$this->exporter->export($key),
$e->getExpectedAsString()
? $this->indent($e->getExpectedAsString())
: $this->exporter->shortenedExport($e->getExpected())
$e->getExpectedAsString() ? $this->indent($e->getExpectedAsString()) : $this->exporter->shortenedExport($e->getExpected())
);
$actString .= sprintf(
$actualAsString .= sprintf(
" %s => %s\n",
$this->exporter->export($key),
$e->getActualAsString()
? $this->indent($e->getActualAsString())
: $this->exporter->shortenedExport($e->getActual())
$e->getActualAsString() ? $this->indent($e->getActualAsString()) : $this->exporter->shortenedExport($e->getActual())
);
$equal = false;
@@ -101,7 +110,7 @@ class ArrayComparator extends Comparator
}
foreach ($remaining as $key => $value) {
$actString .= sprintf(
$actualAsString .= sprintf(
" %s => %s\n",
$this->exporter->export($key),
$this->exporter->shortenedExport($value)
@@ -110,15 +119,15 @@ class ArrayComparator extends Comparator
$equal = false;
}
$expString .= ')';
$actString .= ')';
$expectedAsString .= ')';
$actualAsString .= ')';
if (!$equal) {
throw new ComparisonFailure(
$expected,
$actual,
$expString,
$actString,
$expectedAsString,
$actualAsString,
false,
'Failed asserting that two arrays are equal.'
);

View File

@@ -1,13 +1,12 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the Comparator package.
* This file is part of sebastian/comparator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\Comparator;
use SebastianBergmann\Exporter\Exporter;
@@ -32,10 +31,7 @@ abstract class Comparator
$this->exporter = new Exporter;
}
/**
* @param Factory $factory
*/
public function setFactory(Factory $factory)
public function setFactory(Factory $factory)/*: void*/
{
$this->factory = $factory;
}
@@ -43,8 +39,9 @@ abstract class Comparator
/**
* Returns whether the comparator can compare two values.
*
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
*
* @return bool
*/
abstract public function accepts($expected, $actual);

View File

@@ -1,42 +1,47 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the Comparator package.
* This file is part of sebastian/comparator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\Comparator;
use RuntimeException;
use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
/**
* Thrown when an assertion for string equality failed.
*/
class ComparisonFailure extends \RuntimeException
class ComparisonFailure extends RuntimeException
{
/**
* Expected value of the retrieval which does not match $actual.
*
* @var mixed
*/
protected $expected;
/**
* Actually retrieved value which does not match $expected.
*
* @var mixed
*/
protected $actual;
/**
* The string representation of the expected value
* The string representation of the expected value.
*
* @var string
*/
protected $expectedAsString;
/**
* The string representation of the actual value
* The string representation of the actual value.
*
* @var string
*/
protected $actualAsString;
@@ -49,6 +54,7 @@ class ComparisonFailure extends \RuntimeException
/**
* Optional message which is placed in front of the first line
* returned by toString().
*
* @var string
*/
protected $message;
@@ -56,13 +62,13 @@ class ComparisonFailure extends \RuntimeException
/**
* Initialises with the expected value and the actual value.
*
* @param mixed $expected Expected value retrieved.
* @param mixed $actual Actual value retrieved.
* @param mixed $expected expected value retrieved
* @param mixed $actual actual value retrieved
* @param string $expectedAsString
* @param string $actualAsString
* @param bool $identical
* @param string $message A string which is prefixed on all returned lines
* in the difference output.
* @param string $message a string which is prefixed on all returned lines
* in the difference output
*/
public function __construct($expected, $actual, $expectedAsString, $actualAsString, $identical = false, $message = '')
{
@@ -73,17 +79,11 @@ class ComparisonFailure extends \RuntimeException
$this->message = $message;
}
/**
* @return mixed
*/
public function getActual()
{
return $this->actual;
}
/**
* @return mixed
*/
public function getExpected()
{
return $this->expected;
@@ -114,7 +114,7 @@ class ComparisonFailure extends \RuntimeException
return '';
}
$differ = new Differ("\n--- Expected\n+++ Actual\n");
$differ = new Differ(new UnifiedDiffOutputBuilder("\n--- Expected\n+++ Actual\n"));
return $differ->diff($this->expectedAsString, $this->actualAsString);
}

View File

@@ -1,17 +1,19 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the Comparator package.
* This file is part of sebastian/comparator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\Comparator;
use function sprintf;
use function strtolower;
use DOMDocument;
use DOMNode;
use ValueError;
/**
* Compares DOMNode instances for equality.
@@ -21,8 +23,9 @@ class DOMNodeComparator extends ObjectComparator
/**
* Returns whether the comparator can compare two values.
*
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
*
* @return bool
*/
public function accepts($expected, $actual)
@@ -42,17 +45,13 @@ class DOMNodeComparator extends ObjectComparator
*
* @throws ComparisonFailure
*/
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = array())
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = [])/*: void*/
{
$expectedAsString = $this->nodeToText($expected, true, $ignoreCase);
$actualAsString = $this->nodeToText($actual, true, $ignoreCase);
if ($expectedAsString !== $actualAsString) {
if ($expected instanceof DOMDocument) {
$type = 'documents';
} else {
$type = 'nodes';
}
$type = $expected instanceof DOMDocument ? 'documents' : 'nodes';
throw new ComparisonFailure(
$expected,
@@ -68,40 +67,27 @@ class DOMNodeComparator extends ObjectComparator
/**
* Returns the normalized, whitespace-cleaned, and indented textual
* representation of a DOMNode.
*
* @param DOMNode $node
* @param bool $canonicalize
* @param bool $ignoreCase
* @return string
*/
private function nodeToText(DOMNode $node, $canonicalize, $ignoreCase)
private function nodeToText(DOMNode $node, bool $canonicalize, bool $ignoreCase): string
{
if ($canonicalize) {
$document = new DOMDocument;
$document->loadXML($node->C14N());
try {
@$document->loadXML($node->C14N());
} catch (ValueError $e) {
}
$node = $document;
}
if ($node instanceof DOMDocument) {
$document = $node;
} else {
$document = $node->ownerDocument;
}
$document = $node instanceof DOMDocument ? $node : $node->ownerDocument;
$document->formatOutput = true;
$document->normalizeDocument();
if ($node instanceof DOMDocument) {
$text = $node->saveXML();
} else {
$text = $document->saveXML($node);
}
$text = $node instanceof DOMDocument ? $node->saveXML() : $document->saveXML($node);
if ($ignoreCase) {
$text = strtolower($text);
}
return $text;
return $ignoreCase ? strtolower($text) : $text;
}
}

View File

@@ -1,15 +1,23 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the Comparator package.
* This file is part of sebastian/comparator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\Comparator;
use function abs;
use function floor;
use function sprintf;
use DateInterval;
use DateTime;
use DateTimeInterface;
use DateTimeZone;
use Exception;
/**
* Compares DateTimeInterface instances for equality.
*/
@@ -18,14 +26,15 @@ class DateTimeComparator extends ObjectComparator
/**
* Returns whether the comparator can compare two values.
*
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
*
* @return bool
*/
public function accepts($expected, $actual)
{
return ($expected instanceof \DateTime || $expected instanceof \DateTimeInterface) &&
($actual instanceof \DateTime || $actual instanceof \DateTimeInterface);
return ($expected instanceof DateTime || $expected instanceof DateTimeInterface) &&
($actual instanceof DateTime || $actual instanceof DateTimeInterface);
}
/**
@@ -38,17 +47,29 @@ class DateTimeComparator extends ObjectComparator
* @param bool $ignoreCase Case is ignored when set to true
* @param array $processed List of already processed elements (used to prevent infinite recursion)
*
* @throws Exception
* @throws ComparisonFailure
*/
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = array())
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = [])/*: void*/
{
$delta = new \DateInterval(sprintf('PT%sS', abs($delta)));
/** @var DateTimeInterface $expected */
/** @var DateTimeInterface $actual */
$absDelta = abs($delta);
$delta = new DateInterval(sprintf('PT%dS', $absDelta));
$delta->f = $absDelta - floor($absDelta);
$expectedLower = clone $expected;
$expectedUpper = clone $expected;
$actualClone = (clone $actual)
->setTimezone(new DateTimeZone('UTC'));
if ($actual < $expectedLower->sub($delta) ||
$actual > $expectedUpper->add($delta)) {
$expectedLower = (clone $expected)
->setTimezone(new DateTimeZone('UTC'))
->sub($delta);
$expectedUpper = (clone $expected)
->setTimezone(new DateTimeZone('UTC'))
->add($delta);
if ($actualClone < $expectedLower || $actualClone > $expectedUpper) {
throw new ComparisonFailure(
$expected,
$actual,
@@ -64,14 +85,11 @@ class DateTimeComparator extends ObjectComparator
* Returns an ISO 8601 formatted string representation of a datetime or
* 'Invalid DateTimeInterface object' if the provided DateTimeInterface was not properly
* initialized.
*
* @param \DateTimeInterface $datetime
* @return string
*/
private function dateTimeToString($datetime)
private function dateTimeToString(DateTimeInterface $datetime): string
{
$string = $datetime->format('Y-m-d\TH:i:s.uO');
return $string ? $string : 'Invalid DateTimeInterface object';
return $string ?: 'Invalid DateTimeInterface object';
}
}

View File

@@ -1,17 +1,21 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the Comparator package.
* This file is part of sebastian/comparator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\Comparator;
use function is_float;
use function is_numeric;
/**
* Compares doubles for equality.
*
* @deprecated since v3.0.5 and v4.0.8
*/
class DoubleComparator extends NumericComparator
{
@@ -20,18 +24,19 @@ class DoubleComparator extends NumericComparator
*
* @var float
*/
const EPSILON = 0.0000000001;
public const EPSILON = 0.0000000001;
/**
* Returns whether the comparator can compare two values.
*
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
*
* @return bool
*/
public function accepts($expected, $actual)
{
return (is_double($expected) || is_double($actual)) && is_numeric($expected) && is_numeric($actual);
return (is_float($expected) || is_float($actual)) && is_numeric($expected) && is_numeric($actual);
}
/**
@@ -45,7 +50,7 @@ class DoubleComparator extends NumericComparator
*
* @throws ComparisonFailure
*/
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)/*: void*/
{
if ($delta == 0) {
$delta = self::EPSILON;

View File

@@ -1,15 +1,16 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the Comparator package.
* This file is part of sebastian/comparator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\Comparator;
use Exception;
/**
* Compares Exception instances for equality.
*/
@@ -18,20 +19,22 @@ class ExceptionComparator extends ObjectComparator
/**
* Returns whether the comparator can compare two values.
*
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
*
* @return bool
*/
public function accepts($expected, $actual)
{
return $expected instanceof \Exception && $actual instanceof \Exception;
return $expected instanceof Exception && $actual instanceof Exception;
}
/**
* Converts an object to an array containing all of its private, protected
* and public properties.
*
* @param object $object
* @param object $object
*
* @return array
*/
protected function toArray($object)

View File

@@ -1,48 +1,35 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the Comparator package.
* This file is part of sebastian/comparator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\Comparator;
use function array_unshift;
/**
* Factory for comparators which compare values for equality.
*/
class Factory
{
/**
* @var Comparator[]
*/
private $comparators = array();
/**
* @var Factory
*/
private static $instance;
/**
* Constructs a new factory.
* @var Comparator[]
*/
public function __construct()
{
$this->register(new TypeComparator);
$this->register(new ScalarComparator);
$this->register(new NumericComparator);
$this->register(new DoubleComparator);
$this->register(new ArrayComparator);
$this->register(new ResourceComparator);
$this->register(new ObjectComparator);
$this->register(new ExceptionComparator);
$this->register(new SplObjectStorageComparator);
$this->register(new DOMNodeComparator);
$this->register(new MockObjectComparator);
$this->register(new DateTimeComparator);
}
private $customComparators = [];
/**
* @var Comparator[]
*/
private $defaultComparators = [];
/**
* @return Factory
@@ -50,41 +37,58 @@ class Factory
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new self;
self::$instance = new self; // @codeCoverageIgnore
}
return self::$instance;
}
/**
* Constructs a new factory.
*/
public function __construct()
{
$this->registerDefaultComparators();
}
/**
* Returns the correct comparator for comparing two values.
*
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
*
* @return Comparator
*/
public function getComparatorFor($expected, $actual)
{
foreach ($this->comparators as $comparator) {
foreach ($this->customComparators as $comparator) {
if ($comparator->accepts($expected, $actual)) {
return $comparator;
}
}
foreach ($this->defaultComparators as $comparator) {
if ($comparator->accepts($expected, $actual)) {
return $comparator;
}
}
throw new RuntimeException('No suitable Comparator implementation found');
}
/**
* Registers a new comparator.
*
* This comparator will be returned by getInstance() if its accept() method
* This comparator will be returned by getComparatorFor() if its accept() method
* returns TRUE for the compared values. It has higher priority than the
* existing comparators, meaning that its accept() method will be tested
* existing comparators, meaning that its accept() method will be invoked
* before those of the other comparators.
*
* @param Comparator $comparator The registered comparator
* @param Comparator $comparator The comparator to be registered
*/
public function register(Comparator $comparator)
public function register(Comparator $comparator)/*: void*/
{
array_unshift($this->comparators, $comparator);
array_unshift($this->customComparators, $comparator);
$comparator->setFactory($this);
}
@@ -92,16 +96,46 @@ class Factory
/**
* Unregisters a comparator.
*
* This comparator will no longer be returned by getInstance().
* This comparator will no longer be considered by getComparatorFor().
*
* @param Comparator $comparator The unregistered comparator
* @param Comparator $comparator The comparator to be unregistered
*/
public function unregister(Comparator $comparator)
public function unregister(Comparator $comparator)/*: void*/
{
foreach ($this->comparators as $key => $_comparator) {
foreach ($this->customComparators as $key => $_comparator) {
if ($comparator === $_comparator) {
unset($this->comparators[$key]);
unset($this->customComparators[$key]);
}
}
}
/**
* Unregisters all non-default comparators.
*/
public function reset()/*: void*/
{
$this->customComparators = [];
}
private function registerDefaultComparators(): void
{
$this->registerDefaultComparator(new MockObjectComparator);
$this->registerDefaultComparator(new DateTimeComparator);
$this->registerDefaultComparator(new DOMNodeComparator);
$this->registerDefaultComparator(new SplObjectStorageComparator);
$this->registerDefaultComparator(new ExceptionComparator);
$this->registerDefaultComparator(new ObjectComparator);
$this->registerDefaultComparator(new ResourceComparator);
$this->registerDefaultComparator(new ArrayComparator);
$this->registerDefaultComparator(new NumericComparator);
$this->registerDefaultComparator(new ScalarComparator);
$this->registerDefaultComparator(new TypeComparator);
}
private function registerDefaultComparator(Comparator $comparator): void
{
$this->defaultComparators[] = $comparator;
$comparator->setFactory($this);
}
}

View File

@@ -1,37 +1,40 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the Comparator package.
* This file is part of sebastian/comparator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\Comparator;
use PHPUnit\Framework\MockObject\MockObject;
/**
* Compares PHPUnit_Framework_MockObject_MockObject instances for equality.
* Compares PHPUnit\Framework\MockObject\MockObject instances for equality.
*/
class MockObjectComparator extends ObjectComparator
{
/**
* Returns whether the comparator can compare two values.
*
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
*
* @return bool
*/
public function accepts($expected, $actual)
{
return $expected instanceof \PHPUnit_Framework_MockObject_MockObject && $actual instanceof \PHPUnit_Framework_MockObject_MockObject;
return $expected instanceof MockObject && $actual instanceof MockObject;
}
/**
* Converts an object to an array containing all of its private, protected
* and public properties.
*
* @param object $object
* @param object $object
*
* @return array
*/
protected function toArray($object)
@@ -42,4 +45,4 @@ class MockObjectComparator extends ObjectComparator
return $array;
}
}
}

View File

@@ -1,15 +1,22 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the Comparator package.
* This file is part of sebastian/comparator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\Comparator;
use function abs;
use function is_float;
use function is_infinite;
use function is_nan;
use function is_numeric;
use function is_string;
use function sprintf;
/**
* Compares numerical values for equality.
*/
@@ -18,16 +25,15 @@ class NumericComparator extends ScalarComparator
/**
* Returns whether the comparator can compare two values.
*
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
*
* @return bool
*/
public function accepts($expected, $actual)
{
// all numerical values, but not if one of them is a double
// or both of them are strings
// all numerical values, but not if both of them are strings
return is_numeric($expected) && is_numeric($actual) &&
!(is_double($expected) || is_double($actual)) &&
!(is_string($expected) && is_string($actual));
}
@@ -42,14 +48,14 @@ class NumericComparator extends ScalarComparator
*
* @throws ComparisonFailure
*/
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)/*: void*/
{
if (is_infinite($actual) && is_infinite($expected)) {
if ($this->isInfinite($actual) && $this->isInfinite($expected)) {
return;
}
if ((is_infinite($actual) xor is_infinite($expected)) ||
(is_nan($actual) or is_nan($expected)) ||
if (($this->isInfinite($actual) xor $this->isInfinite($expected)) ||
($this->isNan($actual) || $this->isNan($expected)) ||
abs($actual - $expected) > $delta) {
throw new ComparisonFailure(
$expected,
@@ -65,4 +71,14 @@ class NumericComparator extends ScalarComparator
);
}
}
private function isInfinite($value): bool
{
return is_float($value) && is_infinite($value);
}
private function isNan($value): bool
{
return is_float($value) && is_nan($value);
}
}

View File

@@ -1,15 +1,20 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the Comparator package.
* This file is part of sebastian/comparator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\Comparator;
use function get_class;
use function in_array;
use function is_object;
use function sprintf;
use function substr_replace;
/**
* Compares objects for equality.
*/
@@ -18,8 +23,9 @@ class ObjectComparator extends ArrayComparator
/**
* Returns whether the comparator can compare two values.
*
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
*
* @return bool
*/
public function accepts($expected, $actual)
@@ -39,7 +45,7 @@ class ObjectComparator extends ArrayComparator
*
* @throws ComparisonFailure
*/
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = array())
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false, array &$processed = [])/*: void*/
{
if (get_class($actual) !== get_class($expected)) {
throw new ComparisonFailure(
@@ -57,12 +63,12 @@ class ObjectComparator extends ArrayComparator
}
// don't compare twice to allow for cyclic dependencies
if (in_array(array($actual, $expected), $processed, true) ||
in_array(array($expected, $actual), $processed, true)) {
if (in_array([$actual, $expected], $processed, true) ||
in_array([$expected, $actual], $processed, true)) {
return;
}
$processed[] = array($actual, $expected);
$processed[] = [$actual, $expected];
// don't compare objects if they are identical
// this helps to avoid the error "maximum function nesting level reached"
@@ -95,7 +101,8 @@ class ObjectComparator extends ArrayComparator
* Converts an object to an array containing all of its private, protected
* and public properties.
*
* @param object $object
* @param object $object
*
* @return array
*/
protected function toArray($object)

View File

@@ -1,15 +1,16 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the Comparator package.
* This file is part of sebastian/comparator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\Comparator;
use function is_resource;
/**
* Compares resources for equality.
*/
@@ -18,8 +19,9 @@ class ResourceComparator extends Comparator
/**
* Returns whether the comparator can compare two values.
*
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
*
* @return bool
*/
public function accepts($expected, $actual)
@@ -38,7 +40,7 @@ class ResourceComparator extends Comparator
*
* @throws ComparisonFailure
*/
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)/*: void*/
{
if ($actual != $expected) {
throw new ComparisonFailure(

View File

@@ -1,15 +1,22 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the Comparator package.
* This file is part of sebastian/comparator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\Comparator;
use function is_bool;
use function is_object;
use function is_scalar;
use function is_string;
use function method_exists;
use function sprintf;
use function strtolower;
/**
* Compares scalar or NULL values for equality.
*/
@@ -18,9 +25,11 @@ class ScalarComparator extends Comparator
/**
* Returns whether the comparator can compare two values.
*
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
*
* @return bool
*
* @since Method available since Release 3.6.0
*/
public function accepts($expected, $actual)
@@ -43,14 +52,14 @@ class ScalarComparator extends Comparator
*
* @throws ComparisonFailure
*/
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)/*: void*/
{
$expectedToCompare = $expected;
$actualToCompare = $actual;
// always compare as strings to avoid strange behaviour
// otherwise 0 == 'Foobar'
if (is_string($expected) || is_string($actual)) {
if ((is_string($expected) && !is_bool($actual)) || (is_string($actual) && !is_bool($expected))) {
$expectedToCompare = (string) $expectedToCompare;
$actualToCompare = (string) $actualToCompare;
@@ -60,18 +69,18 @@ class ScalarComparator extends Comparator
}
}
if ($expectedToCompare != $actualToCompare) {
if (is_string($expected) && is_string($actual)) {
throw new ComparisonFailure(
$expected,
$actual,
$this->exporter->export($expected),
$this->exporter->export($actual),
false,
'Failed asserting that two strings are equal.'
);
}
if ($expectedToCompare !== $actualToCompare && is_string($expected) && is_string($actual)) {
throw new ComparisonFailure(
$expected,
$actual,
$this->exporter->export($expected),
$this->exporter->export($actual),
false,
'Failed asserting that two strings are equal.'
);
}
if ($expectedToCompare != $actualToCompare) {
throw new ComparisonFailure(
$expected,
$actual,

View File

@@ -1,15 +1,16 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the Comparator package.
* This file is part of sebastian/comparator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\Comparator;
use SplObjectStorage;
/**
* Compares \SplObjectStorage instances for equality.
*/
@@ -18,13 +19,14 @@ class SplObjectStorageComparator extends Comparator
/**
* Returns whether the comparator can compare two values.
*
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
*
* @return bool
*/
public function accepts($expected, $actual)
{
return $expected instanceof \SplObjectStorage && $actual instanceof \SplObjectStorage;
return $expected instanceof SplObjectStorage && $actual instanceof SplObjectStorage;
}
/**
@@ -38,7 +40,7 @@ class SplObjectStorageComparator extends Comparator
*
* @throws ComparisonFailure
*/
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)/*: void*/
{
foreach ($actual as $object) {
if (!$expected->contains($object)) {

View File

@@ -1,15 +1,17 @@
<?php
<?php declare(strict_types=1);
/*
* This file is part of the Comparator package.
* This file is part of sebastian/comparator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\Comparator;
use function gettype;
use function sprintf;
/**
* Compares values for type equality.
*/
@@ -18,8 +20,9 @@ class TypeComparator extends Comparator
/**
* Returns whether the comparator can compare two values.
*
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
* @param mixed $expected The first value to compare
* @param mixed $actual The second value to compare
*
* @return bool
*/
public function accepts($expected, $actual)
@@ -38,7 +41,7 @@ class TypeComparator extends Comparator
*
* @throws ComparisonFailure
*/
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false)/*: void*/
{
if (gettype($expected) != gettype($actual)) {
throw new ComparisonFailure(

View File

@@ -0,0 +1,16 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/comparator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\Comparator;
use Throwable;
interface Exception extends Throwable
{
}

View File

@@ -0,0 +1,14 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/comparator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\Comparator;
final class RuntimeException extends \RuntimeException implements Exception
{
}