-
-
Notifications
You must be signed in to change notification settings - Fork 970
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1954 from nWidart/add-generate-code-coverage-to-p…
…hpunit-support added command to add enabled modules to code coverage includes
- Loading branch information
Showing
4 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |