-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(console): add
make:request
command (#730)
- Loading branch information
1 parent
bfa1706
commit 987eabf
Showing
3 changed files
with
133 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,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Http\Commands; | ||
|
||
use Tempest\Console\ConsoleArgument; | ||
use Tempest\Console\ConsoleCommand; | ||
use Tempest\Core\PublishesFiles; | ||
use Tempest\Generation\DataObjects\StubFile; | ||
use Tempest\Generation\Exceptions\FileGenerationAbortedException; | ||
use Tempest\Generation\Exceptions\FileGenerationFailedException; | ||
use Tempest\Http\Stubs\RequestStub; | ||
|
||
final class MakeRequestCommand | ||
{ | ||
use PublishesFiles; | ||
|
||
#[ConsoleCommand( | ||
name: 'make:request', | ||
description: 'Creates a new request class', | ||
aliases: ['request:make', 'request:create', 'create:request'], | ||
)] | ||
public function __invoke( | ||
#[ConsoleArgument( | ||
help: 'The name of the request class to create', | ||
)] | ||
string $className, | ||
): void { | ||
$suggestedPath = $this->getSuggestedPath($className); | ||
$targetPath = $this->promptTargetPath($suggestedPath); | ||
$shouldOverride = $this->askForOverride($targetPath); | ||
|
||
try { | ||
$this->stubFileGenerator->generateClassFile( | ||
stubFile: StubFile::from(RequestStub::class), | ||
targetPath: $targetPath, | ||
shouldOverride: $shouldOverride, | ||
); | ||
|
||
$this->success(sprintf('Request successfully created at "%s".', $targetPath)); | ||
} catch (FileGenerationAbortedException|FileGenerationFailedException $e) { | ||
$this->error($e->getMessage()); | ||
} | ||
} | ||
} |
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,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Http\Stubs; | ||
|
||
use Tempest\Http\IsRequest; | ||
use Tempest\Http\Request; | ||
use Tempest\Validation\Rules\Length; | ||
|
||
final class RequestStub implements Request | ||
{ | ||
use IsRequest; | ||
|
||
#[Length(min: 10, max: 120)] | ||
public string $title; | ||
} |
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,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Tempest\Integration\Http; | ||
|
||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\Attributes\Test; | ||
use Tempest\Core\ComposerNamespace; | ||
use Tests\Tempest\Integration\FrameworkIntegrationTestCase; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class MakeRequestCommandTest extends FrameworkIntegrationTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->installer->configure( | ||
__DIR__ . '/install', | ||
new ComposerNamespace('App\\', __DIR__ . '/install/App') | ||
); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
$this->installer->clean(); | ||
|
||
parent::tearDown(); | ||
} | ||
|
||
#[Test] | ||
#[DataProvider('command_input_provider')] | ||
public function make_command( | ||
string $commandArgs, | ||
string $expectedPath, | ||
string $expectedNamespace | ||
): void { | ||
$this->console | ||
->call("make:request {$commandArgs}") | ||
->submit(); | ||
|
||
$this->installer | ||
->assertFileExists($expectedPath) | ||
->assertFileContains($expectedPath, 'namespace ' . $expectedNamespace . ';'); | ||
} | ||
|
||
public static function command_input_provider(): array | ||
{ | ||
return [ | ||
'make_with_defaults' => [ | ||
'commandArgs' => 'BookRequest', | ||
'expectedPath' => 'App/BookRequest.php', | ||
'expectedNamespace' => 'App', | ||
], | ||
'make_with_other_namespace' => [ | ||
'commandArgs' => 'Requests\\BookRequest', | ||
'expectedPath' => 'App/Requests/BookRequest.php', | ||
'expectedNamespace' => 'App\\Requests', | ||
], | ||
'make_with_input_path' => [ | ||
'commandArgs' => 'Requests/BookRequest', | ||
'expectedPath' => 'App/Requests/BookRequest.php', | ||
'expectedNamespace' => 'App\\Requests', | ||
], | ||
]; | ||
} | ||
} |