Skip to content

Commit

Permalink
Tests: finish off namespacing
Browse files Browse the repository at this point in the history
Now all test classes are namespaced, the old autoloader in the test bootstrap is no longer necessary.

Additionally, as long as Composer knows about the namespaced test classes. the (new) custom autoloader is only really needed when PHPUnit is run via a PHAR file.

To this end, this commit:
* Adds a `autoload-dev` section to the `composer.json` configuration file.
* Removes the old autoloader.
* And places the "manual" loading of the PHPUnit polyfills autoloader and the custom autoloader in a condition, so they only get used when the tests are run via a PHAR file.
  • Loading branch information
jrfnl committed Jun 29, 2021
1 parent d6a2232 commit bb565e9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
"Requests": "library/"
}
},
"autoload-dev": {
"psr-4": {
"Requests\\Tests\\": "tests/"
}
},
"scripts": {
"lint": [
"@php ./vendor/php-parallel-lint/php-parallel-lint/parallel-lint . -e php --exclude vendor --exclude .git"
Expand Down
58 changes: 27 additions & 31 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,44 +36,40 @@ function define_from_env($name, $default = false) {
die(1);
}

// Load the PHPUnit Polyfills autoloader.
require_once $vendor_dir . '/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php';
if (defined('__PHPUNIT_PHAR__')) {
// Testing via a PHPUnit phar.

// New autoloader.
spl_autoload_register(
function ($class_name) {
// Only try & load our own classes.
if (stripos($class_name, 'Requests\\Tests\\') !== 0) {
return false;
}
// Load the PHPUnit Polyfills autoloader.
require_once $vendor_dir . '/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php';

// Strip namespace prefix 'Requests\Tests\'.
$relative_class = substr($class_name, 15);
$file = realpath(__DIR__ . '/' . strtr($relative_class, '\\', '/') . '.php');
/*
* Autoloader specifically for the test files.
* Fixes issues with PHPUnit not being able to find test classes being extended when running
* in a non-Composer context.
*/
spl_autoload_register(
function ($class_name) {
// Only try & load our own classes.
if (stripos($class_name, 'Requests\\Tests\\') !== 0) {
return false;
}

if (file_exists($file)) {
include_once $file;
}
// Strip namespace prefix 'Requests\Tests\'.
$relative_class = substr($class_name, 15);
$file = realpath(__DIR__ . '/' . strtr($relative_class, '\\', '/') . '.php');

return true;
}
);

// Old autoloader.
function autoload_tests($class) {
if (strpos($class, 'RequestsTest_') !== 0) {
return;
}
if (file_exists($file)) {
include_once $file;
}

$class = substr($class, 13);
$file = str_replace('_', '/', $class);
if (file_exists(__DIR__ . '/' . $file . '.php')) {
require_once __DIR__ . '/' . $file . '.php';
}
return true;
}
);
} else {
// Testing via a Composer setup.
require_once $vendor_dir . '/autoload.php';
}

spl_autoload_register('autoload_tests');

function httpbin($suffix = '', $ssl = false) {
$host = $ssl ? 'https://' . REQUESTS_TEST_HOST_HTTPS : 'http://' . REQUESTS_TEST_HOST_HTTP;
return rtrim($host, '/') . '/' . ltrim($suffix, '/');
Expand Down

0 comments on commit bb565e9

Please sign in to comment.