Skip to content

Commit

Permalink
[9.x] Add make:job-middleware command (#38)
Browse files Browse the repository at this point in the history
* [9.x] Add `make:job-middleware` command

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

---------

Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone authored Oct 2, 2024
1 parent 37ffebb commit dbe51d9
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/CanvasServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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);
Expand Down
69 changes: 69 additions & 0 deletions src/Console/JobMiddlewareMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Orchestra\Canvas\Console;

use Orchestra\Canvas\Core\Concerns\CodeGenerator;
use Orchestra\Canvas\Core\Concerns\TestGenerator;
use Orchestra\Canvas\Core\Concerns\UsesGeneratorOverrides;
use Symfony\Component\Console\Attribute\AsCommand;

/**
* @see https://github.com/laravel/framework/blob/11.x/src/Illuminate/Foundation/Console/JobMiddlewareMakeCommand.php
*/
#[AsCommand(name: 'make:job-middleware', description: 'Create a new job middleware class')]
class JobMiddlewareMakeCommand extends \Illuminate\Foundation\Console\JobMiddlewareMakeCommand
{
use CodeGenerator;
use TestGenerator;
use UsesGeneratorOverrides;

/**
* Configures the current command.
*
* @return void
*/
#[\Override]
protected function configure()
{
parent::configure();

$this->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();
}
}
16 changes: 16 additions & 0 deletions src/LaravelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,6 +60,7 @@ public function boot(): void
$this->registerExceptionMakeCommand();
$this->registerFactoryMakeCommand();
$this->registerJobMakeCommand();
$this->registerJobMiddlewareMakeCommand();
$this->registerInterfaceMakeCommand();
$this->registerListenerMakeCommand();
$this->registerMailMakeCommand();
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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,
Expand Down
38 changes: 38 additions & 0 deletions tests/Feature/Console/JobMiddlewareMakeCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Orchestra\Canvas\Tests\Feature\Console;

use Orchestra\Canvas\Tests\Feature\TestCase;
use PHPUnit\Framework\Attributes\Test;

class JobMiddlewareMakeCommandTest extends TestCase
{
protected $files = [
'app/Jobs/Middleware/Foo.php',
'tests/Feature/Jobs/Middleware/FooTest.php',
];

#[Test]
public function it_can_generate_job_file()
{
$this->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');
}
}

0 comments on commit dbe51d9

Please sign in to comment.