Skip to content

Commit

Permalink
Merge pull request #1954 from nWidart/add-generate-code-coverage-to-p…
Browse files Browse the repository at this point in the history
…hpunit-support

added command to add enabled modules to code coverage includes
  • Loading branch information
dcblogdev authored Sep 22, 2024
2 parents d275a5b + df22b41 commit 9d015f0
Show file tree
Hide file tree
Showing 4 changed files with 170 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
77 changes: 77 additions & 0 deletions tests/Commands/UpdatePhpunitCoverageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?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);
}

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 9d015f0

Please sign in to comment.