Skip to content

Commit

Permalink
added command to add enabled modules to code coverage includes
Browse files Browse the repository at this point in the history
  • Loading branch information
dcblogdev committed Sep 22, 2024
1 parent d275a5b commit 7eda3d2
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 0 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
],
"require": {
"php": ">=8.2",
"ext-dom": "*",
"ext-json": "*",
"ext-simplexml": "*",
"wikimedia/composer-merge-plugin": "^2.1"
},
"require-dev": {
Expand Down
90 changes: 90 additions & 0 deletions src/Commands/UpdatePhpunitCoverage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Nwidart\Modules\Commands;

use DOMDocument;
use Illuminate\Console\Command;

class UpdatePhpunitCoverage extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'module:update-phpunit-coverage';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Update phpunit.xml source/include path with enabled modules';

/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$appFolder = config('modules.app_folder', 'app/');
$appFolder = rtrim($appFolder, '/') . '/';
$phpunitXmlPath = base_path('phpunit.xml');
$modulesStatusPath = base_path('modules_statuses.json');

if (!file_exists($phpunitXmlPath)) {
$this->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;
}
}
1 change: 1 addition & 0 deletions src/Providers/ConsoleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
Expand Down
87 changes: 87 additions & 0 deletions tests/Commands/UpdatePhpunitCoverageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Commands;

use Nwidart\Modules\Tests\BaseTestCase;

class UpdatePhpunitCoverageTest extends BaseTestCase
{
public function setUp(): void
{
parent::setUp();

if (file_exists(base_path('modules_statuses.json'))) {
unlink(base_path('modules_statuses.json'));
}

if (file_exists(base_path('phpunit.xml'))) {
unlink(base_path('phpunit.xml'));
}
}

public function test_no_phpunit_file()
{
$code = $this->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'
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Modules">
<directory suffix="Test.php">./Modules/*/Tests/Feature</directory>
<directory suffix="Test.php">./Modules/*/Tests/Unit</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>./app</directory>
</include>
</source>
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="MAIL_MAILER" value="array"/>
<env name="PULSE_ENABLED" value="false"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="TELESCOPE_ENABLED" value="false"/>
</php>
</phpunit>
XML;

file_put_contents(base_path('phpunit.xml'), $phpunit);
}

private function MakeModulesStatuses($value)
{
$modulesStatusPath = base_path('modules_statuses.json');
file_put_contents($modulesStatusPath, $value);
}
}

0 comments on commit 7eda3d2

Please sign in to comment.