Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polyfill for equalTo constraint specializations #28

Merged
merged 7 commits into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 21 additions & 0 deletions phpunitpolyfills-autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
53 changes: 53 additions & 0 deletions src/Polyfills/EqualToSpecializations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Yoast\PHPUnitPolyfills\Polyfills;

use PHPUnit\Framework\Constraint\IsEqual;

/**
* Polyfill the Assert::equalToCanonicalizing(), Assert::equalToIgnoringCase() and
* Assert::equalToWithDelta(), which replace the use of Assert::equalTo()
* with these optional parameters.
*
* Introduced in PHPUnit 9.0.0.
* Use of Assert::equalTo() with these respective optional parameters was
* never deprecated but leads to unexpected behaviour as they are ignored in PHPUnit 9.0.0.
*
* @link https://github.com/sebastianbergmann/phpunit/commit/43c01a4e0c74a4bf019a8d879bced5146af2fbb6
*/
trait EqualToSpecializations {

/**
* Creates "is equal" constraint (canonicalizing).
*
* @param mixed $value Expected value for constraint.
*
* @return IsEqual|PHPUnit_Framework_Constraint_IsEqual An isEqual constraint instance.
*/
public static function equalToCanonicalizing( $value ) {
return static::equalTo( $value, 0.0, 10, true, false );
}

/**
* Creates "is equal" constraint (ignoring case).
*
* @param mixed $value Expected value for constraint.
*
* @return IsEqual|PHPUnit_Framework_Constraint_IsEqual An isEqual constraint instance.
*/
public static function equalToIgnoringCase( $value ) {
return static::equalTo( $value, 0.0, 10, false, true );
}

/**
* Creates "is equal" constraint (with delta).
*
* @param mixed $value Expected value for constraint.
* @param float $delta The delta to allow between the expected and the actual value.
*
* @return IsEqual|PHPUnit_Framework_Constraint_IsEqual An isEqual constraint instance.
*/
public static function equalToWithDelta( $value, $delta ) {
return static::equalTo( $value, $delta, 10, false, false );
}
}
8 changes: 8 additions & 0 deletions src/Polyfills/EqualToSpecializations_Empty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Yoast\PHPUnitPolyfills\Polyfills;

/**
* Empty trait for use with PHPUnit >= 9.0.0 in which this polyfill is not needed.
*/
trait EqualToSpecializations {}
2 changes: 2 additions & 0 deletions src/TestCases/TestCasePHPUnitGte8.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,6 +26,7 @@ abstract class TestCase extends PHPUnit_TestCase {
use AssertClosedResource;
use AssertFileEqualsSpecializations;
use AssertionRenames;
use EqualToSpecializations;
use ExpectExceptionMessageMatches;
use ExpectPHPException;

Expand Down
2 changes: 2 additions & 0 deletions src/TestCases/TestCasePHPUnitLte7.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,6 +38,7 @@ abstract class TestCase extends PHPUnit_TestCase {
use AssertIsType;
use AssertNumericType;
use AssertStringContains;
use EqualToSpecializations;
use ExpectException;
use ExpectExceptionMessageMatches;
use ExpectExceptionObject;
Expand Down
2 changes: 2 additions & 0 deletions src/TestCases/XTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -39,6 +40,7 @@ abstract class XTestCase extends PHPUnit_TestCase {
use AssertIsType;
use AssertNumericType;
use AssertStringContains;
use EqualToSpecializations;
use ExpectException;
use ExpectExceptionMessageMatches;
use ExpectExceptionObject;
Expand Down
70 changes: 70 additions & 0 deletions tests/Polyfills/EqualToSpecializationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Yoast\PHPUnitPolyfills\Tests\Polyfills;

use PHPUnit\Framework\TestCase;
use Yoast\PHPUnitPolyfills\Polyfills\EqualToSpecializations;

/**
* Availability test for the functions polyfilled by the EqualToSpecializations trait.
*
* @covers \Yoast\PHPUnitPolyfills\Polyfills\EqualToSpecializations
*/
class EqualToSpecializationsTest extends TestCase {

use EqualToSpecializations;

/**
* Verify availability of the equalToWithDelta() method.
*
* @return void
*/
public function testEqualToWithDelta() {
$this->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' ) ) );
}
}
9 changes: 9 additions & 0 deletions tests/TestCases/TestCaseTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ] ) );
}
}