Skip to content

Commit e76c1f8

Browse files
authored
feat(console): add make:response command (tempestphp#760)
1 parent 25d4a47 commit e76c1f8

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Http\Commands;
6+
7+
use Tempest\Console\ConsoleArgument;
8+
use Tempest\Console\ConsoleCommand;
9+
use Tempest\Core\PublishesFiles;
10+
use Tempest\Generation\DataObjects\StubFile;
11+
use Tempest\Generation\Exceptions\FileGenerationAbortedException;
12+
use Tempest\Generation\Exceptions\FileGenerationFailedException;
13+
use Tempest\Http\Stubs\ResponseStub;
14+
15+
final class MakeResponseCommand
16+
{
17+
use PublishesFiles;
18+
19+
#[ConsoleCommand(
20+
name: 'make:response',
21+
description: 'Creates a new response class',
22+
aliases: ['response:make', 'response:create', 'create:response'],
23+
)]
24+
public function __invoke(
25+
#[ConsoleArgument(
26+
help: 'The name of the response class to create',
27+
)]
28+
string $className,
29+
): void {
30+
$suggestedPath = $this->getSuggestedPath($className);
31+
$targetPath = $this->promptTargetPath($suggestedPath);
32+
$shouldOverride = $this->askForOverride($targetPath);
33+
34+
try {
35+
$this->stubFileGenerator->generateClassFile(
36+
stubFile: StubFile::from(ResponseStub::class),
37+
targetPath: $targetPath,
38+
shouldOverride: $shouldOverride,
39+
);
40+
41+
$this->success(sprintf('Response successfully created at "%s".', $targetPath));
42+
} catch (FileGenerationAbortedException|FileGenerationFailedException $e) {
43+
$this->error($e->getMessage());
44+
}
45+
}
46+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Http\Stubs;
6+
7+
use Tempest\Http\IsResponse;
8+
use Tempest\Http\Response;
9+
use Tempest\Http\Status;
10+
11+
final class ResponseStub implements Response
12+
{
13+
use IsResponse;
14+
15+
public function __construct()
16+
{
17+
$this->setStatus(Status::OK);
18+
$this->addHeader('Content-Type', 'application/json');
19+
}
20+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Tempest\Integration\Http;
6+
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
use PHPUnit\Framework\Attributes\Test;
9+
use Tempest\Core\ComposerNamespace;
10+
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
11+
12+
/**
13+
* @internal
14+
*/
15+
final class MakeResponseCommandTest extends FrameworkIntegrationTestCase
16+
{
17+
protected function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
$this->installer->configure(
22+
__DIR__ . '/install',
23+
new ComposerNamespace('App\\', __DIR__ . '/install/App')
24+
);
25+
}
26+
27+
protected function tearDown(): void
28+
{
29+
$this->installer->clean();
30+
31+
parent::tearDown();
32+
}
33+
34+
#[Test]
35+
#[DataProvider('command_input_provider')]
36+
public function make_command(
37+
string $commandArgs,
38+
string $expectedPath,
39+
string $expectedNamespace
40+
): void {
41+
$this->console
42+
->call("make:response {$commandArgs}")
43+
->submit();
44+
45+
$this->installer
46+
->assertFileExists($expectedPath)
47+
->assertFileContains($expectedPath, 'namespace ' . $expectedNamespace . ';')
48+
->assertFileContains($expectedPath, 'implements Response')
49+
->assertFileContains($expectedPath, 'use IsResponse');
50+
}
51+
52+
public static function command_input_provider(): array
53+
{
54+
return [
55+
'make_with_defaults' => [
56+
'commandArgs' => 'BookCreatedResponse',
57+
'expectedPath' => 'App/BookCreatedResponse.php',
58+
'expectedNamespace' => 'App',
59+
],
60+
'make_with_other_namespace' => [
61+
'commandArgs' => 'Responses\\BookCreatedResponse',
62+
'expectedPath' => 'App/Responses/BookCreatedResponse.php',
63+
'expectedNamespace' => 'App\\Responses',
64+
],
65+
'make_with_input_path' => [
66+
'commandArgs' => 'Responses/BookCreatedResponse',
67+
'expectedPath' => 'App/Responses/BookCreatedResponse.php',
68+
'expectedNamespace' => 'App\\Responses',
69+
],
70+
];
71+
}
72+
}

0 commit comments

Comments
 (0)