From 90e9e0379584bdf34220322e202617cd56d8ba65 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Thu, 11 Oct 2018 11:10:07 +0200 Subject: [PATCH] Closes #3340 --- ChangeLog-7.5.md | 4 + src/Framework/Assert.php | 114 ++++++++++++++++++++++++++++ tests/unit/Framework/AssertTest.php | 35 +++++++++ 3 files changed, 153 insertions(+) diff --git a/ChangeLog-7.5.md b/ChangeLog-7.5.md index 768ae0f844b..ec912d93e7c 100644 --- a/ChangeLog-7.5.md +++ b/ChangeLog-7.5.md @@ -4,5 +4,9 @@ All notable changes of the PHPUnit 7.5 release series are documented in this fil ## [7.5.0] - 2018-12-07 +### Added + +* Implemented [#3340](https://github.com/sebastianbergmann/phpunit/issues/3340): Added `assertEqualsCanonicalizing()`, `assertEqualsIgnoringCase()`, `assertEqualsWithDelta()`, `assertNotEqualsCanonicalizing()`, `assertNotEqualsIgnoringCase()`, and `assertNotEqualsWithDelta()` as alternatives to using `assertEquals()` and `assertNotEquals()` with the `$delta`, `$canonicalize`, or `$ignoreCase` parameters + [7.5.0]: https://github.com/sebastianbergmann/phpunit/compare/7.4...7.5.0 diff --git a/src/Framework/Assert.php b/src/Framework/Assert.php index 0f0cfa155fa..39e946b9096 100644 --- a/src/Framework/Assert.php +++ b/src/Framework/Assert.php @@ -486,6 +486,60 @@ public static function assertEquals($expected, $actual, string $message = '', fl static::assertThat($actual, $constraint, $message); } + /** + * Asserts that two variables are equal (canonicalizing). + * + * @throws ExpectationFailedException + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException + */ + public static function assertEqualsCanonicalizing($expected, $actual, string $message = ''): void + { + $constraint = new IsEqual( + $expected, + 0.0, + 10, + true, + false + ); + + static::assertThat($actual, $constraint, $message); + } + + /** + * Asserts that two variables are equal (ignoring case). + * + * @throws ExpectationFailedException + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException + */ + public static function assertEqualsIgnoringCase($expected, $actual, string $message = ''): void + { + $constraint = new IsEqual( + $expected, + 0.0, + 10, + false, + true + ); + + static::assertThat($actual, $constraint, $message); + } + + /** + * Asserts that two variables are equal (with delta). + * + * @throws ExpectationFailedException + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException + */ + public static function assertEqualsWithDelta($expected, $actual, float $delta, string $message = ''): void + { + $constraint = new IsEqual( + $expected, + $delta + ); + + static::assertThat($actual, $constraint, $message); + } + /** * Asserts that a variable is equal to an attribute of an object. * @@ -533,6 +587,66 @@ public static function assertNotEquals($expected, $actual, string $message = '', static::assertThat($actual, $constraint, $message); } + /** + * Asserts that two variables are not equal (canonicalizing). + * + * @throws ExpectationFailedException + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException + */ + public static function assertNotEqualsCanonicalizing($expected, $actual, string $message = ''): void + { + $constraint = new LogicalNot( + new IsEqual( + $expected, + 0.0, + 10, + true, + false + ) + ); + + static::assertThat($actual, $constraint, $message); + } + + /** + * Asserts that two variables are not equal (ignoring case). + * + * @throws ExpectationFailedException + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException + */ + public static function assertNotEqualsIgnoringCase($expected, $actual, string $message = ''): void + { + $constraint = new LogicalNot( + new IsEqual( + $expected, + 0.0, + 10, + false, + true + ) + ); + + static::assertThat($actual, $constraint, $message); + } + + /** + * Asserts that two variables are not equal (with delta). + * + * @throws ExpectationFailedException + * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException + */ + public static function assertNotEqualsWithDelta($expected, $actual, float $delta, string $message = ''): void + { + $constraint = new LogicalNot( + new IsEqual( + $expected, + $delta + ) + ); + + static::assertThat($actual, $constraint, $message); + } + /** * Asserts that a variable is not equal to an attribute of an object. * diff --git a/tests/unit/Framework/AssertTest.php b/tests/unit/Framework/AssertTest.php index 73d3da2eb4e..94c0daaaf84 100644 --- a/tests/unit/Framework/AssertTest.php +++ b/tests/unit/Framework/AssertTest.php @@ -2506,6 +2506,41 @@ public function testAssertStringNotMatchesFormatFile(): void $this->assertStringNotMatchesFormatFile(TEST_FILES_PATH . 'expectedFileFormat.txt', "FOO\n"); } + public function testStringsCanBeComparedForEqualityIgnoringCase(): void + { + $this->assertEqualsIgnoringCase('a', 'A'); + + $this->assertNotEqualsIgnoringCase('a', 'B'); + } + + public function testArraysOfStringsCanBeComparedForEqualityIgnoringCase(): void + { + $this->assertEqualsIgnoringCase(['a'], ['A']); + + $this->assertNotEqualsIgnoringCase(['a'], ['B']); + } + + public function testStringsCanBeComparedForEqualityWithDelta(): void + { + $this->assertEqualsWithDelta(2.3, 2.5, 0.5); + + $this->assertNotEqualsWithDelta(2.3, 3.5, 0.5); + } + + public function testArraysOfStringsCanBeComparedForEqualityWithDelta(): void + { + $this->assertEqualsWithDelta([2.3], [2.5], 0.5); + + $this->assertNotEqualsWithDelta([2.3], [3.5], 0.5); + } + + public function testArraysCanBeComparedForEqualityWithCanonicalization(): void + { + $this->assertEqualsCanonicalizing([3, 2, 1], [2, 3, 1]); + + $this->assertNotEqualsCanonicalizing([3, 2, 1], [2, 3, 4]); + } + protected function sameValues(): array { $object = new \SampleClass(4, 8, 15);