diff --git a/README.md b/README.md index 5a8c40e..5d870f8 100644 --- a/README.md +++ b/README.md @@ -359,6 +359,16 @@ These methods were introduced in PHPUnit 8.5.0 as alternatives to using `Assert: COMMENT: No documentation available (yet) for these assertions on the PHPUnit site. --> +#### PHPUnit < 9.0.0: `Yoast\PHPUnitPolyfills\Polyfills\EqualToSpecializations` + +Polyfills the following methods: +| | | +|-----------------------------------|---------------------------------| +| `Assert::equalToCanonicalizing()` | `Assert::equalToIgnoringCase()` | +| `Assert::equalToWithDelta()` | | + +These methods, which are typically used to verify parameters passed to Mock Objects, were introduced in PHPUnit 9.0.0 as alternatives to using `Assert::EqualTo()` with these optional parameters. Support for passing the respective optional parameters to `Assert::EqualTo()` was removed in PHPUnit 9.0.0. + #### PHPUnit < 9.1.0: `Yoast\PHPUnitPolyfills\Polyfills\AssertionRenames` Polyfills the following renamed methods: diff --git a/phpunitpolyfills-autoload.php b/phpunitpolyfills-autoload.php index 2dd8213..851188c 100644 --- a/phpunitpolyfills-autoload.php +++ b/phpunitpolyfills-autoload.php @@ -65,6 +65,10 @@ public static function load( $className ) { self::loadAssertFileEqualsSpecializations(); return true; + case 'Yoast\PHPUnitPolyfills\Polyfills\EqualToSpecializations': + self::loadEqualToSpecializations(); + return true; + case 'Yoast\PHPUnitPolyfills\Polyfills\AssertionRenames': self::loadAssertionRenames(); return true; @@ -324,6 +328,23 @@ public static function loadAssertFileEqualsSpecializations() { require_once __DIR__ . '/src/Polyfills/AssertFileEqualsSpecializations_Empty.php'; } + /** + * Load the EqualToSpecializations polyfill or an empty trait with the same name + * if a PHPUnit version is used which already contains this functionality. + * + * @return void + */ + public static function loadEqualToSpecializations() { + if ( \method_exists( '\PHPUnit\Framework\Assert', 'equalToWithDelta' ) === false ) { + // PHPUnit < 9.0.0. + require_once __DIR__ . '/src/Polyfills/EqualToSpecializations.php'; + return; + } + + // PHPUnit >= 9.0.0. + require_once __DIR__ . '/src/Polyfills/EqualToSpecializations_Empty.php'; + } + /** * Load the AssertionRenames polyfill or an empty trait with the same name * if a PHPUnit version is used which already contains this functionality. diff --git a/src/Polyfills/EqualToSpecializations.php b/src/Polyfills/EqualToSpecializations.php new file mode 100644 index 0000000..2bce1ff --- /dev/null +++ b/src/Polyfills/EqualToSpecializations.php @@ -0,0 +1,53 @@ += 9.0.0 in which this polyfill is not needed. + */ +trait EqualToSpecializations {} diff --git a/src/TestCases/TestCasePHPUnitGte8.php b/src/TestCases/TestCasePHPUnitGte8.php index cd5b120..f819be9 100644 --- a/src/TestCases/TestCasePHPUnitGte8.php +++ b/src/TestCases/TestCasePHPUnitGte8.php @@ -7,6 +7,7 @@ use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource; use Yoast\PHPUnitPolyfills\Polyfills\AssertFileEqualsSpecializations; use Yoast\PHPUnitPolyfills\Polyfills\AssertionRenames; +use Yoast\PHPUnitPolyfills\Polyfills\EqualToSpecializations; use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches; use Yoast\PHPUnitPolyfills\Polyfills\ExpectPHPException; @@ -25,6 +26,7 @@ abstract class TestCase extends PHPUnit_TestCase { use AssertClosedResource; use AssertFileEqualsSpecializations; use AssertionRenames; + use EqualToSpecializations; use ExpectExceptionMessageMatches; use ExpectPHPException; diff --git a/src/TestCases/TestCasePHPUnitLte7.php b/src/TestCases/TestCasePHPUnitLte7.php index 6dc9596..e906972 100644 --- a/src/TestCases/TestCasePHPUnitLte7.php +++ b/src/TestCases/TestCasePHPUnitLte7.php @@ -12,6 +12,7 @@ use Yoast\PHPUnitPolyfills\Polyfills\AssertIsType; use Yoast\PHPUnitPolyfills\Polyfills\AssertNumericType; use Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains; +use Yoast\PHPUnitPolyfills\Polyfills\EqualToSpecializations; use Yoast\PHPUnitPolyfills\Polyfills\ExpectException; use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches; use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionObject; @@ -37,6 +38,7 @@ abstract class TestCase extends PHPUnit_TestCase { use AssertIsType; use AssertNumericType; use AssertStringContains; + use EqualToSpecializations; use ExpectException; use ExpectExceptionMessageMatches; use ExpectExceptionObject; diff --git a/src/TestCases/XTestCase.php b/src/TestCases/XTestCase.php index a5ff6ac..3d4dbd9 100644 --- a/src/TestCases/XTestCase.php +++ b/src/TestCases/XTestCase.php @@ -12,6 +12,7 @@ use Yoast\PHPUnitPolyfills\Polyfills\AssertIsType; use Yoast\PHPUnitPolyfills\Polyfills\AssertNumericType; use Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains; +use Yoast\PHPUnitPolyfills\Polyfills\EqualToSpecializations; use Yoast\PHPUnitPolyfills\Polyfills\ExpectException; use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches; use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionObject; @@ -39,6 +40,7 @@ abstract class XTestCase extends PHPUnit_TestCase { use AssertIsType; use AssertNumericType; use AssertStringContains; + use EqualToSpecializations; use ExpectException; use ExpectExceptionMessageMatches; use ExpectExceptionObject; diff --git a/tests/Polyfills/EqualToSpecializationsTest.php b/tests/Polyfills/EqualToSpecializationsTest.php new file mode 100644 index 0000000..97871c7 --- /dev/null +++ b/tests/Polyfills/EqualToSpecializationsTest.php @@ -0,0 +1,70 @@ +assertThat( 2.5, $this->equalToWithDelta( 2.3, 0.5 ) ); + } + + /** + * Verify availability of the equalToCanonicalizing() method. + * + * @return void + */ + public function testEqualToCanonicalizing() { + self::assertThat( [ 2, 3, 1 ], static::equalToCanonicalizing( [ 3, 2, 1 ] ) ); + } + + /** + * Verify availability of the equalIgnoringCase() method. + * + * @return void + */ + public function testEqualToIgnoringCase() { + self::assertThat( 'A', self::equalToIgnoringCase( 'a' ) ); + } + + /** + * Verify ability of the equalToWithDelta() method to correctly fail a comparison. + * + * @return void + */ + public function testEqualToWithDeltaNegative() { + self::assertThat( 3.5, $this->logicalNot( $this->equalToWithDelta( 2.3, 0.5 ) ) ); + } + + /** + * Verify ability of the equalToCanonicalizing() method to correctly fail a comparison. + * + * @return void + */ + public function testEqualToCanonicalizingNegative() { + static::assertThat( [ 2, 3, 1 ], $this->logicalNot( static::equalToCanonicalizing( [ 4, 2, 1 ] ) ) ); + } + + /** + * Verify ability of the equalToIgnoringCaseNegative() method to correctly fail a comparison. + * + * @return void + */ + public function testEqualToIgnoringCaseNegative() { + self::assertThat( 'A', $this->logicalNot( $this->equalToIgnoringCase( 'b' ) ) ); + } +} diff --git a/tests/TestCases/TestCaseTestTrait.php b/tests/TestCases/TestCaseTestTrait.php index d7b4c84..54601f5 100644 --- a/tests/TestCases/TestCaseTestTrait.php +++ b/tests/TestCases/TestCaseTestTrait.php @@ -154,4 +154,13 @@ public function testAvailabilityAssertClosedResource() { $this->assertIsClosedResource( $resource ); } + + /** + * Verify availability of trait polyfilled PHPUnit methods [13]. + * + * @return void + */ + public function testAvailabilityEqualToSpecializations() { + self::assertThat( [ 2, 3, 1 ], $this->equalToCanonicalizing( [ 3, 2, 1 ] ) ); + } }