diff --git a/composer.json b/composer.json index 35d41d3a80e8829d72ea4c9147ca27a99b6bcab7..96032e450dca6340783b5d6d301712fc7f41a616 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,12 @@ }, "files": ["lib/_autoload_modules.php"] }, + "autoload-dev": { + "psr-4": { + "SimpleSAML\\Test\\": "tests" + }, + "files": ["tests/_autoload_modules.php"] + }, "require": { "php": ">=5.5", "ext-SPL": "*", diff --git a/tests/_autoload_modules.php b/tests/_autoload_modules.php new file mode 100644 index 0000000000000000000000000000000000000000..cbe6c9fc049fd51536008de2e94066d5688cb325 --- /dev/null +++ b/tests/_autoload_modules.php @@ -0,0 +1,42 @@ +<?php +/** + * This file registers an autoloader for test classes used by SimpleSAMLphp modules unit tests. + */ + +/** + * Autoload function for SimpleSAMLphp modules test classes following PSR-4. + * Module test classes have namespaces like SimpleSAML\Test\Module\<moduleName>\Auth\Process + * + * @param string $className Name of the class. + */ +function sspmodTestClassAutoloadPSR4($className) +{ + $elements = explode('\\', $className); + if ($elements[0] === '') { + // class name starting with /, ignore + array_shift($elements); + } + if (count($elements) < 5) { + return; // it can't be a module test class + } + if (array_shift($elements) !== 'SimpleSAML') { + return; // the first element is not "SimpleSAML" + } + if (array_shift($elements) !== 'Test') { + return; // the second element is not "test" + } + if (array_shift($elements) !== 'Module') { + return; // the second element is not "module" + } + + // this is a SimpleSAMLphp module test class following PSR-4 + $module = array_shift($elements); + $moduleTestDir = __DIR__ .'/modules/'.$module; + $file = $moduleTestDir .'/lib/'.implode('/', $elements).'.php'; + + if (file_exists($file)) { + require_once($file); + } +} + +spl_autoload_register('sspmodTestClassAutoloadPSR4');