Skip to content

Commit eaf0534

Browse files
committed
Add custom autoloader/fall-through to PHPUnit native functionality
This commit: * Adds a custom autoloader which will either load the polyfills or will load "empty" replacements for the classes in this package which will fall through to using the PHPUnit native functionality for PHPUnit 4.x - 8.x. * Widens the version requirements for PHP and PHPUnit to allow installing of this package on PHP 5.4 - current in combination with PHPUnit 4.8.36/5.7.21 - current. * Updates the `autoload` section in the `composer.json` to load the custom autoloader instead of using PSR4 autoloading. * Makes a few select exceptions to the coding standards for the custom autoloader and the "fall through" files for CS rules which require a higher minimum PHP version. - `strict_types` is only available as of PHP 7.0. - scalar type declarations and return types are only available as of PHP 7.0, with `void` only being available as of PHP 7.1. - Importing functions and constants with `use` statements is only available as of PHP 5.6. * Updates the README with information about the fact that the package no longer restrict installation to PHPUnit 9.
1 parent 494da46 commit eaf0534

File tree

7 files changed

+154
-8
lines changed

7 files changed

+154
-8
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ Simply use it by importing it with Composer
1313
composer require --dev dms/phpunit-arraysubset-asserts
1414
```
1515

16+
> :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.
17+
>
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.
23+
24+
1625
## Usage
1726

1827
You have two options to use this in your classes: either directly as a static call or as a trait if you wish to keep existing references working.

assertarraysubset-autoload.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace DMS\PHPUnitExtensions\ArraySubset;
4+
5+
if (\class_exists('DMS\PHPUnitExtensions\ArraySubset\Autoload', false) === false) {
6+
7+
/**
8+
* Custom autoloader.
9+
*
10+
* {@internal The code in this file must be PHP cross-version compatible for PHP 5.4 - current!}
11+
*/
12+
class Autoload
13+
{
14+
/**
15+
* Loads a class.
16+
*
17+
* @param string $className The name of the class to load.
18+
*
19+
* @return bool
20+
*/
21+
public static function load($className)
22+
{
23+
// Only load classes belonging to this library.
24+
if (\stripos($className, 'DMS\PHPUnitExtensions\ArraySubset') !== 0) {
25+
return false;
26+
}
27+
28+
switch ($className) {
29+
case 'DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts':
30+
if (\method_exists('\PHPUnit\Framework\Assert', 'assertArraySubset') === false) {
31+
// PHPUnit >= 9.0.0.
32+
require_once __DIR__ . '/src/ArraySubsetAsserts.php';
33+
34+
// Straight away load the other classes needed as well.
35+
// These should only ever be loaded in this context anyway.
36+
require_once __DIR__ . '/src/ArrayAccessible.php';
37+
require_once __DIR__ . '/src/Constraint/ArraySubset.php';
38+
39+
return true;
40+
}
41+
42+
// PHPUnit < 9.0.0.
43+
require_once __DIR__ . '/src/ArraySubsetAssertsEmpty.php';
44+
45+
return true;
46+
47+
case 'DMS\PHPUnitExtensions\ArraySubset\Assert':
48+
if (\method_exists('\PHPUnit\Framework\Assert', 'assertArraySubset') === false) {
49+
// PHPUnit >= 9.0.0.
50+
require_once __DIR__ . '/src/Assert.php';
51+
52+
return true;
53+
}
54+
55+
// PHPUnit < 9.0.0.
56+
require_once __DIR__ . '/src/AssertFallThrough.php';
57+
58+
return true;
59+
}
60+
61+
return false;
62+
}
63+
}
64+
65+
\spl_autoload_register(__NAMESPACE__ . '\Autoload::load');
66+
}

composer.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"description": "This package provides ArraySubset and related asserts once deprecated in PHPUnit 8",
44
"type": "library",
55
"require": {
6-
"phpunit/phpunit": "^9.0",
7-
"php": "^7.3|^7.4|^8.0"
6+
"phpunit/phpunit": "^4.8.36 || ^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0",
7+
"php": "^5.4 || ^7.0 || ^8.0"
88
},
99
"license": "MIT",
1010
"authors": [
@@ -18,9 +18,7 @@
1818
"dms/coding-standard": "^8"
1919
},
2020
"autoload": {
21-
"psr-4": {
22-
"DMS\\PHPUnitExtensions\\ArraySubset\\": "src"
23-
}
21+
"files": ["assertarraysubset-autoload.php"]
2422
},
2523
"autoload-dev": {
2624
"psr-4": {

composer.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpcs.xml.dist

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<arg name="cache" value=".phpcs.cache" /> <!-- cache the results and don't commit them -->
88
<arg value="np" /> <!-- n = ignore warnings, p = show progress -->
99

10+
<file>assertarraysubset-autoload.php</file>
1011
<file>src</file>
1112
<file>tests</file>
1213

@@ -15,6 +16,30 @@
1516
</rule>
1617

1718
<rule ref="SlevomatCodingStandard.Operators.DisallowEqualOperators.DisallowedEqualOperator">
18-
<exclude-pattern>src/Constraint/ArraySubset.php</exclude-pattern>
19+
<exclude-pattern>src/Constraint/ArraySubset\.php</exclude-pattern>
1920
</rule>
21+
22+
<!-- BC-layer: exclude rules which would prevent code from being PHP cross-version compatible. -->
23+
<rule ref="Squiz.Classes.ClassFileName.NoMatch">
24+
<exclude-pattern>assertarraysubset-autoload\.php</exclude-pattern>
25+
<exclude-pattern>src/ArraySubsetAssertsEmpty\.php</exclude-pattern>
26+
<exclude-pattern>src/AssertFallThrough\.php</exclude-pattern>
27+
</rule>
28+
<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing">
29+
<exclude-pattern>assertarraysubset-autoload\.php</exclude-pattern>
30+
<exclude-pattern>src/ArraySubsetAssertsEmpty\.php</exclude-pattern>
31+
<exclude-pattern>src/AssertFallThrough\.php</exclude-pattern>
32+
</rule>
33+
<rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName">
34+
<exclude-pattern>assertarraysubset-autoload\.php</exclude-pattern>
35+
</rule>
36+
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint">
37+
<exclude-pattern>assertarraysubset-autoload\.php</exclude-pattern>
38+
<exclude-pattern>src/AssertFallThrough\.php</exclude-pattern>
39+
</rule>
40+
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint">
41+
<exclude-pattern>assertarraysubset-autoload\.php</exclude-pattern>
42+
<exclude-pattern>src/AssertFallThrough\.php</exclude-pattern>
43+
</rule>
44+
2045
</ruleset>

src/ArraySubsetAssertsEmpty.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace DMS\PHPUnitExtensions\ArraySubset;
4+
5+
/**
6+
* ArraySubsetAsserts trait for use with PHPUnit 4.x - 8.x.
7+
*
8+
* As this trait is empty, calls to `assertArraySubset()` will automatically fall through
9+
* to PHPUnit itself and will use the PHPUnit native `assertArraySubset()` method.
10+
*
11+
* {@internal The code in this file must be PHP cross-version compatible for PHP 5.4 - current!}
12+
*/
13+
trait ArraySubsetAsserts
14+
{
15+
}

src/AssertFallThrough.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace DMS\PHPUnitExtensions\ArraySubset;
4+
5+
use PHPUnit\Framework\Assert as PhpUnitAssert;
6+
7+
/**
8+
* Assert class for use with PHPUnit 4.x - 8.x.
9+
*
10+
* The method in this class will fall through to PHPUnit itself and use the PHPUnit
11+
* native `assertArraySubset()` method.
12+
*
13+
* {@internal The code in this file must be PHP cross-version compatible for PHP 5.4 - current!}
14+
*/
15+
final class Assert
16+
{
17+
/**
18+
* Asserts that an array has a specified subset.
19+
*
20+
* @param array|ArrayAccess|mixed[] $subset
21+
* @param array|ArrayAccess|mixed[] $array
22+
* @param bool $checkForObjectIdentity
23+
* @param string $message
24+
*
25+
* @throws ExpectationFailedException
26+
* @throws InvalidArgumentException
27+
* @throws Exception
28+
*/
29+
public static function assertArraySubset($subset, $array, $checkForObjectIdentity = false, $message = '')
30+
{
31+
PhpUnitAssert::assertArraySubset($subset, $array, $checkForObjectIdentity, $message);
32+
}
33+
}

0 commit comments

Comments
 (0)