Skip to content

Commit 13bc731

Browse files
authored
feat(console): add make:command command (#1048)
1 parent d696826 commit 13bc731

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Console\Commands;
6+
7+
use Tempest\Console\ConsoleArgument;
8+
use Tempest\Console\ConsoleCommand;
9+
use Tempest\Console\Stubs\CommandStub;
10+
use Tempest\Core\PublishesFiles;
11+
use Tempest\Generation\DataObjects\StubFile;
12+
13+
use function Tempest\Support\str;
14+
15+
final class MakeCommandCommand
16+
{
17+
use PublishesFiles;
18+
19+
#[ConsoleCommand(
20+
name: 'make:command',
21+
description: 'Creates a new command class',
22+
aliases: ['command:make', 'command:create', 'create:command'],
23+
)]
24+
public function __invoke(
25+
#[ConsoleArgument(description: 'The name of the command class to create')]
26+
string $className,
27+
): void {
28+
$suggestedPath = $this->getSuggestedPath($className);
29+
$targetPath = $this->promptTargetPath($suggestedPath);
30+
$shouldOverride = $this->askForOverride($targetPath);
31+
32+
$this->stubFileGenerator->generateClassFile(
33+
stubFile: StubFile::from(CommandStub::class),
34+
targetPath: $targetPath,
35+
shouldOverride: $shouldOverride,
36+
replacements: [
37+
'dummy-command-slug' => str($className)->kebab()->toString(),
38+
],
39+
);
40+
41+
$this->console->success(sprintf('File successfully created at <em>%s</em>.', $targetPath));
42+
}
43+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Console\Stubs;
6+
7+
use Tempest\Console\Console;
8+
use Tempest\Console\ConsoleCommand;
9+
10+
final class CommandStub
11+
{
12+
public function __construct(
13+
private Console $console,
14+
) {
15+
}
16+
17+
#[ConsoleCommand(name: 'dummy-command-slug')]
18+
public function __invoke(): void
19+
{
20+
$this->console->success('Done!');
21+
}
22+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Tempest\Integration\Console\Commands;
6+
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use PHPUnit\Framework\Attributes\Test;
9+
use Tempest\Cache\CacheConfig;
10+
use Tempest\CommandBus\CommandBusConfig;
11+
use Tempest\Console\ConsoleConfig;
12+
use Tempest\Console\Enums\ConfigType;
13+
use Tempest\Core\ComposerNamespace;
14+
use Tempest\Database\Config\MysqlConfig;
15+
use Tempest\EventBus\EventBusConfig;
16+
use Tempest\Log\LogConfig;
17+
use Tempest\View\Renderers\BladeConfig;
18+
use Tempest\View\Renderers\TwigConfig;
19+
use Tempest\View\ViewConfig;
20+
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
21+
22+
use function Tempest\get;
23+
use function Tempest\Support\str;
24+
25+
/**
26+
* @internal
27+
*/
28+
final class MakeCommandCommandTest extends FrameworkIntegrationTestCase
29+
{
30+
protected function setUp(): void
31+
{
32+
parent::setUp();
33+
34+
$this->installer->configure(
35+
__DIR__ . '/install',
36+
new ComposerNamespace('App\\', __DIR__ . '/install/App'),
37+
);
38+
}
39+
40+
protected function tearDown(): void
41+
{
42+
$this->installer->clean();
43+
44+
parent::tearDown();
45+
}
46+
47+
#[DataProvider('command_input_provider')]
48+
#[Test]
49+
public function make_command(
50+
string $commandArgs,
51+
string $expectedPath,
52+
string $expectedNamespace,
53+
): void {
54+
$this->console
55+
->call("make:command {$commandArgs}")
56+
->submit();
57+
58+
$this->installer
59+
->assertFileExists($expectedPath)
60+
->assertFileContains($expectedPath, 'namespace ' . $expectedNamespace . ';');
61+
}
62+
63+
public static function command_input_provider(): array
64+
{
65+
return [
66+
'make_with_defaults' => [
67+
'commandArgs' => 'MyCommand',
68+
'expectedPath' => 'App/MyCommand.php',
69+
'expectedNamespace' => 'App',
70+
],
71+
'make_with_other_namespace' => [
72+
'commandArgs' => 'Commands\\MyCommand',
73+
'expectedPath' => 'App/Commands/MyCommand.php',
74+
'expectedNamespace' => 'App\\Commands',
75+
],
76+
'make_with_input_path' => [
77+
'commandArgs' => 'Commands/MyCommand',
78+
'expectedPath' => 'App/Commands/MyCommand.php',
79+
'expectedNamespace' => 'App\\Commands',
80+
],
81+
];
82+
}
83+
}

0 commit comments

Comments
 (0)