Skip to content

Commit

Permalink
Try stricter API for Command args existence.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Apr 6, 2024
1 parent d3dd712 commit 7a3de21
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
20 changes: 20 additions & 0 deletions src/Console/Arguments.php
Expand Up @@ -104,6 +104,8 @@ public function hasArgumentAt(int $index): bool
*/
public function hasArgument(string $name): bool
{
$this->assertArgumentExists($name);

$offset = array_search($name, $this->argNames, true);
if ($offset === false) {
return false;
Expand All @@ -120,6 +122,8 @@ public function hasArgument(string $name): bool
*/
public function getArgument(string $name): ?string
{
$this->assertArgumentExists($name);

$offset = array_search($name, $this->argNames, true);
if ($offset === false || !isset($this->args[$offset])) {
return null;
Expand Down Expand Up @@ -205,4 +209,20 @@ public function hasOption(string $name): bool
{
return isset($this->options[$name]);
}

/**
* @param string $name
* @return void
*/
protected function assertArgumentExists(string $name): void
{
if (in_array($name, $this->argNames, true)) {
return;
}

throw new ConsoleException(sprintf(
'Argument `%s` is not defined on this Command. Could this be an option maybe?',
$name
));
}
}
19 changes: 15 additions & 4 deletions tests/TestCase/Console/ArgumentsTest.php
Expand Up @@ -70,8 +70,6 @@ public function testHasArgument(): void
$args = new Arguments($values, [], $names);
$this->assertTrue($args->hasArgument('size'));
$this->assertTrue($args->hasArgument('color'));
$this->assertFalse($args->hasArgument('hair'));
$this->assertFalse($args->hasArgument('Hair'), 'casing matters');
$this->assertFalse($args->hasArgument('odd'));
}

Expand All @@ -85,8 +83,7 @@ public function testGetArgument(): void
$args = new Arguments($values, [], $names);
$this->assertSame($values[0], $args->getArgument('size'));
$this->assertSame($values[1], $args->getArgument('color'));
$this->assertNull($args->getArgument('Color'));
$this->assertNull($args->getArgument('hair'));
$this->assertNull($args->getArgument('odd'));
}

/**
Expand All @@ -101,6 +98,20 @@ public function testGetArgumentMissing(): void
$this->assertNull($args->getArgument('color'));
}

/**
* get arguments by name
*/
public function testGetArgumentInvalid(): void
{
$values = [];
$names = ['size'];
$args = new Arguments($values, [], $names);

$this->expectException(ConsoleException::class);

$args->getArgument('color');
}

/**
* test getOptions()
*/
Expand Down
14 changes: 7 additions & 7 deletions tests/TestCase/Console/CommandTest.php
Expand Up @@ -146,7 +146,7 @@ public function testRunOutputHelpLongOption(): void

$this->assertSame(
CommandInterface::CODE_SUCCESS,
$command->run(['--help'], $this->getMockIo($output))
$command->run(['test-me', '--help'], $this->getMockIo($output))
);
$messages = implode("\n", $output->messages());
$this->assertStringNotContainsString('Demo', $messages);
Expand All @@ -162,7 +162,7 @@ public function testRunVerboseOption(): void
$command->setName('cake demo');
$output = new StubConsoleOutput();

$this->assertNull($command->run(['--verbose'], $this->getMockIo($output)));
$this->assertNull($command->run(['test-me', '--verbose'], $this->getMockIo($output)));
$messages = implode("\n", $output->messages());
$this->assertStringContainsString('Verbose!', $messages);
$this->assertStringContainsString('Demo Command!', $messages);
Expand All @@ -179,7 +179,7 @@ public function testRunQuietOption(): void
$command->setName('cake demo');
$output = new StubConsoleOutput();

$this->assertNull($command->run(['--quiet'], $this->getMockIo($output)));
$this->assertNull($command->run(['test-me', '--quiet'], $this->getMockIo($output)));
$messages = implode("\n", $output->messages());
$this->assertStringContainsString('Quiet!', $messages);
$this->assertStringNotContainsString('Verbose!', $messages);
Expand Down Expand Up @@ -242,9 +242,9 @@ public function testExecuteCommandString(): void
{
$output = new StubConsoleOutput();
$command = new Command();
$result = $command->executeCommand(DemoCommand::class, [], $this->getMockIo($output));
$result = $command->executeCommand(DemoCommand::class, ['test-me'], $this->getMockIo($output));
$this->assertNull($result);
$this->assertEquals(['Quiet!', 'Demo Command!'], $output->messages());
$this->assertEquals(['Quiet!', 'Demo Command!', 'test-me'], $output->messages());
}

/**
Expand Down Expand Up @@ -276,9 +276,9 @@ public function testExecuteCommandInstance(): void
{
$output = new StubConsoleOutput();
$command = new Command();
$result = $command->executeCommand(new DemoCommand(), [], $this->getMockIo($output));
$result = $command->executeCommand(new DemoCommand(), ['test-me'], $this->getMockIo($output));
$this->assertNull($result);
$this->assertEquals(['Quiet!', 'Demo Command!'], $output->messages());
$this->assertEquals(['Quiet!', 'Demo Command!', 'test-me'], $output->messages());
}

/**
Expand Down

0 comments on commit 7a3de21

Please sign in to comment.