-
-
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.
Co-authored-by: Brent Roose <[email protected]> Co-authored-by: Enzo Innocenzi <[email protected]>
- Loading branch information
1 parent
04000ac
commit 2a73033
Showing
9 changed files
with
157 additions
and
22 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
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
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
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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Tempest\Fixtures\Console; | ||
|
||
use Tempest\Console\ConsoleArgument; | ||
use Tempest\Console\ConsoleCommand; | ||
use Tempest\Console\HasConsole; | ||
|
||
final readonly class CommandWithArgumentName | ||
{ | ||
use HasConsole; | ||
|
||
#[ConsoleCommand(name: 'command-with-argument-name')] | ||
public function __invoke( | ||
#[ConsoleArgument(name: 'new-name')] | ||
string $input, | ||
#[ConsoleArgument(name: 'new-flag')] | ||
bool $flag = false, | ||
): void { | ||
$this->writeln($input); | ||
$this->writeln($flag ? 'true' : 'false'); | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Tempest\Fixtures\Console; | ||
|
||
use Tempest\Console\ConsoleArgument; | ||
|
||
// tests/Integration/Console/Input/ConsoleArgumentDefinitionTest.php | ||
final readonly class CommandWithDifferentArguments | ||
{ | ||
public function __invoke( | ||
string $string, | ||
string $camelCaseString, | ||
#[ConsoleArgument(name: 'my-kebab-string')] | ||
string $renamedKebabString, | ||
#[ConsoleArgument(name: 'myCamelString')] | ||
string $renamedCamelString, | ||
bool $bool, | ||
bool $camelCaseBool, | ||
string $camelCaseStringWithDefault = 'foo', | ||
bool $camelCaseBoolWithTrueDefault = true, | ||
bool $camelCaseBoolWithFalseDefault = false, | ||
): void { | ||
} | ||
} |
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
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
50 changes: 50 additions & 0 deletions
50
tests/Integration/Console/Input/ConsoleArgumentDefinitionTest.php
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,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Tempest\Integration\Console\Input; | ||
|
||
use PHPUnit\Framework\Attributes\TestWith; | ||
use PHPUnit\Framework\TestCase; | ||
use RuntimeException; | ||
use Tempest\Console\Input\ConsoleArgumentDefinition; | ||
use Tempest\Reflection\ClassReflector; | ||
use Tempest\Reflection\ParameterReflector; | ||
use Tests\Tempest\Fixtures\Console\CommandWithDifferentArguments; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ConsoleArgumentDefinitionTest extends TestCase | ||
{ | ||
#[TestWith(['string', 'string', 'string', null])] | ||
#[TestWith(['bool', 'bool', 'bool', null])] | ||
#[TestWith(['camelCaseString', 'camel-case-string', 'string', null])] | ||
#[TestWith(['camelCaseBool', 'camel-case-bool', 'bool', null])] | ||
#[TestWith(['renamedCamelString', 'my-camel-string', 'string', null])] | ||
#[TestWith(['renamedKebabString', 'my-kebab-string', 'string', null])] | ||
#[TestWith(['camelCaseBool', 'camel-case-bool', 'bool', null])] | ||
#[TestWith(['camelCaseStringWithDefault', 'camel-case-string-with-default', 'string', 'foo'])] | ||
#[TestWith(['camelCaseBoolWithTrueDefault', 'camel-case-bool-with-true-default', 'bool', true])] | ||
#[TestWith(['camelCaseBoolWithFalseDefault', 'camel-case-bool-with-false-default', 'bool', false])] | ||
public function test_parse_named_arguments_with_types_and_defaults(string $originalParameter, string $expectedName, string $expectedType, mixed $expectedDefault): void | ||
{ | ||
$definition = ConsoleArgumentDefinition::fromParameter($this->getParameter($originalParameter)); | ||
$this->assertSame($expectedName, $definition->name); | ||
$this->assertSame($expectedType, $definition->type); | ||
$this->assertSame($expectedDefault, $definition->default); | ||
} | ||
|
||
private function getParameter(string $name): ParameterReflector | ||
{ | ||
$reflector = new ClassReflector(CommandWithDifferentArguments::class); | ||
|
||
foreach ($reflector->getMethod('__invoke')->getParameters() as $parameter) { | ||
if ($parameter->getName() === $name) { | ||
return $parameter; | ||
} | ||
} | ||
|
||
throw new RuntimeException("Parameter not found: {$name}"); | ||
} | ||
} |
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