Skip to content

Commit

Permalink
Merge pull request #65 from jrfnl/feature/50-prevent-warnings-on-phpu…
Browse files Browse the repository at this point in the history
…nit-50

Allow the polyfill to kick in with PHPUnit 8.x
  • Loading branch information
rdohms authored Feb 13, 2022
2 parents 0afc0f2 + 96fd5ea commit 428293c
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 38 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ jobs:
experimental: [false]

include:
- php: '7.2'
dependency-version: 'prefer-lowest'
experimental: false
- php: '7.3'
dependency-version: 'prefer-lowest'
experimental: false
Expand Down Expand Up @@ -67,8 +70,12 @@ jobs:
- name: 'Composer: remove CS dependency'
run: composer remove --dev --no-update dms/coding-standard --no-interaction

- name: 'Composer: update PHPUnit for testing lowest'
if: ${{ matrix.dependency-version == 'prefer-lowest' }}
- name: 'Composer: update PHPUnit for testing lowest (PHP 7.2)'
if: ${{ matrix.dependency-version == 'prefer-lowest' && matrix.php == '7.2' }}
run: composer require --no-update phpunit/phpunit:"^8.0" --no-interaction

- name: 'Composer: update PHPUnit for testing lowest (PHP 7.3+)'
if: ${{ matrix.dependency-version == 'prefer-lowest' && matrix.php != '7.2' }}
run: composer require --no-update phpunit/phpunit:"^9.0" --no-interaction

- name: Install dependencies - normal
Expand Down
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ composer require --dev dms/phpunit-arraysubset-asserts

> :bulb: The package can be safely required on PHP 5.4 to current in combination with PHPUnit 4.8.36/5.7.21 to current.
>
> When the PHPUnit `assertArraySubset()` method is natively available (PHPUnit 4.x - 8.x), the PHPUnit native functionality will be used.
> For PHPUnit 9 and higher, the extension will kick in and polyfill the functionality which was removed from PHPUnit.
>
> Note: PHPUnit 8.x will show deprecation notices about the use of the `assertArraySubset()` method.
> With this extension in place, those can be safely ignored.
> When the PHPUnit `assertArraySubset()` method is natively available and not deprecated (PHPUnit 4.x - 7.x),
> the PHPUnit native functionality will be used.
> For PHPUnit 8 and higher, the extension will kick in and polyfill the functionality which was removed from PHPUnit.

## Usage
Expand Down
48 changes: 37 additions & 11 deletions assertarraysubset-autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace DMS\PHPUnitExtensions\ArraySubset;

use PHPUnit\Runner\Version as PHPUnit_Version;
use PHPUnit_Runner_Version;

if (\class_exists('DMS\PHPUnitExtensions\ArraySubset\Autoload', false) === false) {

/**
Expand All @@ -25,49 +28,51 @@ public static function load($className)
return false;
}

$loadPolyfill = \version_compare(self::getPHPUnitVersion(), '8.0.0', '>=');

switch ($className) {
case 'DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts':
if (\method_exists('\PHPUnit\Framework\Assert', 'assertArraySubset') === false) {
// PHPUnit >= 9.0.0.
if ($loadPolyfill === true) {
// PHPUnit >= 8.0.0.
require_once __DIR__ . '/src/ArraySubsetAsserts.php';

return true;
}

// PHPUnit < 9.0.0.
// PHPUnit < 8.0.0.
require_once __DIR__ . '/src/ArraySubsetAssertsEmpty.php';

return true;

case 'DMS\PHPUnitExtensions\ArraySubset\Assert':
if (\method_exists('\PHPUnit\Framework\Assert', 'assertArraySubset') === false) {
// PHPUnit >= 9.0.0.
if ($loadPolyfill === true) {
// PHPUnit >= 8.0.0.
require_once __DIR__ . '/src/Assert.php';

return true;
}

// PHPUnit < 9.0.0.
// PHPUnit < 8.0.0.
require_once __DIR__ . '/src/AssertFallThrough.php';

return true;

/*
* Handle arbitrary additional classes via PSR-4, but only allow loading on PHPUnit >= 9.0.0,
* as additional classes should only ever _need_ to be loaded when using PHPUnit >= 9.0.0.
* Handle arbitrary additional classes via PSR-4, but only allow loading on PHPUnit >= 8.0.0,
* as additional classes should only ever _need_ to be loaded when using PHPUnit >= 8.0.0.
*/
default:
if (\method_exists('\PHPUnit\Framework\Assert', 'assertArraySubset') === true) {
if ($loadPolyfill === false) {
// PHPUnit < 9.0.0.
throw new \RuntimeException(
\sprintf(
'Using class "%s" is only supported in combination with PHPUnit >= 9.0.0',
'Using class "%s" is only supported in combination with PHPUnit >= 8.0.0',
$className
)
);
}

// PHPUnit >= 9.0.0.
// PHPUnit >= 8.0.0.
$file = \realpath(
__DIR__ . \DIRECTORY_SEPARATOR
. 'src' . \DIRECTORY_SEPARATOR
Expand All @@ -83,6 +88,27 @@ public static function load($className)

return false;
}

/**
* 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';
}
}

\spl_autoload_register(__NAMESPACE__ . '\Autoload::load');
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<testsuites>
<testsuite name="unit">
<directory>tests/Availability</directory>
<directory phpVersion="7.3.0" phpVersionOperator=">=">tests/Unit</directory>
<directory phpVersion="7.2.0" phpVersionOperator=">=">tests/Unit</directory>
</testsuite>
</testsuites>

Expand Down
26 changes: 23 additions & 3 deletions src/ArraySubsetAsserts.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use PHPUnit\Framework\Assert as PhpUnitAssert;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\InvalidArgumentException;
use PHPUnit\Util\InvalidArgumentHelper;

use function class_exists;
use function is_array;

trait ArraySubsetAsserts
Expand All @@ -22,20 +24,38 @@ trait ArraySubsetAsserts
* @param array|ArrayAccess|mixed[] $array
*
* @throws ExpectationFailedException
* @throws InvalidArgumentException
* @throws InvalidArgumentException|Exception
* @throws Exception
*/
public static function assertArraySubset($subset, $array, bool $checkForObjectIdentity = false, string $message = ''): void
{
if (! (is_array($subset) || $subset instanceof ArrayAccess)) {
throw InvalidArgumentException::create(
if (class_exists(InvalidArgumentException::class)) {
// PHPUnit 8.4.0+.
throw InvalidArgumentException::create(
1,
'array or ArrayAccess'
);
}

// PHPUnit < 8.4.0.
throw InvalidArgumentHelper::factory(
1,
'array or ArrayAccess'
);
}

if (! (is_array($array) || $array instanceof ArrayAccess)) {
throw InvalidArgumentException::create(
if (class_exists(InvalidArgumentException::class)) {
// PHPUnit 8.4.0+.
throw InvalidArgumentException::create(
2,
'array or ArrayAccess'
);
}

// PHPUnit < 8.4.0.
throw InvalidArgumentHelper::factory(
2,
'array or ArrayAccess'
);
Expand Down
2 changes: 1 addition & 1 deletion src/ArraySubsetAssertsEmpty.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace DMS\PHPUnitExtensions\ArraySubset;

/**
* ArraySubsetAsserts trait for use with PHPUnit 4.x - 8.x.
* ArraySubsetAsserts trait for use with PHPUnit 4.x - 7.x.
*
* As this trait is empty, calls to `assertArraySubset()` will automatically fall through
* to PHPUnit itself and will use the PHPUnit native `assertArraySubset()` method.
Expand Down
4 changes: 2 additions & 2 deletions src/AssertFallThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use PHPUnit\Framework\Assert as PhpUnitAssert;

/**
* Assert class for use with PHPUnit 4.x - 8.x.
* Assert class for use with PHPUnit 4.x - 7.x.
*
* The method in this class will fall through to PHPUnit itself and use the PHPUnit
* native `assertArraySubset()` method.
Expand All @@ -23,7 +23,7 @@ final class Assert
* @param string $message
*
* @throws ExpectationFailedException
* @throws InvalidArgumentException
* @throws InvalidArgumentException|Exception
* @throws Exception
*/
public static function assertArraySubset($subset, $array, $checkForObjectIdentity = false, $message = '')
Expand Down
6 changes: 3 additions & 3 deletions src/Constraint/ArraySubset.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function __construct(iterable $subset, bool $strict = false)
* @return mixed[]|bool|null
*
* @throws ExpectationFailedException
* @throws InvalidArgumentException
* @throws InvalidArgumentException|Exception
*/
public function evaluate($other, string $description = '', bool $returnResult = false): ?bool
{
Expand Down Expand Up @@ -103,7 +103,7 @@ public function evaluate($other, string $description = '', bool $returnResult =
/**
* Returns a string representation of the constraint.
*
* @throws InvalidArgumentException
* @throws InvalidArgumentException|Exception
*/
public function toString(): string
{
Expand All @@ -118,7 +118,7 @@ public function toString(): string
*
* @param mixed $other evaluated value or object
*
* @throws InvalidArgumentException
* @throws InvalidArgumentException|Exception
*/
protected function failureDescription($other): string
{
Expand Down
10 changes: 5 additions & 5 deletions tests/Availability/AutoloadExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
use PHPUnit\Framework\TestCase;

/**
* Testing that autoloading classes which should only ever be loaded on PHPUnit >= 9 will
* generate an exception when attempted on PHPUnit < 9..
* Testing that autoloading classes which should only ever be loaded on PHPUnit >= 8 will
* generate an exception when attempted on PHPUnit < 8.
*
* Note: the autoloading in combination with PHPUnit 9+ is automatically tested via the
* Note: the autoloading in combination with PHPUnit 8+ is automatically tested via the
* actual tests for the polyfill as the class will be called upon.
*
* {@internal The code in this file must be PHP cross-version compatible for PHP 5.4 - current!}
*
* @requires PHPUnit < 9
* @requires PHPUnit < 8
*/
final class AutoloadExceptionTest extends TestCase
{
public function testAutoloadException()
{
$expection = '\RuntimeException';
$message = 'Using class "DMS\PHPUnitExtensions\ArraySubset\Constraint\ArraySubset" is only supported in combination with PHPUnit >= 9.0.0';
$message = 'Using class "DMS\PHPUnitExtensions\ArraySubset\Constraint\ArraySubset" is only supported in combination with PHPUnit >= 8.0.0';

if (\method_exists('\PHPUnit\Framework\TestCase', 'expectException') === true) {
$this->expectException($expection);
Expand Down
2 changes: 1 addition & 1 deletion tests/Availability/AvailibilityViaClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use PHPUnit\Framework\TestCase;

/**
* Testing that the autoloading of the fall-through `Assert` class for PHPUnit 4.x - 8.x works correctly.
* Testing that the autoloading of the fall-through `Assert` class for PHPUnit 4.x - 7.x works correctly.
*
* {@internal The code in this file must be PHP cross-version compatible for PHP 5.4 - current!}
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Availability/AvailibilityViaTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use PHPUnit\Framework\TestCase;

/**
* Testing that the autoloading of the empty `ArraySubsetAsserts` trait for PHPUnit 4.x - 8.x works correctly.
* Testing that the autoloading of the empty `ArraySubsetAsserts` trait for PHPUnit 4.x - 7.x works correctly.
*
* {@internal The code in this file must be PHP cross-version compatible for PHP 5.4 - current!}
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use PHPUnit\Framework\TestCase;

/**
* @requires PHPUnit >= 9
* @requires PHPUnit >= 8
*/
final class AssertTest extends TestCase
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Constraint/ArraySubsetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use function sprintf;

/**
* @requires PHPUnit >= 9
* @requires PHPUnit >= 8
*/
final class ArraySubsetTest extends TestCase
{
Expand Down Expand Up @@ -60,7 +60,7 @@ public static function evaluateDataProvider(): array
* @param array|Traversable|mixed[] $other
*
* @throws ExpectationFailedException
* @throws InvalidArgumentException
* @throws InvalidArgumentException|Exception
*
* @dataProvider evaluateDataProvider
*/
Expand Down

0 comments on commit 428293c

Please sign in to comment.