From 626ed7a01c80352b46251bcbfb29f528ad62c077 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Wed, 13 Sep 2023 13:45:17 +0800 Subject: [PATCH 1/2] wip Signed-off-by: Mior Muhammad Zaki --- src/Commands/Generators/Code.php | 31 +++++++ src/Commands/Generators/ConsoleGenerator.php | 87 +++++++++++++++++++ storage/canvas/code.stub | 8 ++ storage/canvas/generator.stub | 51 +++++++++++ .../Feature/Commands/Generators/CodeTest.php | 53 +++++++++++ .../Generators/ConsoleGeneratorTest.php | 87 +++++++++++++++++++ 6 files changed, 317 insertions(+) create mode 100644 src/Commands/Generators/Code.php create mode 100644 src/Commands/Generators/ConsoleGenerator.php create mode 100644 storage/canvas/code.stub create mode 100644 storage/canvas/generator.stub create mode 100644 tests/Feature/Commands/Generators/CodeTest.php create mode 100644 tests/Feature/Commands/Generators/ConsoleGeneratorTest.php diff --git a/src/Commands/Generators/Code.php b/src/Commands/Generators/Code.php new file mode 100644 index 0000000..33f12d4 --- /dev/null +++ b/src/Commands/Generators/Code.php @@ -0,0 +1,31 @@ +getStubFileName(); + } + + /** + * Get the stub file name for the generator. + */ + public function getStubFileName(): string + { + return __DIR__.'/../../../storage/canvas/code.stub'; + } +} diff --git a/src/Commands/Generators/ConsoleGenerator.php b/src/Commands/Generators/ConsoleGenerator.php new file mode 100644 index 0000000..de76c1a --- /dev/null +++ b/src/Commands/Generators/ConsoleGenerator.php @@ -0,0 +1,87 @@ + + */ + 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 + */ + 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 + */ + 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'], + ]; + } +} diff --git a/storage/canvas/code.stub b/storage/canvas/code.stub new file mode 100644 index 0000000..daae325 --- /dev/null +++ b/storage/canvas/code.stub @@ -0,0 +1,8 @@ + + */ + 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(), + ]; + } +} diff --git a/tests/Feature/Commands/Generators/CodeTest.php b/tests/Feature/Commands/Generators/CodeTest.php new file mode 100644 index 0000000..eb1c192 --- /dev/null +++ b/tests/Feature/Commands/Generators/CodeTest.php @@ -0,0 +1,53 @@ + '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'); + } +} diff --git a/tests/Feature/Commands/Generators/ConsoleGeneratorTest.php b/tests/Feature/Commands/Generators/ConsoleGeneratorTest.php new file mode 100644 index 0000000..c7a48f6 --- /dev/null +++ b/tests/Feature/Commands/Generators/ConsoleGeneratorTest.php @@ -0,0 +1,87 @@ + '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'); + } +} From bd94889794d1feb14b505e9275374d9420d07585 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Wed, 13 Sep 2023 13:49:39 +0800 Subject: [PATCH 2/2] wip Signed-off-by: Mior Muhammad Zaki --- phpunit.xml | 56 ++++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index 53b2727..7c2f46a 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,31 +1,31 @@ - - - src/ - - - src/Core/Commands/Concerns - - - - - ./tests/ - - - - - + xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" + backupGlobals="false" + backupStaticAttributes="false" + bootstrap="vendor/autoload.php" + colors="true" + convertDeprecationsToExceptions="true" + convertErrorsToExceptions="true" + convertNoticesToExceptions="true" + convertWarningsToExceptions="true" + processIsolation="false" + stopOnFailure="false" + verbose="true"> + + + src/ + + + src/Testing/ + + + + + tests/ + + + + +