From 48a4bcf3e9919505331cd7832ce9e0def156e889 Mon Sep 17 00:00:00 2001
From: Patrick Radtke <patrick@cirrusidentity.com>
Date: Sat, 30 Mar 2019 15:53:08 -0600
Subject: [PATCH] Add dev autoloader for module test classes

---
 composer.json               |  6 ++++++
 tests/_autoload_modules.php | 42 +++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)
 create mode 100644 tests/_autoload_modules.php

diff --git a/composer.json b/composer.json
index 35d41d3a8..96032e450 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 000000000..cbe6c9fc0
--- /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');
-- 
GitLab