From ebcb2b6204f71deae1486b38fa08351a4d42752e Mon Sep 17 00:00:00 2001 From: Chris Morrell Date: Wed, 6 Jul 2022 14:20:49 -0400 Subject: [PATCH] Show an invalid option error when module does not exist --- CHANGELOG.md | 1 + src/Console/Commands/Modularize.php | 13 +++++++++---- tests/Commands/Make/MakeControllerTest.php | 9 +++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8316ca6..66d7f45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Console/Commands/Modularize.php b/src/Console/Commands/Modularize.php index 92ae860..713623a 100644 --- a/src/Console/Commands/Modularize.php +++ b/src/Console/Commands/Modularize.php @@ -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; diff --git a/tests/Commands/Make/MakeControllerTest.php b/tests/Commands/Make/MakeControllerTest.php index 41356ef..496633e 100644 --- a/tests/Commands/Make/MakeControllerTest.php +++ b/tests/Commands/Make/MakeControllerTest.php @@ -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 { @@ -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;