Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Cli/Attribute/InvalidArgument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Bauhaus\Cli\Attribute;

use InvalidArgumentException;

final class InvalidArgument extends InvalidArgumentException
{
public function __construct(string $invalid)
{
parent::__construct("Invalid cli argument: '$invalid'");
}
}
12 changes: 12 additions & 0 deletions src/Cli/Attribute/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ final class Name
public function __construct(
private string $value,
) {
$this->assertIsValid();
}

public function equalTo(self $that): bool
Expand All @@ -21,4 +22,15 @@ public function __toString(): string
{
return $this->value;
}

private function assertIsValid(): void
{
$alphaNumeric = 'A-Za-z0-9';
$allowed = "$alphaNumeric-_";
$word = "[$alphaNumeric][$allowed]*";

if (0 === preg_match("/^$word(:$word)*$/", $this->value)) {
throw new InvalidArgument($this->value);
}
}
}
2 changes: 1 addition & 1 deletion src/Cli/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function execute(Input $input, Output $output): void

public function match(Input $input): bool
{
return $this->id->equalTo($input->commandId());
return $this->id->equalTo($input->commandName());
}

private function extractEntrypointAttributes(): void
Expand Down
8 changes: 4 additions & 4 deletions src/Cli/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
final class Input
{
private array $argv;
private Name $commandId;
private Name $commandName;

private function __construct(string ...$rawInput)
{
Expand All @@ -25,13 +25,13 @@ public static function fromString(string $string): self
return new self(...explode(' ', $string));
}

public function commandId(): Name
public function commandName(): Name
{
return $this->commandId;
return $this->commandName;
}

private function parseInput(): void
{
$this->commandId = new Name($this->argv[1]);
$this->commandName = new Name($this->argv[1]);
}
}
78 changes: 78 additions & 0 deletions tests/Cli/Attribute/NameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Bauhaus\Cli\Attribute;

use PHPUnit\Framework\TestCase;

class NameTest extends TestCase
{
public function invalidNames(): array
{
return [
['inv lid'],
['inv@lid'],
[' invalid'],
[''],
['-'],
['invalid:'],
];
}

/**
* @test
* @dataProvider invalidNames
*/
public function throwExceptionIfNameIsInvalid(string $invalid): void
{
$this->expectException(InvalidArgument::class);
$this->expectExceptionMessage("Invalid cli argument: '$invalid'");

new Name($invalid);
}

public function validNames(): array
{
return [
['valid'],
['val:id'],
['va:aa:lid'],
['v:aa:lid'],
['v:a:lid'],
['val_id'],
['val-id'],
['val-i_d'],
['val_-id'],
];
}

/**
* @test
* @dataProvider validNames
*/
public function convertToString(string $str): void
{
$name = new Name($str);

$this->assertEquals($str, (string) $name);
}

/**
* @test
*/
public function returnTrueIfTwoNameAreEqual(): void
{
$name = new Name('something');

$this->assertTrue($name->equalTo(new Name('something')));
}

/**
* @test
*/
public function returnFalseIfTwoNameAreNotEqual(): void
{
$name = new Name('something');

$this->assertFalse($name->equalTo(new Name('anotherThing')));
}
}
2 changes: 1 addition & 1 deletion tests/Cli/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function determineCommandIdFromArgv(): void
$expected = new Name('command-id');

$input = Input::fromArgv('./bin/console', 'command-id');
$commandId = $input->commandId();
$commandId = $input->commandName();

$this->assertEquals($expected, $commandId);
}
Expand Down