Skip to content

Commit

Permalink
Unit test command and refactor service provider
Browse files Browse the repository at this point in the history
  • Loading branch information
norbybaru committed Jan 25, 2024
1 parent 38701a4 commit 1c6bb71
Show file tree
Hide file tree
Showing 22 changed files with 413 additions and 76 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ Run the following command from your projects root
```php
composer require norbybaru/modularize
```

For Laravel versions lower than 5.5, this step is important after running above script.
- Open your `config/app.php` file and add custom service provider:
```php
NorbyBaru\Modularize\ModularizeServiceProvider::class
## Config
Publish configuration
```bash
php artisan vendor:publish --provider="NorbyBaru\Modularize\ModularizeServiceProvider" --tag="modularize-config"
```

## Usage
Expand Down
20 changes: 20 additions & 0 deletions config/modularize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

return [

/**
* By enabling modular config, the package will autoload all modules found in the root_path directory
*/
'enable' => true,

/**
* Define application root directory folder to create modules files
*/
'root_path' => 'modules',

/**
* Routes created under the Routes/ directory of a module would be autoload to be discovered by the application.
* Setting 'autoload_routes => false' will require manually registering module routes through a service provider.
*/
'autoload_routes' => true,
];
7 changes: 0 additions & 7 deletions config/module.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Console/Commands/ModuleMakeControllerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function handle()
$type = '';
}

$name = $this->qualifyClass('Modules\\'.$module.'\\'.$folder.'\\'.$filename);
$name = $this->qualifyClass($module.'\\'.$folder.'\\'.$filename);

if ($this->files->exists($path = $this->getPath($name))) {
$this->logFileExist($name);
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/ModuleMakeEventCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function handle()
$filename = Str::studly($this->getNameInput());
$folder = $this->getFolderPath();

$name = $this->qualifyClass('Modules\\'.$module.'\\'.$folder.'\\'.$filename);
$name = $this->qualifyClass($module.'\\'.$folder.'\\'.$filename);

if (! $path = $this->getFilePath($name)) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/ModuleMakeJobCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function handle()
$filename = Str::studly($this->getNameInput());
$folder = $this->getFolderPath();

$name = $this->qualifyClass('Modules\\'.$module.'\\'.$folder.'\\'.$filename);
$name = $this->qualifyClass($module.'\\'.$folder.'\\'.$filename);

if (! $path = $this->getFilePath($name)) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/ModuleMakeListenerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function handle()

if ($event = $this->option('event')) {
$type = 'event.';
$event = $this->qualifyClass('Modules\\'.$module.'\\'.'Events'.'\\'.$event);
$event = $this->qualifyClass($module.'\\'.'Events'.'\\'.$event);
}

if ($this->option('queued')) {
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/ModuleMakeMiddlewareCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function handle()
$filename = Str::studly($this->getNameInput());
$folder = $this->getFolderPath();

$name = $this->qualifyClass('Modules\\'.$module.'\\'.$folder.'\\'.$filename);
$name = $this->qualifyClass($module.'\\'.$folder.'\\'.$filename);

if ($this->files->exists($path = $this->getPath($name))) {
$this->logFileExist($name);
Expand Down
11 changes: 4 additions & 7 deletions src/Console/Commands/ModuleMakeMigrationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace NorbyBaru\Modularize\Console\Commands;

use Illuminate\Console\GeneratorCommand;
use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class ModuleMakeMigrationCommand extends GeneratorCommand
class ModuleMakeMigrationCommand extends ModuleMakerCommand
{
/**
* The name and signature of the console command.
Expand Down Expand Up @@ -50,7 +49,7 @@ public function handle()
$create = $this->option('create');
$update = $this->option('table');

$path = $this->qualifyClass('Modules\\'.$module.'\\'.$this->folder);
$path = $this->qualifyClass($module.'\\'.$this->folder);
$path = $this->classPath($path);

$arguments = [
Expand All @@ -71,11 +70,9 @@ public function handle()

}

private function getPluralName(string $name): string
protected function getFolderPath(): string
{
return Str::of($name)
->plural()
->snake();
return 'Migrations';
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Console/Commands/ModuleMakeModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function handle()
$type = 'pivot.';
}

$name = $this->qualifyClass('Modules\\'.$module.'\\'.$folder.'\\'.$filename);
$name = $this->qualifyClass($module.'\\'.$folder.'\\'.$filename);

if ($this->files->exists($path = $this->getPath($name))) {
$this->logFileExist($name);
Expand All @@ -64,6 +64,8 @@ public function handle()
$this->makeDirectory($path);
$this->files->put($path, $this->buildClass($name));

$this->logFileCreated($name);

if ($this->option('migration')) {
$this->makeMigration(name: $filename, module: $module);
}
Expand All @@ -76,8 +78,6 @@ public function handle()
$this->makePolicy(name: $filename, module: $module);
}

$this->logFileCreated($name);

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/ModuleMakeNotificationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function handle()
$filename = Str::studly($this->getNameInput());
$folder = $this->getFolderPath();

$name = $this->qualifyClass('Modules\\'.$module.'\\'.$folder.'\\'.$filename);
$name = $this->qualifyClass($module.'\\'.$folder.'\\'.$filename);

if ($this->files->exists($path = $this->getPath($name))) {
$this->logFileExist($name);
Expand Down
4 changes: 2 additions & 2 deletions src/Console/Commands/ModuleMakePolicyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public function handle()

if ($model = $this->option('model')) {
$type = 'model.';
$model = $this->qualifyClass('Modules\\'.$module.'\\'.'Models'.'\\'.$model);
$model = $this->qualifyClass($module.'\\'.'Models'.'\\'.$model);
}

$name = $this->qualifyClass('Modules\\'.$module.'\\'.$folder.'\\'.$filename);
$name = $this->qualifyClass($module.'\\'.$folder.'\\'.$filename);

if ($this->files->exists($path = $this->getPath($name))) {
$this->logFileExist($name);
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/ModuleMakeProviderCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function handle()
$filename = Str::studly($this->getNameInput());
$folder = $this->getFolderPath();

$name = $this->qualifyClass('Modules\\'.$module.'\\'.$folder.'\\'.$filename);
$name = $this->qualifyClass($module.'\\'.$folder.'\\'.$filename);

if ($this->files->exists($path = $this->getPath($name))) {
$this->logFileExist($name);
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/ModuleMakeRequestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function handle()
$filename = Str::studly($this->getNameInput());
$folder = $this->getFolderPath();

$name = $this->qualifyClass('Modules\\'.$module.'\\'.$folder.'\\'.$filename);
$name = $this->qualifyClass($module.'\\'.$folder.'\\'.$filename);

if ($this->files->exists($path = $this->getPath($name))) {
$this->logFileExist($name);
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/ModuleMakeResourceCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function handle()
$filename = Str::studly($this->getNameInput());
$folder = $this->getFolderPath();

$name = $this->qualifyClass('Modules\\'.$module.'\\'.$folder.'\\'.$filename);
$name = $this->qualifyClass($module.'\\'.$folder.'\\'.$filename);

if ($this->files->exists($path = $this->getPath($name))) {
$this->logFileExist($name);
Expand Down
165 changes: 165 additions & 0 deletions src/Console/Commands/ModuleMakeTestCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

namespace NorbyBaru\Modularize\Console\Commands;

use DOMDocument;
use Illuminate\Support\Str;

class ModuleMakeTestCommand extends ModuleMakerCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'module:make:test
{name : The name of the test}
{--module= : Name of module migration should belong to}
{--u|unit : Create a unit test}
{--p|pest : Create a Pest test}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate resource for a module';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Test';

public function handle()
{
$module = $this->getModuleInput();
$filename = Str::studly($this->getNameInput());
$folder = $this->getFolderPath();

$testType = 'Feature';
$prefix = 'test';
$type = '';
if ($this->option('unit')) {
$type = 'unit.';
$testType = 'Unit';
}

$name = $this->qualifyClass($module.'\\'.$folder.'\\'.$testType.'\\'.$filename);

if ($this->files->exists($path = $this->getPath($name))) {
$this->logFileExist($name);

return true;
}

if ($this->option('pest')) {
$prefix = 'pest';
}

$this->setStubFile("{$prefix}.{$type}");
$this->makeDirectory($path);

$this->files->put($path, $this->buildClass($name));

$this->logFileCreated($name);

$this->updatePhpUnitXmlFile();
}

protected function getFolderPath(): string
{
return 'Tests';
}

/**
* Update phpunit.xml file with Module test directories
*/
protected function updatePhpUnitXmlFile()
{
$path = base_path('phpunit.xml.dist');

if (! $this->files->exists($path)) {
$path = base_path('phpunit.xml');

if (! $this->files->exists($path)) {
return;
}
}

/** @var \SimpleXMLElement */
$xml = simplexml_load_file($path);
$dom = $this->createDomDocument($xml);
$rootDirectory = $this->getModuleRootDirectory();
$updateDocument = false;

// Specify the testsuite name and attribute to check
$testSuiteName = 'Module Feature';
$testSuiteDirectory = "./{$rootDirectory}/**/Tests/Feature";
$testSuite = $xml->xpath("//testsuite[@name='{$testSuiteName}']");

// Check if the attribute already exists for the child element
if (! $testSuite || (string) $testSuite[0]->directory != $testSuiteDirectory) {
$dom = $this->updateDocument($dom, $testSuiteName, $testSuiteDirectory, $path);
$updateDocument = true;
}

$testSuiteName = 'Module Unit';
$testSuiteDirectory = "./{$rootDirectory}/**/Tests/Unit";
$testSuite = $xml->xpath("//testsuite[@name='{$testSuiteName}']");

if (! $testSuite || (string) $testSuite[0]->directory != $testSuiteDirectory) {
$dom = $this->updateDocument($dom, $testSuiteName, $testSuiteDirectory, $path);
$updateDocument = true;
}

if ($updateDocument) {
$this->saveDomDocument($dom, $path);
$this->components->info(sprintf('[%s] updated successfully.', $path));
}
}

private function createDomDocument(\SimpleXMLElement $xml): DOMDocument
{
// Create a new DOMDocument
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;

// Import the SimpleXML object into the DOMDocument
$dom->loadXML($xml->asXML());

return $dom;
}

/**
* Save the modified DOMDocument back to the phpunit.xml file
*/
private function saveDomDocument(DOMDocument $dom, string $path): void
{
$dom->save($path);
}

private function updateDocument(DOMDocument $dom, string $testSuiteName, string $testSuiteDirectory, string $path): DOMDocument
{
// Get the root element (<testsuites>)
$testSuites = $dom->getElementsByTagName('testsuites')->item(0);
// Create a new <testsuite> element
$newTestSuite = $dom->createElement('testsuite');
$newTestSuite->setAttribute('name', $testSuiteName);

// Create a new <directory> element inside <testsuite>
$newDirectory = $dom->createElement('directory', $testSuiteDirectory);
$domAttribute = $dom->createAttribute('suffix');
$domAttribute->value = 'Test.php';

$newDirectory->appendChild($domAttribute);
$newTestSuite->appendChild($newDirectory);

// Append the new <testsuite> to <testsuites>
$testSuites->appendChild($newTestSuite);

return $dom;
}
}
Loading

0 comments on commit 1c6bb71

Please sign in to comment.