Skip to content

Commit

Permalink
QA
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Apr 9, 2024
1 parent 8990581 commit 957aeef
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 24 deletions.
10 changes: 8 additions & 2 deletions app/Console/ComplexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@

namespace App\Console;

use Tempest\Console\Console;
use Tempest\Console\ConsoleCommand;

final readonly class ComplexCommand
{
#[ConsoleCommand('complex')]
public function __invoke(string $a, string $b, string $c)
public function __construct(private Console $console)
{
}

#[ConsoleCommand('complex')]
public function __invoke(string $a, string $b, string $c, bool $flag = false)
{
$this->console->writeln($a . $b . $c);
$this->console->writeln($flag ? 'true' : 'false');
}
}
8 changes: 4 additions & 4 deletions src/ConsoleArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
use Attribute;

#[Attribute(Attribute::TARGET_METHOD | Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
final class ConsoleArgument
final readonly class ConsoleArgument
{
public function __construct(
public readonly ?string $description = null,
public readonly string $help = '',
public readonly array $aliases = [],
public ?string $description = null,
public string $help = '',
public array $aliases = [],
) {
}
}
2 changes: 1 addition & 1 deletion src/ConsoleArgumentBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public function findFor(ConsoleArgumentDefinition $argumentDefinition): ?Console
if ($argumentDefinition->hasDefault) {
return new ConsoleInputArgument(
name: $argumentDefinition->name,
value: $argumentDefinition->default,
position: $argumentDefinition->position,
value: $argumentDefinition->default,
);
}

Expand Down
20 changes: 10 additions & 10 deletions src/ConsoleArgumentDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
use ReflectionNamedType;
use ReflectionParameter;

final class ConsoleArgumentDefinition
final readonly class ConsoleArgumentDefinition
{
public function __construct(
public readonly string $name,
public readonly string $type,
public readonly mixed $default,
public readonly bool $hasDefault,
public readonly int $position,
public readonly ?string $description = null,
public readonly array $aliases = [],
public readonly ?string $help = null,
public string $name,
public string $type,
public mixed $default,
public bool $hasDefault,
public int $position,
public ?string $description = null,
public array $aliases = [],
public ?string $help = null,
) {
}

Expand Down Expand Up @@ -58,7 +58,7 @@ public function matchesArgument(ConsoleInputArgument $argument): bool
return false;
}

foreach ([$argument->name, ...$this->aliases] as $alias) {
foreach ([$this->name, ...$this->aliases] as $alias) {
if ($alias === $argument->name) {
return true;
}
Expand Down
8 changes: 5 additions & 3 deletions src/ConsoleInputArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ final class ConsoleInputArgument
{
public function __construct(
public ?string $name,
public ?int $position,
public mixed $value,
public int $position,
public bool $isPositional = false,
) {
}

Expand All @@ -20,14 +21,15 @@ public static function fromString(string $argument, int $position): ConsoleInput

return new ConsoleInputArgument(
name: $key,
position: null,
value: $value,
position: $position,
);
} else {
return new ConsoleInputArgument(
name: null,
value: $argument,
position: $position,
value: $argument,
isPositional: true,
);
}
}
Expand Down
16 changes: 12 additions & 4 deletions tests/ArgumentBagTest.php → tests/ConsoleArgumentBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

namespace Tests\Tempest\Console;

use PHPUnit\Framework\TestCase;
use Tempest\Console\ConsoleArgumentBag;

/**
* @internal
* @small
*/
final class ArgumentBagTest extends TestCase
final class ConsoleArgumentBagTest extends TestCase
{
public function test_argument_bag_works(): void
{
Expand All @@ -34,17 +33,26 @@ public function test_argument_bag_works(): void

$forceFlag = $bag->all()[1];
$this->assertSame(true, $forceFlag->value);
$this->assertSame(1, $forceFlag->position);
$this->assertSame(null, $forceFlag->position);
$this->assertSame('force', $forceFlag->name);

$timesFlag = $bag->all()[2];
$this->assertSame('2', $timesFlag->value);
$this->assertSame(2, $timesFlag->position);
$this->assertSame(null, $timesFlag->position);
$this->assertSame('times', $timesFlag->name);

$this->assertSame(
'hello:world',
$bag->getCommandName(),
);
}

public function test_positional_vs_named_input(): void
{
$this->console
->call('complex a --c=c --b=b --flag')
->assertContains('abc')
->assertContains('true')
;
}
}

0 comments on commit 957aeef

Please sign in to comment.