From 333160f3db7231c68a9908574911120b899ef8cf Mon Sep 17 00:00:00 2001 From: Kevin Ullyott Date: Mon, 18 May 2026 22:18:18 -0400 Subject: [PATCH 1/3] Make test concerns available in autoload Signed-off-by: Kevin Ullyott --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index f5b92db..afcd119 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,8 @@ }, "autoload": { "psr-4": { - "InterNACHI\\Modular\\": "src/" + "InterNACHI\\Modular\\": "src/", + "InterNACHI\\Modular\\Tests\\Concerns\\": "tests/Concerns/" } }, "autoload-dev": { From 94b0a71e63346eb911ea938818c4ba3098b1753e Mon Sep 17 00:00:00 2001 From: Kevin Ullyott Date: Mon, 18 May 2026 22:21:11 -0400 Subject: [PATCH 2/3] Refactor PreloadsAppModules trait for improved module loading and path management to be shared Signed-off-by: Kevin Ullyott --- tests/Concerns/PreloadsAppModules.php | 57 ++++++++++++++++++--------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/tests/Concerns/PreloadsAppModules.php b/tests/Concerns/PreloadsAppModules.php index a04f453..39ab391 100644 --- a/tests/Concerns/PreloadsAppModules.php +++ b/tests/Concerns/PreloadsAppModules.php @@ -8,37 +8,56 @@ trait PreloadsAppModules { protected static $autoloader_registered = false; - + #[Before] public function prepareTestModule(): void { - $src = __DIR__.'/../testbench-core/app-modules'; + $src = $this->appModulesFixturePath(); $dest = static::applicationBasePath().'/app-modules'; - + $fs = new Filesystem(); $fs->deleteDirectory($dest); $fs->copyDirectory($src, $dest); } - + #[Before] public function prepareModuleAutoloader(): void { - if (! static::$autoloader_registered) { - spl_autoload_register(function($fqcn) { - if (str_starts_with($fqcn, 'Modules\\TestModule\\')) { - $path = str_replace( - ['Modules\\TestModule\\', '\\'], - ['', DIRECTORY_SEPARATOR], - $fqcn - ); - $path = static::applicationBasePath().'/app-modules/test-module/src/'.$path.'.php'; - if (file_exists($path)) { - include_once $path; - } - } - }); + if (static::$autoloader_registered) { + return; } - + + $namespaces = $this->moduleNamespaceMap(); + $basePath = static::applicationBasePath(); + + spl_autoload_register(function($fqcn) use ($namespaces, $basePath) { + foreach ($namespaces as $namespace => $module) { + if (! str_starts_with($fqcn, $namespace)) { + continue; + } + $relative = str_replace( + [$namespace, '\\'], + ['', DIRECTORY_SEPARATOR], + $fqcn + ); + $path = $basePath.'/app-modules/'.$module.'/src/'.$relative.'.php'; + if (file_exists($path)) { + include_once $path; + } + return; + } + }); + static::$autoloader_registered = true; } + + protected function appModulesFixturePath(): string + { + return __DIR__.'/../testbench-core/app-modules'; + } + + protected function moduleNamespaceMap(): array + { + return ['Modules\\TestModule\\' => 'test-module']; + } } From 9e2a47db60bb2c1f8376a866c43b39c13815358c Mon Sep 17 00:00:00 2001 From: Chris Morrell Date: Thu, 21 May 2026 10:41:03 -0400 Subject: [PATCH 3/3] Code style + return bugfix --- tests/Concerns/PreloadsAppModules.php | 34 +++++++++++++-------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/tests/Concerns/PreloadsAppModules.php b/tests/Concerns/PreloadsAppModules.php index 39ab391..86756e4 100644 --- a/tests/Concerns/PreloadsAppModules.php +++ b/tests/Concerns/PreloadsAppModules.php @@ -7,55 +7,53 @@ trait PreloadsAppModules { - protected static $autoloader_registered = false; - + protected static bool $autoloader_registered = false; + #[Before] public function prepareTestModule(): void { $src = $this->appModulesFixturePath(); $dest = static::applicationBasePath().'/app-modules'; - + $fs = new Filesystem(); $fs->deleteDirectory($dest); $fs->copyDirectory($src, $dest); } - + #[Before] public function prepareModuleAutoloader(): void { if (static::$autoloader_registered) { return; } - + $namespaces = $this->moduleNamespaceMap(); - $basePath = static::applicationBasePath(); - - spl_autoload_register(function($fqcn) use ($namespaces, $basePath) { + $base_path = static::applicationBasePath(); + + spl_autoload_register(function($fqcn) use ($namespaces, $base_path) { foreach ($namespaces as $namespace => $module) { if (! str_starts_with($fqcn, $namespace)) { continue; } - $relative = str_replace( - [$namespace, '\\'], - ['', DIRECTORY_SEPARATOR], - $fqcn - ); - $path = $basePath.'/app-modules/'.$module.'/src/'.$relative.'.php'; + + $relative = str_replace([$namespace, '\\'], ['', DIRECTORY_SEPARATOR], $fqcn); + $path = "{$base_path}/app-modules/{$module}/src/{$relative}.php"; + if (file_exists($path)) { include_once $path; + return; } - return; } }); - + static::$autoloader_registered = true; } - + protected function appModulesFixturePath(): string { return __DIR__.'/../testbench-core/app-modules'; } - + protected function moduleNamespaceMap(): array { return ['Modules\\TestModule\\' => 'test-module'];