Skip to content

Commit ab8c440

Browse files
authored
Merge pull request #48 from jrfnl/feature/allow-install-on-php-lt-7.3
Allow installation on PHP < 7.3 icw PHPUnit < 9
2 parents 494da46 + e609974 commit ab8c440

15 files changed

+304
-12
lines changed

.github/workflows/run-tests.yml

+21-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,21 @@ jobs:
2121

2222
strategy:
2323
matrix:
24-
php: ['8.0', '7.4', '7.3']
25-
dependency-version: [prefer-lowest, prefer-stable]
24+
php: ['8.0', '7.4', '7.3', '7.2', '7.1', '7.0', '5.6', '5.5', '5.4']
25+
dependency-version: ['prefer-stable']
2626
experimental: [false]
2727

2828
include:
29+
- php: '7.3'
30+
dependency-version: 'prefer-lowest'
31+
experimental: false
32+
- php: '7.4'
33+
dependency-version: 'prefer-lowest'
34+
experimental: false
35+
- php: '8.0'
36+
dependency-version: 'prefer-lowest'
37+
experimental: false
38+
2939
- php: '8.1'
3040
dependency-version: 'prefer-stable'
3141
experimental: true
@@ -44,6 +54,15 @@ jobs:
4454
coverage: none
4555
tools: composer
4656

57+
# Remove the coding standards package as it has a higher minimum PHP
58+
# requirement and would prevent running the tests on older PHP versions.
59+
- name: 'Composer: remove CS dependency'
60+
run: composer remove --dev --no-update dms/coding-standard
61+
62+
- name: 'Composer: update PHPUnit for testing lowest'
63+
if: ${{ matrix.dependency-version == 'prefer-lowest' }}
64+
run: composer require --no-update phpunit/phpunit:"^9.0"
65+
4766
- name: Install dependencies - normal
4867
if: ${{ matrix.php < 8.1 }}
4968
run: |

README.md

+9
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

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
return true;
35+
}
36+
37+
// PHPUnit < 9.0.0.
38+
require_once __DIR__ . '/src/ArraySubsetAssertsEmpty.php';
39+
40+
return true;
41+
42+
case 'DMS\PHPUnitExtensions\ArraySubset\Assert':
43+
if (\method_exists('\PHPUnit\Framework\Assert', 'assertArraySubset') === false) {
44+
// PHPUnit >= 9.0.0.
45+
require_once __DIR__ . '/src/Assert.php';
46+
47+
return true;
48+
}
49+
50+
// PHPUnit < 9.0.0.
51+
require_once __DIR__ . '/src/AssertFallThrough.php';
52+
53+
return true;
54+
55+
/*
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.
58+
*/
59+
default:
60+
if (\method_exists('\PHPUnit\Framework\Assert', 'assertArraySubset') === true) {
61+
// PHPUnit < 9.0.0.
62+
throw new \RuntimeException(
63+
\sprintf(
64+
'Using class "%s" is only supported in combination with PHPUnit >= 9.0.0',
65+
$className
66+
)
67+
);
68+
}
69+
70+
// PHPUnit >= 9.0.0.
71+
$file = \realpath(
72+
__DIR__ . \DIRECTORY_SEPARATOR
73+
. 'src' . \DIRECTORY_SEPARATOR
74+
. \strtr(\substr($className, 33), '\\', \DIRECTORY_SEPARATOR) . '.php'
75+
);
76+
77+
if (\file_exists($file) === true) {
78+
require_once $file;
79+
80+
return true;
81+
}
82+
}
83+
84+
return false;
85+
}
86+
}
87+
88+
\spl_autoload_register(__NAMESPACE__ . '\Autoload::load');
89+
}

composer.json

+3-5
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

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpcs.xml.dist

+30-1
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,34 @@
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+
<exclude-pattern>tests/bootstrap\.php</exclude-pattern>
33+
<exclude-pattern>tests/Availability/*\.php</exclude-pattern>
34+
</rule>
35+
<rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName">
36+
<exclude-pattern>assertarraysubset-autoload\.php</exclude-pattern>
37+
<exclude-pattern>tests/Availability/*\.php</exclude-pattern>
38+
</rule>
39+
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint">
40+
<exclude-pattern>assertarraysubset-autoload\.php</exclude-pattern>
41+
<exclude-pattern>src/AssertFallThrough\.php</exclude-pattern>
42+
</rule>
43+
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint">
44+
<exclude-pattern>assertarraysubset-autoload\.php</exclude-pattern>
45+
<exclude-pattern>src/AssertFallThrough\.php</exclude-pattern>
46+
<exclude-pattern>tests/Availability/*\.php</exclude-pattern>
47+
</rule>
48+
2049
</ruleset>

phpunit.xml.dist

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
beStrictAboutOutputDuringTests="true"
88
beStrictAboutTodoAnnotatedTests="true"
99
beStrictAboutChangesToGlobalState="true"
10-
bootstrap="vendor/autoload.php"
10+
bootstrap="tests/bootstrap.php"
1111
>
1212
<testsuites>
1313
<testsuite name="unit">
14-
<directory>tests</directory>
14+
<directory>tests/Availability</directory>
15+
<directory phpVersion="7.3.0" phpVersionOperator=">=">tests/Unit</directory>
1516
</testsuite>
1617
</testsuites>
1718

src/ArraySubsetAssertsEmpty.php

+15
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

+33
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace DMS\PHPUnitExtensions\ArraySubset\Tests\Availibility;
4+
5+
use DMS\PHPUnitExtensions\ArraySubset\Constraint\ArraySubset;
6+
use PHPUnit\Framework\TestCase;
7+
8+
/**
9+
* Testing that autoloading classes which should only ever be loaded on PHPUnit >= 9 will
10+
* generate an exception when attempted on PHPUnit < 9..
11+
*
12+
* Note: the autoloading in combination with PHPUnit 9+ is automatically tested via the
13+
* actual tests for the polyfill as the class will be called upon.
14+
*
15+
* {@internal The code in this file must be PHP cross-version compatible for PHP 5.4 - current!}
16+
*
17+
* @requires PHPUnit < 9
18+
*/
19+
final class AutoloadExceptionTest extends TestCase
20+
{
21+
public function testAutoloadException()
22+
{
23+
$expection = '\RuntimeException';
24+
$message = 'Using class "DMS\PHPUnitExtensions\ArraySubset\Constraint\ArraySubset" is only supported in combination with PHPUnit >= 9.0.0';
25+
26+
if (\method_exists('\PHPUnit\Framework\TestCase', 'expectException') === true) {
27+
$this->expectException($expection);
28+
$this->expectExceptionMessage($message);
29+
} else {
30+
$this->setExpectedException($expection, $message);
31+
}
32+
33+
new ArraySubset();
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace DMS\PHPUnitExtensions\ArraySubset\Tests\Availibility;
4+
5+
use DMS\PHPUnitExtensions\ArraySubset\Assert;
6+
use PHPUnit\Framework\TestCase;
7+
8+
/**
9+
* Testing that the autoloading of the fall-through `Assert` class for PHPUnit 4.x - 8.x works correctly.
10+
*
11+
* {@internal The code in this file must be PHP cross-version compatible for PHP 5.4 - current!}
12+
*/
13+
final class AvailibilityViaClassTest extends TestCase
14+
{
15+
public function testAssertArraySubsetisAvailabe()
16+
{
17+
Assert::assertArraySubset([1, 2], [1, 2, 3]);
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace DMS\PHPUnitExtensions\ArraySubset\Tests\Availibility;
4+
5+
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
6+
use PHPUnit\Framework\TestCase;
7+
8+
/**
9+
* Testing that the autoloading of the empty `ArraySubsetAsserts` trait for PHPUnit 4.x - 8.x works correctly.
10+
*
11+
* {@internal The code in this file must be PHP cross-version compatible for PHP 5.4 - current!}
12+
*/
13+
final class AvailibilityViaTraitTest extends TestCase
14+
{
15+
use ArraySubsetAsserts;
16+
17+
public function testAssertArraySubsetisAvailabe()
18+
{
19+
static::assertArraySubset([1, 2], [1, 2, 3]);
20+
$this->assertArraySubset([1, 2], [1, 2, 3]);
21+
}
22+
}

tests/Unit/AssertTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use PHPUnit\Framework\ExpectationFailedException;
99
use PHPUnit\Framework\TestCase;
1010

11+
/**
12+
* @requires PHPUnit >= 9
13+
*/
1114
final class AssertTest extends TestCase
1215
{
1316
public function testAssertArraySubsetPassesStrictConfig(): void

tests/Unit/Constraint/ArraySubsetTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
use function sprintf;
1818

19+
/**
20+
* @requires PHPUnit >= 9
21+
*/
1922
final class ArraySubsetTest extends TestCase
2023
{
2124
/**

0 commit comments

Comments
 (0)