Skip to content

Commit

Permalink
feat(console): add make:request command (#730)
Browse files Browse the repository at this point in the history
  • Loading branch information
gturpin-dev authored Nov 14, 2024
1 parent bfa1706 commit 987eabf
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Tempest/Http/src/Commands/MakeRequestCommand.php
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());
}
}
}
17 changes: 17 additions & 0 deletions src/Tempest/Http/src/Stubs/RequestStub.php
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;
}
70 changes: 70 additions & 0 deletions tests/Integration/Http/MakeRequestCommandTest.php
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',
],
];
}
}

0 comments on commit 987eabf

Please sign in to comment.