-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mior Muhammad Zaki <[email protected]>
- Loading branch information
Showing
6 changed files
with
317 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Orchestra\Canvas\Core\Commands\Generators; | ||
|
||
use Orchestra\Canvas\Core\Commands\Generator; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
|
||
#[AsCommand(name: 'make:class', description: 'Create a new class')] | ||
class Code extends Generator | ||
{ | ||
/** | ||
* The type of class being generated. | ||
*/ | ||
protected string $type = 'Class'; | ||
|
||
/** | ||
* Get the stub file for the generator. | ||
*/ | ||
public function getStubFile(): string | ||
{ | ||
return $this->getStubFileName(); | ||
} | ||
|
||
/** | ||
* Get the stub file name for the generator. | ||
*/ | ||
public function getStubFileName(): string | ||
{ | ||
return __DIR__.'/../../../storage/canvas/code.stub'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace Orchestra\Canvas\Core\Commands\Generators; | ||
|
||
use Illuminate\Support\Str; | ||
use Orchestra\Canvas\Core\Commands\Generator; | ||
use Orchestra\Canvas\Core\GeneratesCommandCode; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Input\InputOption; | ||
|
||
#[AsCommand(name: 'make:generator', description: 'Create a new generator command')] | ||
class ConsoleGenerator extends Generator | ||
{ | ||
/** | ||
* The type of class being generated. | ||
*/ | ||
protected string $type = 'Generator command'; | ||
|
||
/** | ||
* The type of file being generated. | ||
*/ | ||
protected string $fileType = 'command'; | ||
|
||
/** | ||
* Generator processor. | ||
* | ||
* @var class-string<\Orchestra\Canvas\Core\GeneratesCode> | ||
*/ | ||
protected string $processor = GeneratesCommandCode::class; | ||
|
||
/** | ||
* Get the stub file for the generator. | ||
*/ | ||
public function getPublishedStubFileName(): ?string | ||
{ | ||
return null; | ||
} | ||
|
||
/** | ||
* Get the stub file for the generator. | ||
*/ | ||
public function getStubFile(): string | ||
{ | ||
return __DIR__.'/../../../storage/canvas/generator.stub'; | ||
} | ||
|
||
/** | ||
* Get the default namespace for the class. | ||
*/ | ||
public function getDefaultNamespace(string $rootNamespace): string | ||
{ | ||
return $this->preset->config('console.namespace', $rootNamespace.'\Console\Commands'); | ||
} | ||
|
||
/** | ||
* Generator options. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function generatorOptions(): array | ||
{ | ||
/** @var string $command */ | ||
$command = $this->option('command'); | ||
|
||
if (! Str::startsWith($command, 'make:')) { | ||
$command = "make:{$command}"; | ||
} | ||
|
||
return [ | ||
'command' => $command, | ||
'force' => $this->option('force'), | ||
]; | ||
} | ||
|
||
/** | ||
* Get the console command options. | ||
* | ||
* @return array<int, array> | ||
*/ | ||
protected function getOptions() | ||
{ | ||
return [ | ||
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the generator already exists'], | ||
['command', null, InputOption::VALUE_OPTIONAL, 'The terminal command that should be assigned', 'make:name'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace DummyNamespace; | ||
|
||
class DummyClass | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace DummyNamespace; | ||
|
||
use Orchestra\Canvas\Commands\Generator; | ||
use Orchestra\Canvas\Core\GeneratesCode; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
|
||
#[AsCommand(name: 'dummy:command', description: 'Create a new class')] | ||
class DummyClass extends Generator | ||
{ | ||
/** | ||
* The type of class being generated. | ||
* | ||
* @var string | ||
*/ | ||
protected string $type = 'Class'; | ||
|
||
/** | ||
* Generator processor. | ||
* | ||
* @var class-string<\Orchestra\Canvas\Core\GeneratesCode> | ||
*/ | ||
protected string $processor = GeneratesCode::class; | ||
|
||
/** | ||
* Get the stub file name for the generator. | ||
*/ | ||
public function getStubFileName(): string | ||
{ | ||
// Implement path to stub file. | ||
} | ||
|
||
/** | ||
* Get the default namespace for the class. | ||
*/ | ||
public function getDefaultNamespace(string $rootNamespace): string | ||
{ | ||
return $rootNamespace; | ||
} | ||
|
||
/** | ||
* Generator options. | ||
*/ | ||
public function generatorOptions(): array | ||
{ | ||
return [ | ||
'name' => $this->generatorName(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Orchestra\Canvas\Tests\Feature\Commands\Generators; | ||
|
||
use Illuminate\Console\Application as Artisan; | ||
use Orchestra\Canvas\Core\Commands\Generators; | ||
use Orchestra\Canvas\Core\Presets\Laravel; | ||
use Orchestra\Canvas\Core\Testing\TestCase; | ||
use Orchestra\Testbench\Concerns\WithWorkbench; | ||
|
||
class CodeTest extends TestCase | ||
{ | ||
use WithWorkbench; | ||
|
||
protected $files = [ | ||
'app/Value/Foo.php', | ||
]; | ||
|
||
/** @test */ | ||
public function it_can_generate_class_file() | ||
{ | ||
$preset = new Laravel( | ||
['namespace' => 'App', 'generators' => [Generators\Code::class]], $this->app->basePath(), $this->filesystem | ||
); | ||
|
||
$this->instance('orchestra.canvas', $preset); | ||
|
||
Artisan::starting(fn ($artisan) => $preset->addAdditionalCommands($artisan)); | ||
|
||
$this->artisan('make:class', ['name' => 'Value/Foo']) | ||
->assertExitCode(0); | ||
|
||
$this->assertFileContains([ | ||
'namespace App\Value;', | ||
'class Foo', | ||
], 'app/Value/Foo.php'); | ||
} | ||
|
||
/** @test */ | ||
public function it_cant_generate_class_file() | ||
{ | ||
$this->expectException('Symfony\Component\Console\Exception\CommandNotFoundException'); | ||
$this->expectExceptionMessage('The command "make:class" does not exist.'); | ||
|
||
$this->artisan('make:class', ['name' => 'Foo']) | ||
->assertExitCode(0); | ||
|
||
$this->assertFileContains([ | ||
'namespace App\Value;', | ||
'class Foo', | ||
], 'app/Value/Foo.php'); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
tests/Feature/Commands/Generators/ConsoleGeneratorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace Orchestra\Canvas\Tests\Feature\Commands\Generators; | ||
|
||
use Illuminate\Console\Application as Artisan; | ||
use Orchestra\Canvas\Core\Commands\Generators; | ||
use Orchestra\Canvas\Core\Presets\Laravel; | ||
use Orchestra\Canvas\Core\Testing\TestCase; | ||
use Orchestra\Testbench\Concerns\WithWorkbench; | ||
|
||
class ConsoleGeneratorTest extends TestCase | ||
{ | ||
use WithWorkbench; | ||
|
||
protected $files = [ | ||
'app/Console/Commands/FooCommand.php', | ||
]; | ||
|
||
/** @test */ | ||
public function it_can_generate_command_file() | ||
{ | ||
$preset = new Laravel( | ||
['namespace' => 'App', 'generators' => [Generators\ConsoleGenerator::class]], $this->app->basePath(), $this->filesystem | ||
); | ||
|
||
$this->instance('orchestra.canvas', $preset); | ||
|
||
Artisan::starting(fn ($artisan) => $preset->addAdditionalCommands($artisan)); | ||
|
||
$this->artisan('make:generator', ['name' => 'FooCommand']) | ||
->assertExitCode(0); | ||
|
||
$this->assertFileContains([ | ||
'namespace App\Console\Commands;', | ||
'use Orchestra\Canvas\Commands\Generator;', | ||
'use Symfony\Component\Console\Attribute\AsCommand;', | ||
'#[AsCommand(name: \'make:name\', description: \'Create a new class\')]', | ||
'class FooCommand extends Generator', | ||
], 'app/Console/Commands/FooCommand.php'); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_generate_command_file_with_command_name() | ||
{ | ||
$preset = new Laravel( | ||
['namespace' => 'App', 'generators' => [Generators\ConsoleGenerator::class]], $this->app->basePath(), $this->filesystem | ||
); | ||
|
||
$this->instance('orchestra.canvas', $preset); | ||
|
||
Artisan::starting(fn ($artisan) => $preset->addAdditionalCommands($artisan)); | ||
|
||
$this->artisan('make:generator', ['name' => 'FooCommand', '--command' => 'make:foobar']) | ||
->assertExitCode(0); | ||
|
||
$this->assertFileContains([ | ||
'namespace App\Console\Commands;', | ||
'use Orchestra\Canvas\Commands\Generator;', | ||
'use Symfony\Component\Console\Attribute\AsCommand;', | ||
'#[AsCommand(name: \'make:foobar\', description: \'Create a new class\')]', | ||
'class FooCommand extends Generator', | ||
], 'app/Console/Commands/FooCommand.php'); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_generate_command_file_with_command_name_without_make_prefix() | ||
{ | ||
$preset = new Laravel( | ||
['namespace' => 'App', 'generators' => [Generators\ConsoleGenerator::class]], $this->app->basePath(), $this->filesystem | ||
); | ||
|
||
$this->instance('orchestra.canvas', $preset); | ||
|
||
Artisan::starting(fn ($artisan) => $preset->addAdditionalCommands($artisan)); | ||
|
||
$this->artisan('make:generator', ['name' => 'FooCommand', '--command' => 'foobar']) | ||
->assertExitCode(0); | ||
|
||
$this->assertFileContains([ | ||
'namespace App\Console\Commands;', | ||
'use Orchestra\Canvas\Commands\Generator;', | ||
'use Symfony\Component\Console\Attribute\AsCommand;', | ||
'#[AsCommand(name: \'make:foobar\', description: \'Create a new class\')]', | ||
'class FooCommand extends Generator', | ||
], 'app/Console/Commands/FooCommand.php'); | ||
} | ||
} |