Skip to content

Commit

Permalink
Drop support for PHPUnit < 6.4 [5]
Browse files Browse the repository at this point in the history
Remove work-arounds for PHPUnit 5.x from the test suite.
  • Loading branch information
jrfnl committed Sep 6, 2024
1 parent 21bfd45 commit 1ea653a
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 176 deletions.
8 changes: 0 additions & 8 deletions phpunitpolyfills-autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use PHPUnit\Runner\Version as PHPUnit_Version;
use PHPUnit_Runner_Version;

if ( \class_exists( 'Yoast\PHPUnitPolyfills\Autoload', false ) === false ) {

Expand Down Expand Up @@ -350,20 +349,13 @@ public static function loadTestListenerDefaultImplementation() {
/**
* Retrieve the PHPUnit version id.
*
* As both the pre-PHPUnit 6 class, as well as the PHPUnit 6+ class contain the `id()` function,
* this should work independently of whether or not another library may have aliased the class.
*
* @return string Version number as a string.
*/
public static function getPHPUnitVersion() {
if ( \class_exists( '\PHPUnit\Runner\Version' ) ) {
return PHPUnit_Version::id();
}

if ( \class_exists( '\PHPUnit_Runner_Version' ) ) {
return PHPUnit_Runner_Version::id();
}

return '0';
}
}
Expand Down
22 changes: 3 additions & 19 deletions tests/Polyfills/AssertClosedResourceNotResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_AssertionFailedError;
use stdClass;
use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper;
use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource;
Expand Down Expand Up @@ -39,7 +38,7 @@ final class AssertClosedResourceNotResourceTest extends TestCase {
public function testAssertIsClosedResource( $value ) {
$pattern = '`^Failed asserting that .+? is of type ["]?resource \(closed\)["]?`s';

$this->expectException( $this->getAssertionFailedExceptionName() );
$this->expectException( AssertionFailedError::class );
$this->expectExceptionMessageMatches( $pattern );

$this->assertIsClosedResource( $value );
Expand All @@ -54,7 +53,7 @@ public function testAssertIsClosedResource( $value ) {
public function testAssertIsClosedResourceFailsWithCustomMessage() {
$pattern = '`^This assertion failed for reason XYZ\s+Failed asserting that .+? is of type ["]?resource \(closed\)["]?`s';

$this->expectException( $this->getAssertionFailedExceptionName() );
$this->expectException( AssertionFailedError::class );
$this->expectExceptionMessageMatches( $pattern );

$this->assertIsClosedResource( 'text string', 'This assertion failed for reason XYZ' );
Expand Down Expand Up @@ -84,7 +83,7 @@ public function testAssertIsNotClosedResource( $value ) {
public function testAssertIsNotClosedResourceFailsWithCustomMessage() {
$pattern = '`^This assertion failed for reason XYZ\s+Failed asserting that .+? not of type ["]?resource \(closed\)["]?`s';

$this->expectException( $this->getAssertionFailedExceptionName() );
$this->expectException( AssertionFailedError::class );
$this->expectExceptionMessageMatches( $pattern );

$resource = \opendir( __DIR__ . '/Fixtures/' );
Expand Down Expand Up @@ -125,19 +124,4 @@ public static function dataNotResource() {
'object' => [ new stdClass() ],
];
}

/**
* Helper function: retrieve the name of the "assertion failed" exception to expect (PHPUnit cross-version).
*
* @return string
*/
public function getAssertionFailedExceptionName() {
$exception = AssertionFailedError::class;
if ( \class_exists( PHPUnit_Framework_AssertionFailedError::class ) ) {
// PHPUnit < 6.
$exception = PHPUnit_Framework_AssertionFailedError::class;
}

return $exception;
}
}
21 changes: 3 additions & 18 deletions tests/Polyfills/AssertClosedResourceShmopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Yoast\PHPUnitPolyfills\Tests\Polyfills;

use PHPUnit\Framework\Attributes\Before;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\Attributes\RequiresPhpExtension;
use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper;
use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource;
Expand All @@ -24,34 +24,19 @@
* @covers \Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource
*
* @requires extension shmop
* @requires PHP < 8.0
*
* @phpcs:disable Generic.PHP.DeprecatedFunctions.Deprecated
* @phpcs:disable PHPCompatibility.FunctionUse.RemovedFunctions.shmop_closeDeprecated
*/
#[CoversClass( AssertClosedResource::class )]
#[CoversClass( ResourceHelper::class )]
#[RequiresPhp( '< 8.0' )]
#[RequiresPhpExtension( 'shmop' )]
final class AssertClosedResourceShmopTest extends AssertClosedResourceTestCase {

use AssertClosedResource;

/**
* Skip these tests on incompatible PHP versions.
*
* {@internal The PHPUnit `@requires` tag can only handle one `PHP` requirement,
* while we need two.}
*
* @before
*
* @return void
*/
#[Before]
protected function skipOnIncompatiblePHP() {
if ( \PHP_VERSION_ID < 70000 || \PHP_VERSION_ID >= 80000 ) {
$this->markTestSkipped( 'This test requires PHP 7.x.' );
}
}

/**
* Verify availability of the assertIsClosedResource() method.
*
Expand Down
20 changes: 2 additions & 18 deletions tests/Polyfills/AssertClosedResourceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_AssertionFailedError;
use Yoast\PHPUnitPolyfills\Polyfills\AssertClosedResource;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches;

Expand All @@ -26,7 +25,7 @@ abstract class AssertClosedResourceTestCase extends TestCase {
public function isClosedResourceExpectExceptionOnOpenResource( $actual ) {
$pattern = '`^Failed asserting that .+? is of type ["]?resource \(closed\)["]?`';

$this->expectException( $this->getAssertionFailedExceptionName() );
$this->expectException( AssertionFailedError::class );
$this->expectExceptionMessageMatches( $pattern );

$this->assertIsClosedResource( $actual );
Expand All @@ -46,24 +45,9 @@ public function isNotClosedResourceExpectExceptionOnClosedResource( $actual ) {
*/
$pattern = '`^Failed asserting that (resource \(closed\)|NULL) is not of type ["]?resource \(closed\)["]?`';

$this->expectException( $this->getAssertionFailedExceptionName() );
$this->expectException( AssertionFailedError::class );
$this->expectExceptionMessageMatches( $pattern );

self::assertIsNotClosedResource( $actual );
}

/**
* Helper function: retrieve the name of the "assertion failed" exception to expect (PHPUnit cross-version).
*
* @return string
*/
public function getAssertionFailedExceptionName() {
$exception = AssertionFailedError::class;
if ( \class_exists( PHPUnit_Framework_AssertionFailedError::class ) ) {
// PHPUnit < 6.
$exception = PHPUnit_Framework_AssertionFailedError::class;
}

return $exception;
}
}
22 changes: 3 additions & 19 deletions tests/Polyfills/AssertIgnoringLineEndingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PHPUnit\Framework\TestCase;
use PHPUnit\Runner\Version as PHPUnit_Version;
use PHPUnit\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar_Old;
use PHPUnit_Framework_AssertionFailedError;
use PHPUnitPHAR\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar;
use SebastianBergmann\Exporter\Exporter;
use stdClass;
Expand Down Expand Up @@ -156,7 +155,7 @@ public function testAssertStringEqualsStringIgnoringLineEndingsFails( $expected,
self::normalizeLineEndings( $expected )
);

$this->expectException( $this->getAssertionFailedExceptionName() );
$this->expectException( AssertionFailedError::class );
$this->expectExceptionMessage( $msg );

$this->assertStringEqualsStringIgnoringLineEndings( $expected, $actual );
Expand Down Expand Up @@ -197,7 +196,7 @@ public function testAssertStringEqualsStringIgnoringLineEndingsFailsWithCustomMe

$pattern = '`^This assertion failed for reason XYZ\s+' . \preg_quote( $msg, '`' ) . '`s';

$this->expectException( $this->getAssertionFailedExceptionName() );
$this->expectException( AssertionFailedError::class );
$this->expectExceptionMessageMatches( $pattern );

$this->assertStringEqualsStringIgnoringLineEndings( $expected, $actual, 'This assertion failed for reason XYZ' );
Expand Down Expand Up @@ -338,7 +337,7 @@ public function testAssertStringContainsStringIgnoringLineEndingsFails( $needle,
'( \[[^\]]+\]\(length: [0-9]+\))?'
);

$this->expectException( $this->getAssertionFailedExceptionName() );
$this->expectException( AssertionFailedError::class );
$this->expectExceptionMessageMatches( $pattern );

$this->assertStringContainsStringIgnoringLineEndings( $needle, $haystack );
Expand Down Expand Up @@ -372,21 +371,6 @@ public static function dataThrowsTypeErrorOnInvalidType() {
];
}

/**
* Helper function: retrieve the name of the "assertion failed" exception to expect (PHPUnit cross-version).
*
* @return string
*/
public function getAssertionFailedExceptionName() {
$exception = AssertionFailedError::class;
if ( \class_exists( PHPUnit_Framework_AssertionFailedError::class ) ) {
// PHPUnit < 6.
$exception = PHPUnit_Framework_AssertionFailedError::class;
}

return $exception;
}

/**
* Normalize line endings.
*
Expand Down
22 changes: 3 additions & 19 deletions tests/Polyfills/AssertIsListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_AssertionFailedError;
use stdClass;
use Yoast\PHPUnitPolyfills\Polyfills\AssertIsList;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches;
Expand Down Expand Up @@ -34,7 +33,7 @@ final class AssertIsListTest extends TestCase {
*/
#[DataProvider( 'dataAssertIsListFailsOnInvalidInputType' )]
public function testAssertIsListFailsOnInvalidInputType( $actual, $type ) {
$this->expectException( $this->getAssertionFailedExceptionName() );
$this->expectException( AssertionFailedError::class );
$this->expectExceptionMessageMatches( '`^Failed asserting that ' . $type . ' is a list`' );

$this->assertIsList( $actual );
Expand Down Expand Up @@ -133,7 +132,7 @@ public static function dataAssertIsListPass() {
*/
#[DataProvider( 'dataAssertIsListFail' )]
public function testAssertIsListFail( $actual ) {
$this->expectException( $this->getAssertionFailedExceptionName() );
$this->expectException( AssertionFailedError::class );
$this->expectExceptionMessage( 'Failed asserting that an array is a list' );

static::assertIsList( $actual );
Expand Down Expand Up @@ -191,7 +190,7 @@ public static function dataAssertIsListFail() {
public function testAssertIsListFailsWithCustomMessage() {
$pattern = '`^This assertion failed for reason XYZ\s+Failed asserting that an array is a list\.`';

$this->expectException( $this->getAssertionFailedExceptionName() );
$this->expectException( AssertionFailedError::class );
$this->expectExceptionMessageMatches( $pattern );

$array = [
Expand All @@ -201,19 +200,4 @@ public function testAssertIsListFailsWithCustomMessage() {

$this->assertIsList( $array, 'This assertion failed for reason XYZ' );
}

/**
* Helper function: retrieve the name of the "assertion failed" exception to expect (PHPUnit cross-version).
*
* @return string
*/
public function getAssertionFailedExceptionName() {
$exception = AssertionFailedError::class;
if ( \class_exists( PHPUnit_Framework_AssertionFailedError::class ) ) {
// PHPUnit < 6.
$exception = PHPUnit_Framework_AssertionFailedError::class;
}

return $exception;
}
}
20 changes: 2 additions & 18 deletions tests/Polyfills/AssertObjectEqualsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use PHPUnit\Framework\ComparisonMethodDoesNotExistException;
use PHPUnit\Framework\TestCase;
use PHPUnit\Runner\Version as PHPUnit_Version;
use PHPUnit_Framework_AssertionFailedError;
use stdClass;
use TypeError;
use Yoast\PHPUnitPolyfills\Exceptions\InvalidComparisonMethodException;
Expand Down Expand Up @@ -452,7 +451,7 @@ public function testAssertObjectEqualsFailsOnMethodParamTypeMismatch() {
public function testAssertObjectEqualsFailsAsNotEqual() {
$msg = 'Failed asserting that two objects are equal.';

$this->expectException( $this->getAssertionFailedExceptionName() );
$this->expectException( AssertionFailedError::class );
$this->expectExceptionMessage( $msg );

$expected = new ValueObject( 'test' );
Expand All @@ -469,26 +468,11 @@ public function testAssertObjectEqualsFailsAsNotEqual() {
public function testAssertObjectEqualsFailsAsNotEqualWithCustomMessage() {
$pattern = '`^This assertion failed for reason XYZ\s+Failed asserting that two objects are equal\.`';

$this->expectException( $this->getAssertionFailedExceptionName() );
$this->expectException( AssertionFailedError::class );
$this->expectExceptionMessageMatches( $pattern );

$expected = new ValueObject( 'test' );
$actual = new ValueObject( 'testing... 1..2..3' );
$this->assertObjectEquals( $expected, $actual, 'equals', 'This assertion failed for reason XYZ' );
}

/**
* Helper function: retrieve the name of the "assertion failed" exception to expect (PHPUnit cross-version).
*
* @return string
*/
public function getAssertionFailedExceptionName() {
$exception = AssertionFailedError::class;
if ( \class_exists( PHPUnit_Framework_AssertionFailedError::class ) ) {
// PHPUnit < 6.
$exception = PHPUnit_Framework_AssertionFailedError::class;
}

return $exception;
}
}
24 changes: 4 additions & 20 deletions tests/Polyfills/AssertObjectPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use PHPUnit\Runner\Version as PHPUnit_Version;
use PHPUnit_Framework_AssertionFailedError;
use stdClass;
use TypeError;
use Yoast\PHPUnitPolyfills\Polyfills\AssertObjectProperty;
Expand Down Expand Up @@ -237,7 +236,7 @@ public function testAssertObjectHasPropertyFails( $name ) {
\preg_quote( $name, '`' )
);

$this->expectException( $this->getAssertionFailedExceptionName() );
$this->expectException( AssertionFailedError::class );
$this->expectExceptionMessageMatches( $pattern );

static::assertObjectHasProperty( $name, new ObjectWithProperties() );
Expand All @@ -259,7 +258,7 @@ public function testAssertObjectNotHasPropertyFails( $name ) {
\preg_quote( $name, '`' )
);

$this->expectException( $this->getAssertionFailedExceptionName() );
$this->expectException( AssertionFailedError::class );
$this->expectExceptionMessageMatches( $pattern );

$this->assertObjectNotHasProperty( $name, new ObjectWithProperties() );
Expand Down Expand Up @@ -304,7 +303,7 @@ public static function dataAssertObjectPropertyUnavailableProps() {
public function testAssertObjectHasPropertyFailsWithCustomMessage() {
$pattern = '`^This assertion failed for reason XYZ\s+Failed asserting that object of class `';

$this->expectException( $this->getAssertionFailedExceptionName() );
$this->expectException( AssertionFailedError::class );
$this->expectExceptionMessageMatches( $pattern );

$this->assertObjectHasProperty( 'doesNotExist', new ObjectWithProperties(), 'This assertion failed for reason XYZ' );
Expand All @@ -319,24 +318,9 @@ public function testAssertObjectHasPropertyFailsWithCustomMessage() {
public function testAssertObjectNotHasPropertyFailsWithCustomMessage() {
$pattern = '`^This assertion failed for reason XYZ\s+Failed asserting that object of class `';

$this->expectException( $this->getAssertionFailedExceptionName() );
$this->expectException( AssertionFailedError::class );
$this->expectExceptionMessageMatches( $pattern );

$this->assertObjectNotHasProperty( 'protectedWithDefaultValue', new ObjectWithProperties(), 'This assertion failed for reason XYZ' );
}

/**
* Helper function: retrieve the name of the "assertion failed" exception to expect (PHPUnit cross-version).
*
* @return string
*/
public function getAssertionFailedExceptionName() {
$exception = AssertionFailedError::class;
if ( \class_exists( PHPUnit_Framework_AssertionFailedError::class ) ) {
// PHPUnit < 6.
$exception = PHPUnit_Framework_AssertionFailedError::class;
}

return $exception;
}
}
Loading

0 comments on commit 1ea653a

Please sign in to comment.