126 lines
3.0 KiB
PHP
126 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace MintyPHP\Tests\Console;
|
|
|
|
use MintyPHP\Console\Command;
|
|
use MintyPHP\Console\ConsoleApplication;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class ConsoleApplicationTest extends TestCase
|
|
{
|
|
public function testListReturnsZero(): void
|
|
{
|
|
$app = new ConsoleApplication();
|
|
$app->register(new DummyCommand());
|
|
|
|
$exitCode = $app->run(['bin/console', 'list']);
|
|
|
|
self::assertSame(0, $exitCode);
|
|
}
|
|
|
|
public function testEmptyArgvShowsListAndReturnsZero(): void
|
|
{
|
|
$app = new ConsoleApplication();
|
|
|
|
$exitCode = $app->run(['bin/console']);
|
|
|
|
self::assertSame(0, $exitCode);
|
|
}
|
|
|
|
public function testUnknownCommandReturnsOne(): void
|
|
{
|
|
$app = new ConsoleApplication();
|
|
|
|
$exitCode = $app->run(['bin/console', 'nonexistent']);
|
|
|
|
self::assertSame(1, $exitCode);
|
|
}
|
|
|
|
public function testDispatchesRegisteredCommand(): void
|
|
{
|
|
$command = new DummyCommand();
|
|
$app = new ConsoleApplication();
|
|
$app->register($command);
|
|
|
|
$exitCode = $app->run(['bin/console', 'test:dummy', 'arg1', '--flag', '--key=value']);
|
|
|
|
self::assertSame(0, $exitCode);
|
|
self::assertSame(['arg1'], $command->lastArgs);
|
|
self::assertSame(['flag' => true, 'key' => 'value'], $command->lastOptions);
|
|
}
|
|
|
|
public function testCommandExitCodePropagates(): void
|
|
{
|
|
$app = new ConsoleApplication();
|
|
$app->register(new FailingCommand());
|
|
|
|
$exitCode = $app->run(['bin/console', 'test:fail']);
|
|
|
|
self::assertSame(42, $exitCode);
|
|
}
|
|
|
|
public function testMultiplePositionalArgs(): void
|
|
{
|
|
$command = new DummyCommand();
|
|
$app = new ConsoleApplication();
|
|
$app->register($command);
|
|
|
|
$app->run(['bin/console', 'test:dummy', 'first', 'second', 'third']);
|
|
|
|
self::assertSame(['first', 'second', 'third'], $command->lastArgs);
|
|
}
|
|
|
|
public function testOptionWithEqualsSign(): void
|
|
{
|
|
$command = new DummyCommand();
|
|
$app = new ConsoleApplication();
|
|
$app->register($command);
|
|
|
|
$app->run(['bin/console', 'test:dummy', '--module=addressbook']);
|
|
|
|
self::assertSame([], $command->lastArgs);
|
|
self::assertSame(['module' => 'addressbook'], $command->lastOptions);
|
|
}
|
|
}
|
|
|
|
final class DummyCommand extends Command
|
|
{
|
|
public array $lastArgs = [];
|
|
public array $lastOptions = [];
|
|
|
|
public function name(): string
|
|
{
|
|
return 'test:dummy';
|
|
}
|
|
|
|
public function description(): string
|
|
{
|
|
return 'A dummy command';
|
|
}
|
|
|
|
public function execute(array $args, array $options): int
|
|
{
|
|
$this->lastArgs = $args;
|
|
$this->lastOptions = $options;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
final class FailingCommand extends Command
|
|
{
|
|
public function name(): string
|
|
{
|
|
return 'test:fail';
|
|
}
|
|
|
|
public function description(): string
|
|
{
|
|
return 'Always fails';
|
|
}
|
|
|
|
public function execute(array $args, array $options): int
|
|
{
|
|
return 42;
|
|
}
|
|
}
|