Skip to content

Commit 428293c

Browse files
authored
Merge pull request #65 from jrfnl/feature/50-prevent-warnings-on-phpunit-50
Allow the polyfill to kick in with PHPUnit 8.x
2 parents 0afc0f2 + 96fd5ea commit 428293c

13 files changed

+89
-38
lines changed

.github/workflows/run-tests.yml

+9-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ jobs:
3030
experimental: [false]
3131

3232
include:
33+
- php: '7.2'
34+
dependency-version: 'prefer-lowest'
35+
experimental: false
3336
- php: '7.3'
3437
dependency-version: 'prefer-lowest'
3538
experimental: false
@@ -67,8 +70,12 @@ jobs:
6770
- name: 'Composer: remove CS dependency'
6871
run: composer remove --dev --no-update dms/coding-standard --no-interaction
6972

70-
- name: 'Composer: update PHPUnit for testing lowest'
71-
if: ${{ matrix.dependency-version == 'prefer-lowest' }}
73+
- name: 'Composer: update PHPUnit for testing lowest (PHP 7.2)'
74+
if: ${{ matrix.dependency-version == 'prefer-lowest' && matrix.php == '7.2' }}
75+
run: composer require --no-update phpunit/phpunit:"^8.0" --no-interaction
76+
77+
- name: 'Composer: update PHPUnit for testing lowest (PHP 7.3+)'
78+
if: ${{ matrix.dependency-version == 'prefer-lowest' && matrix.php != '7.2' }}
7279
run: composer require --no-update phpunit/phpunit:"^9.0" --no-interaction
7380

7481
- name: Install dependencies - normal

README.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ composer require --dev dms/phpunit-arraysubset-asserts
1515

1616
> :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.
1717
>
18-
> When the PHPUnit `assertArraySubset()` method is natively available (PHPUnit 4.x - 8.x), the PHPUnit native functionality will be used.
19-
> For PHPUnit 9 and higher, the extension will kick in and polyfill the functionality which was removed from PHPUnit.
20-
>
21-
> Note: PHPUnit 8.x will show deprecation notices about the use of the `assertArraySubset()` method.
22-
> With this extension in place, those can be safely ignored.
18+
> When the PHPUnit `assertArraySubset()` method is natively available and not deprecated (PHPUnit 4.x - 7.x),
19+
> the PHPUnit native functionality will be used.
20+
> For PHPUnit 8 and higher, the extension will kick in and polyfill the functionality which was removed from PHPUnit.
2321
2422

2523
## Usage

assertarraysubset-autoload.php

+37-11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace DMS\PHPUnitExtensions\ArraySubset;
44

5+
use PHPUnit\Runner\Version as PHPUnit_Version;
6+
use PHPUnit_Runner_Version;
7+
58
if (\class_exists('DMS\PHPUnitExtensions\ArraySubset\Autoload', false) === false) {
69

710
/**
@@ -25,49 +28,51 @@ public static function load($className)
2528
return false;
2629
}
2730

31+
$loadPolyfill = \version_compare(self::getPHPUnitVersion(), '8.0.0', '>=');
32+
2833
switch ($className) {
2934
case 'DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts':
30-
if (\method_exists('\PHPUnit\Framework\Assert', 'assertArraySubset') === false) {
31-
// PHPUnit >= 9.0.0.
35+
if ($loadPolyfill === true) {
36+
// PHPUnit >= 8.0.0.
3237
require_once __DIR__ . '/src/ArraySubsetAsserts.php';
3338

3439
return true;
3540
}
3641

37-
// PHPUnit < 9.0.0.
42+
// PHPUnit < 8.0.0.
3843
require_once __DIR__ . '/src/ArraySubsetAssertsEmpty.php';
3944

4045
return true;
4146

4247
case 'DMS\PHPUnitExtensions\ArraySubset\Assert':
43-
if (\method_exists('\PHPUnit\Framework\Assert', 'assertArraySubset') === false) {
44-
// PHPUnit >= 9.0.0.
48+
if ($loadPolyfill === true) {
49+
// PHPUnit >= 8.0.0.
4550
require_once __DIR__ . '/src/Assert.php';
4651

4752
return true;
4853
}
4954

50-
// PHPUnit < 9.0.0.
55+
// PHPUnit < 8.0.0.
5156
require_once __DIR__ . '/src/AssertFallThrough.php';
5257

5358
return true;
5459

5560
/*
56-
* Handle arbitrary additional classes via PSR-4, but only allow loading on PHPUnit >= 9.0.0,
57-
* as additional classes should only ever _need_ to be loaded when using PHPUnit >= 9.0.0.
61+
* Handle arbitrary additional classes via PSR-4, but only allow loading on PHPUnit >= 8.0.0,
62+
* as additional classes should only ever _need_ to be loaded when using PHPUnit >= 8.0.0.
5863
*/
5964
default:
60-
if (\method_exists('\PHPUnit\Framework\Assert', 'assertArraySubset') === true) {
65+
if ($loadPolyfill === false) {
6166
// PHPUnit < 9.0.0.
6267
throw new \RuntimeException(
6368
\sprintf(
64-
'Using class "%s" is only supported in combination with PHPUnit >= 9.0.0',
69+
'Using class "%s" is only supported in combination with PHPUnit >= 8.0.0',
6570
$className
6671
)
6772
);
6873
}
6974

70-
// PHPUnit >= 9.0.0.
75+
// PHPUnit >= 8.0.0.
7176
$file = \realpath(
7277
__DIR__ . \DIRECTORY_SEPARATOR
7378
. 'src' . \DIRECTORY_SEPARATOR
@@ -83,6 +88,27 @@ public static function load($className)
8388

8489
return false;
8590
}
91+
92+
/**
93+
* Retrieve the PHPUnit version id.
94+
*
95+
* As both the pre-PHPUnit 6 class, as well as the PHPUnit 6+ class contain the `id()` function,
96+
* this should work independently of whether or not another library may have aliased the class.
97+
*
98+
* @return string Version number as a string.
99+
*/
100+
public static function getPHPUnitVersion()
101+
{
102+
if (\class_exists('\PHPUnit\Runner\Version')) {
103+
return PHPUnit_Version::id();
104+
}
105+
106+
if (\class_exists('\PHPUnit_Runner_Version')) {
107+
return PHPUnit_Runner_Version::id();
108+
}
109+
110+
return '0';
111+
}
86112
}
87113

88114
\spl_autoload_register(__NAMESPACE__ . '\Autoload::load');

phpunit.xml.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<testsuites>
1717
<testsuite name="unit">
1818
<directory>tests/Availability</directory>
19-
<directory phpVersion="7.3.0" phpVersionOperator=">=">tests/Unit</directory>
19+
<directory phpVersion="7.2.0" phpVersionOperator=">=">tests/Unit</directory>
2020
</testsuite>
2121
</testsuites>
2222

src/ArraySubsetAsserts.php

+23-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
use PHPUnit\Framework\Assert as PhpUnitAssert;
1111
use PHPUnit\Framework\ExpectationFailedException;
1212
use PHPUnit\Framework\InvalidArgumentException;
13+
use PHPUnit\Util\InvalidArgumentHelper;
1314

15+
use function class_exists;
1416
use function is_array;
1517

1618
trait ArraySubsetAsserts
@@ -22,20 +24,38 @@ trait ArraySubsetAsserts
2224
* @param array|ArrayAccess|mixed[] $array
2325
*
2426
* @throws ExpectationFailedException
25-
* @throws InvalidArgumentException
27+
* @throws InvalidArgumentException|Exception
2628
* @throws Exception
2729
*/
2830
public static function assertArraySubset($subset, $array, bool $checkForObjectIdentity = false, string $message = ''): void
2931
{
3032
if (! (is_array($subset) || $subset instanceof ArrayAccess)) {
31-
throw InvalidArgumentException::create(
33+
if (class_exists(InvalidArgumentException::class)) {
34+
// PHPUnit 8.4.0+.
35+
throw InvalidArgumentException::create(
36+
1,
37+
'array or ArrayAccess'
38+
);
39+
}
40+
41+
// PHPUnit < 8.4.0.
42+
throw InvalidArgumentHelper::factory(
3243
1,
3344
'array or ArrayAccess'
3445
);
3546
}
3647

3748
if (! (is_array($array) || $array instanceof ArrayAccess)) {
38-
throw InvalidArgumentException::create(
49+
if (class_exists(InvalidArgumentException::class)) {
50+
// PHPUnit 8.4.0+.
51+
throw InvalidArgumentException::create(
52+
2,
53+
'array or ArrayAccess'
54+
);
55+
}
56+
57+
// PHPUnit < 8.4.0.
58+
throw InvalidArgumentHelper::factory(
3959
2,
4060
'array or ArrayAccess'
4161
);

src/ArraySubsetAssertsEmpty.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace DMS\PHPUnitExtensions\ArraySubset;
44

55
/**
6-
* ArraySubsetAsserts trait for use with PHPUnit 4.x - 8.x.
6+
* ArraySubsetAsserts trait for use with PHPUnit 4.x - 7.x.
77
*
88
* As this trait is empty, calls to `assertArraySubset()` will automatically fall through
99
* to PHPUnit itself and will use the PHPUnit native `assertArraySubset()` method.

src/AssertFallThrough.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use PHPUnit\Framework\Assert as PhpUnitAssert;
66

77
/**
8-
* Assert class for use with PHPUnit 4.x - 8.x.
8+
* Assert class for use with PHPUnit 4.x - 7.x.
99
*
1010
* The method in this class will fall through to PHPUnit itself and use the PHPUnit
1111
* native `assertArraySubset()` method.
@@ -23,7 +23,7 @@ final class Assert
2323
* @param string $message
2424
*
2525
* @throws ExpectationFailedException
26-
* @throws InvalidArgumentException
26+
* @throws InvalidArgumentException|Exception
2727
* @throws Exception
2828
*/
2929
public static function assertArraySubset($subset, $array, $checkForObjectIdentity = false, $message = '')

src/Constraint/ArraySubset.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function __construct(iterable $subset, bool $strict = false)
6161
* @return mixed[]|bool|null
6262
*
6363
* @throws ExpectationFailedException
64-
* @throws InvalidArgumentException
64+
* @throws InvalidArgumentException|Exception
6565
*/
6666
public function evaluate($other, string $description = '', bool $returnResult = false): ?bool
6767
{
@@ -103,7 +103,7 @@ public function evaluate($other, string $description = '', bool $returnResult =
103103
/**
104104
* Returns a string representation of the constraint.
105105
*
106-
* @throws InvalidArgumentException
106+
* @throws InvalidArgumentException|Exception
107107
*/
108108
public function toString(): string
109109
{
@@ -118,7 +118,7 @@ public function toString(): string
118118
*
119119
* @param mixed $other evaluated value or object
120120
*
121-
* @throws InvalidArgumentException
121+
* @throws InvalidArgumentException|Exception
122122
*/
123123
protected function failureDescription($other): string
124124
{

tests/Availability/AutoloadExceptionTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@
66
use PHPUnit\Framework\TestCase;
77

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

2626
if (\method_exists('\PHPUnit\Framework\TestCase', 'expectException') === true) {
2727
$this->expectException($expection);

tests/Availability/AvailibilityViaClassTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use PHPUnit\Framework\TestCase;
77

88
/**
9-
* Testing that the autoloading of the fall-through `Assert` class for PHPUnit 4.x - 8.x works correctly.
9+
* Testing that the autoloading of the fall-through `Assert` class for PHPUnit 4.x - 7.x works correctly.
1010
*
1111
* {@internal The code in this file must be PHP cross-version compatible for PHP 5.4 - current!}
1212
*/

tests/Availability/AvailibilityViaTraitTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use PHPUnit\Framework\TestCase;
77

88
/**
9-
* Testing that the autoloading of the empty `ArraySubsetAsserts` trait for PHPUnit 4.x - 8.x works correctly.
9+
* Testing that the autoloading of the empty `ArraySubsetAsserts` trait for PHPUnit 4.x - 7.x works correctly.
1010
*
1111
* {@internal The code in this file must be PHP cross-version compatible for PHP 5.4 - current!}
1212
*/

tests/Unit/AssertTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use PHPUnit\Framework\TestCase;
1111

1212
/**
13-
* @requires PHPUnit >= 9
13+
* @requires PHPUnit >= 8
1414
*/
1515
final class AssertTest extends TestCase
1616
{

tests/Unit/Constraint/ArraySubsetTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use function sprintf;
1919

2020
/**
21-
* @requires PHPUnit >= 9
21+
* @requires PHPUnit >= 8
2222
*/
2323
final class ArraySubsetTest extends TestCase
2424
{
@@ -60,7 +60,7 @@ public static function evaluateDataProvider(): array
6060
* @param array|Traversable|mixed[] $other
6161
*
6262
* @throws ExpectationFailedException
63-
* @throws InvalidArgumentException
63+
* @throws InvalidArgumentException|Exception
6464
*
6565
* @dataProvider evaluateDataProvider
6666
*/

0 commit comments

Comments
 (0)