From dbe51d918c4614f9c5ac9b7b7d3baac2360daf5d Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Wed, 2 Oct 2024 09:00:54 +0800 Subject: [PATCH] [9.x] Add `make:job-middleware` command (#38) * [9.x] Add `make:job-middleware` command Signed-off-by: Mior Muhammad Zaki * wip Signed-off-by: Mior Muhammad Zaki --------- Signed-off-by: Mior Muhammad Zaki --- src/CanvasServiceProvider.php | 6 +- src/Console/JobMiddlewareMakeCommand.php | 69 +++++++++++++++++++ src/LaravelServiceProvider.php | 16 +++++ .../Console/JobMiddlewareMakeCommandTest.php | 38 ++++++++++ 4 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 src/Console/JobMiddlewareMakeCommand.php create mode 100644 tests/Feature/Console/JobMiddlewareMakeCommandTest.php diff --git a/src/CanvasServiceProvider.php b/src/CanvasServiceProvider.php index 1b9418c..d96e531 100644 --- a/src/CanvasServiceProvider.php +++ b/src/CanvasServiceProvider.php @@ -24,8 +24,8 @@ public function register(): void $manager->setDefaultDriver('canvas'); }); - $this->app->singleton('orchestra.canvas', function (Application $app) { - $workingPath = \defined('CANVAS_WORKING_PATH') ? CANVAS_WORKING_PATH : $this->app->basePath(); + $this->app->singleton('orchestra.canvas', static function (Application $app) { + $workingPath = \defined('CANVAS_WORKING_PATH') ? CANVAS_WORKING_PATH : $app->basePath(); $filesystem = $app->make('files'); @@ -39,7 +39,7 @@ public function register(): void 'feature' => 'Tests\TestCase', ]); - $config['namespace'] = rescue(fn () => rtrim($this->app->getNamespace(), '\\'), null, false); + $config['namespace'] = rescue(fn () => rtrim($app->getNamespace(), '\\'), null, false); } return Canvas::preset($config, $workingPath); diff --git a/src/Console/JobMiddlewareMakeCommand.php b/src/Console/JobMiddlewareMakeCommand.php new file mode 100644 index 0000000..7b79d7b --- /dev/null +++ b/src/Console/JobMiddlewareMakeCommand.php @@ -0,0 +1,69 @@ +addGeneratorPresetOptions(); + } + + /** + * Execute the console command. + * + * @return bool|null + * + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException + */ + #[\Override] + public function handle() + { + /** @phpstan-ignore return.type */ + return $this->generateCode() ? self::SUCCESS : self::FAILURE; + } + + /** + * Get the destination class path. + * + * @param string $name + * @return string + */ + #[\Override] + protected function getPath($name) + { + return $this->getPathUsingCanvas($name); + } + + /** + * Get the root namespace for the class. + * + * @return string + */ + #[\Override] + protected function rootNamespace() + { + return $this->rootNamespaceUsingCanvas(); + } +} diff --git a/src/LaravelServiceProvider.php b/src/LaravelServiceProvider.php index 4d04927..ba06801 100644 --- a/src/LaravelServiceProvider.php +++ b/src/LaravelServiceProvider.php @@ -16,6 +16,7 @@ use Illuminate\Foundation\Console\ExceptionMakeCommand; use Illuminate\Foundation\Console\InterfaceMakeCommand; use Illuminate\Foundation\Console\JobMakeCommand; +use Illuminate\Foundation\Console\JobMiddlewareMakeCommand; use Illuminate\Foundation\Console\ListenerMakeCommand; use Illuminate\Foundation\Console\MailMakeCommand; use Illuminate\Foundation\Console\ModelMakeCommand; @@ -59,6 +60,7 @@ public function boot(): void $this->registerExceptionMakeCommand(); $this->registerFactoryMakeCommand(); $this->registerJobMakeCommand(); + $this->registerJobMiddlewareMakeCommand(); $this->registerInterfaceMakeCommand(); $this->registerListenerMakeCommand(); $this->registerMailMakeCommand(); @@ -239,6 +241,18 @@ protected function registerJobMakeCommand(): void ); } + /** + * Register the command. + * + * @return void + */ + protected function registerJobMiddlewareMakeCommand() + { + $this->app->singleton( + JobMiddlewareMakeCommand::class, static fn ($app) => new Console\JobMiddlewareMakeCommand($app['files']) + ); + } + /** * Register the command. */ @@ -528,6 +542,8 @@ public function provides() Console\InterfaceMakeCommand::class, JobMakeCommand::class, Console\JobMakeCommand::class, + JobMiddlewareMakeCommand::class, + Console\JobMiddlewareMakeCommand::class, ListenerMakeCommand::class, Console\ListenerMakeCommand::class, MailMakeCommand::class, diff --git a/tests/Feature/Console/JobMiddlewareMakeCommandTest.php b/tests/Feature/Console/JobMiddlewareMakeCommandTest.php new file mode 100644 index 0000000..67b75ef --- /dev/null +++ b/tests/Feature/Console/JobMiddlewareMakeCommandTest.php @@ -0,0 +1,38 @@ +artisan('make:job-middleware', ['name' => 'Foo']) + ->assertExitCode(0); + + $this->assertFileContains([ + 'namespace App\Jobs\Middleware;', + 'class Foo', + ], 'app/Jobs/Middleware/Foo.php'); + + $this->assertFilenameNotExists('tests/Feature/Jobs/Middleware/FooTest.php'); + } + + #[Test] + public function it_can_generate_job_file_with_tests() + { + $this->artisan('make:job-middleware', ['name' => 'Foo', '--test' => true]) + ->assertExitCode(0); + + $this->assertFilenameExists('app/Jobs/Middleware/Foo.php'); + $this->assertFilenameExists('tests/Feature/Jobs/Middleware/FooTest.php'); + } +}