Skip to content

Commit

Permalink
Merge branch '7.x' into 8.x
Browse files Browse the repository at this point in the history
Signed-off-by: Mior Muhammad Zaki <[email protected]>
  • Loading branch information
crynobone committed Sep 13, 2023
2 parents 27ad3d1 + bd94889 commit f146875
Show file tree
Hide file tree
Showing 6 changed files with 317 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/Commands/Generators/Code.php
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';
}
}
87 changes: 87 additions & 0 deletions src/Commands/Generators/ConsoleGenerator.php
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'],
];
}
}
8 changes: 8 additions & 0 deletions storage/canvas/code.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace DummyNamespace;

class DummyClass
{
//
}
51 changes: 51 additions & 0 deletions storage/canvas/generator.stub
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(),
];
}
}
53 changes: 53 additions & 0 deletions tests/Feature/Commands/Generators/CodeTest.php
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 tests/Feature/Commands/Generators/ConsoleGeneratorTest.php
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');
}
}

0 comments on commit f146875

Please sign in to comment.