Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e609974

Browse files
committed
Autoloader: add fall-back to PSR4 autoloading
This fallback will only allow loading additional files on PHPUnit >= 9.x and will throw an exception otherwise. Includes dedicated test for the autoload exception.
1 parent 0478c56 commit e609974

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

‎assertarraysubset-autoload.php‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ public static function load($className)
3131
// PHPUnit >= 9.0.0.
3232
require_once __DIR__ . '/src/ArraySubsetAsserts.php';
3333

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-
3934
return true;
4035
}
4136

@@ -56,6 +51,34 @@ public static function load($className)
5651
require_once __DIR__ . '/src/AssertFallThrough.php';
5752

5853
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+
}
5982
}
6083

6184
return false;

‎phpcs.xml.dist‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
</rule>
3535
<rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName">
3636
<exclude-pattern>assertarraysubset-autoload\.php</exclude-pattern>
37+
<exclude-pattern>tests/Availability/*\.php</exclude-pattern>
3738
</rule>
3839
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint">
3940
<exclude-pattern>assertarraysubset-autoload\.php</exclude-pattern>
Lines changed: 35 additions & 0 deletions
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+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /