diff --git a/composer.json b/composer.json index 2bce92201..b33e0a0ab 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,9 @@ ], "require": { "php": ">=8.2", + "ext-dom": "*", "ext-json": "*", + "ext-simplexml": "*", "wikimedia/composer-merge-plugin": "^2.1" }, "require-dev": { diff --git a/src/Commands/UpdatePhpunitCoverage.php b/src/Commands/UpdatePhpunitCoverage.php new file mode 100644 index 000000000..79d0cbf2f --- /dev/null +++ b/src/Commands/UpdatePhpunitCoverage.php @@ -0,0 +1,90 @@ +error("phpunit.xml file not found: {$phpunitXmlPath}"); + return 100; + } + + if (!file_exists($modulesStatusPath)) { + $this->error("Modules statuses file not found: {$modulesStatusPath}"); + return 99; + } + + $enabledModules = json_decode(file_get_contents($modulesStatusPath), true); + + if (json_last_error() !== JSON_ERROR_NONE) { + $this->error("Error decoding JSON from {$modulesStatusPath}: " . json_last_error_msg()); + return 98; + } + + $modulesPath = base_path('Modules/'); + $moduleDirs = []; + + foreach ($enabledModules as $module => $status) { + if ($status) { // Only add enabled modules + $moduleDir = $modulesPath . $module . '/' . $appFolder; + if (is_dir($moduleDir)) { + $moduleDirs[] = $moduleDir; + } + } + } + + + + $phpunitXml = simplexml_load_file($phpunitXmlPath); + + $sourceInclude = $phpunitXml->xpath('//source/include')[0]; + + unset($sourceInclude->directory); + + $sourceInclude->addChild('directory', './app'); + + foreach ($moduleDirs as $dir) { + $directory = $sourceInclude->addChild('directory', str_replace(base_path(), '.', $dir)); + $directory->addAttribute('suffix', '.php'); + } + + $dom = new DOMDocument(); + $dom->preserveWhiteSpace = false; + $dom->formatOutput = true; + $dom->loadXML($phpunitXml->asXML()); + $dom->save($phpunitXmlPath); + + $this->info("phpunit.xml updated with enabled module directories."); + + return 0; + } +} diff --git a/src/Providers/ConsoleServiceProvider.php b/src/Providers/ConsoleServiceProvider.php index eb39dbec9..1417e532d 100644 --- a/src/Providers/ConsoleServiceProvider.php +++ b/src/Providers/ConsoleServiceProvider.php @@ -97,6 +97,7 @@ public static function defaultCommands(): Collection Commands\ModuleDiscoverCommand::class, Commands\ModuleClearCompiledCommand::class, Commands\SetupCommand::class, + Commands\UpdatePhpunitCoverage::class, Commands\Database\MigrateFreshCommand::class, ]); diff --git a/tests/Commands/UpdatePhpunitCoverageTest.php b/tests/Commands/UpdatePhpunitCoverageTest.php new file mode 100644 index 000000000..1cff158a7 --- /dev/null +++ b/tests/Commands/UpdatePhpunitCoverageTest.php @@ -0,0 +1,87 @@ +artisan('module:update-phpunit-coverage'); + + $this->assertSame(100, $code); + } + + public function test_no_modules_statuses_file() + { + $this->makePhpunit(); + + $code = $this->artisan('module:update-phpunit-coverage'); + + $this->assertSame(99, $code); + } + + public function test_modules_statuses_file_is_not_json() + { + $this->makePhpunit(); + $this->makeModulesStatuses('not json'); + + $code = $this->artisan('module:update-phpunit-coverage'); + + $this->assertSame(98, $code); + } + + private function MakePhpunit() + { + $phpunit = <<<'XML' + + + + + ./Modules/*/Tests/Feature + ./Modules/*/Tests/Unit + + + + + ./app + + + + + + + + + + + + + + + +XML; + + file_put_contents(base_path('phpunit.xml'), $phpunit); + } + + private function MakeModulesStatuses($value) + { + $modulesStatusPath = base_path('modules_statuses.json'); + file_put_contents($modulesStatusPath, $value); + } +}