Skip to content

Commit

Permalink
Show an invalid option error when module does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
inxilpro committed Jul 6, 2022
1 parent c389359 commit ebcb2b6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Addressed issue where `make:migration` and `make:livewire` were not loading the custom `--module` option
- Added additional tests for `make:` commands to catch necessary changes quicker in the future
- Passing a `--module` flag for an unknown module now triggers a console error

## [1.8.0] - 2022-06-04

Expand Down
13 changes: 9 additions & 4 deletions src/Console/Commands/Modularize.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@

use InterNACHI\Modular\Support\ModuleConfig;
use InterNACHI\Modular\Support\ModuleRegistry;
use Symfony\Component\Console\Exception\InvalidOptionException;
use Symfony\Component\Console\Input\InputOption;

trait Modularize
{
protected function module(): ?ModuleConfig
{
if ($module = $this->option('module')) {
return $this->getLaravel()
->make(ModuleRegistry::class)
->module($module);
if ($name = $this->option('module')) {
$registry = $this->getLaravel()->make(ModuleRegistry::class);

if ($module = $registry->module($name)) {
return $module;
}

throw new InvalidOptionException(sprintf('The "%s" module does not exist.', $name));
}

return null;
Expand Down
9 changes: 9 additions & 0 deletions tests/Commands/Make/MakeControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use InterNACHI\Modular\Tests\Concerns\TestsMakeCommands;
use InterNACHI\Modular\Tests\Concerns\WritesToAppFilesystem;
use InterNACHI\Modular\Tests\TestCase;
use Symfony\Component\Console\Exception\InvalidOptionException;

class MakeControllerTest extends TestCase
{
Expand All @@ -21,6 +22,14 @@ public function test_it_overrides_the_default_command(): void
->assertExitCode(0);
}

public function test_it_produces_an_error_if_the_module_does_not_exist(): void
{
$this->expectException(InvalidOptionException::class);
$this->expectExceptionMessage('The "does-not-exist" module does not exist.');

$this->artisan('make:controller', ['name' => 'Test', '--module' => 'does-not-exist']);
}

public function test_it_scaffolds_a_controller_in_the_module_when_module_option_is_set(): void
{
$command = MakeController::class;
Expand Down

0 comments on commit ebcb2b6

Please sign in to comment.