Skip to content

Commit 1d10fa5

Browse files
committed
Add availability tests for the fall-through functionality
... and run the tests against all PHP versions on which the package can be installed. This commit: * Adds a test `bootstrap.php` file which allows for running the tests either via PHPUnit phar files or via a Composer installed version. * Adds two additional test classes which make sure that the `assertArraySubset()` functionality can be safely used and falls through to the PHPUnit native functionality for PHPUnit < 9. * Prevents the _real_ unit tests from loading on PHP < 7.3 via an annotation in the `phpunit.xml.dist` file. If those files would be loaded on older PHP versions, the test run would break on the parse errors due to modern PHP syntax being used. * Prevents the _real_ unit tests from running on PHPUnit < 9. These tests only need to be run on PHPUnit >= 9 as that is the only time when the polyfills will actually be used. * Makes a few select exceptions to the coding standards for the new test files for CS rules which require a higher minimum PHP version. - `strict_types` is only available as of PHP 7.0. - The `void` return type is only available as of PHP 7.1.
1 parent eaf0534 commit 1d10fa5

File tree

7 files changed

+70
-2
lines changed

7 files changed

+70
-2
lines changed

phpcs.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
<exclude-pattern>assertarraysubset-autoload\.php</exclude-pattern>
3030
<exclude-pattern>src/ArraySubsetAssertsEmpty\.php</exclude-pattern>
3131
<exclude-pattern>src/AssertFallThrough\.php</exclude-pattern>
32+
<exclude-pattern>tests/bootstrap\.php</exclude-pattern>
33+
<exclude-pattern>tests/Availability/*\.php</exclude-pattern>
3234
</rule>
3335
<rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName">
3436
<exclude-pattern>assertarraysubset-autoload\.php</exclude-pattern>
@@ -40,6 +42,7 @@
4042
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint">
4143
<exclude-pattern>assertarraysubset-autoload\.php</exclude-pattern>
4244
<exclude-pattern>src/AssertFallThrough\.php</exclude-pattern>
45+
<exclude-pattern>tests/Availability/*\.php</exclude-pattern>
4346
</rule>
4447

4548
</ruleset>

phpunit.xml.dist

Lines changed: 3 additions & 2 deletions
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

Lines changed: 19 additions & 0 deletions
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+
}
Lines changed: 22 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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
/**

tests/bootstrap.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* PHPUnit bootstrap file.
4+
*
5+
* {@internal The code in this file must be PHP cross-version compatible for PHP 5.4 - current!}
6+
*/
7+
8+
if (defined('__PHPUNIT_PHAR__')) {
9+
// Testing via a PHPUnit phar.
10+
require_once __DIR__ . '/../assertarraysubset-autoload.php';
11+
} elseif (file_exists(__DIR__ . '/../vendor/autoload.php')) {
12+
// Testing via a Composer install.
13+
require_once __DIR__ . '/../vendor/autoload.php';
14+
} else {
15+
echo 'Run "composer install" before attempting to run the tests.';
16+
exit(1);
17+
}

0 commit comments

Comments
 (0)