Tasker/tests/RunnerTest.php

65 lines
2 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Phred\Tasker\Tests;
use PHPUnit\Framework\TestCase;
use Phred\Tasker\Runner;
use Phred\ConsoleContracts\CommandInterface;
use Phred\ConsoleContracts\InputInterface;
use Phred\ConsoleContracts\OutputInterface;
use Psr\Container\ContainerInterface;
class RunnerTest extends TestCase
{
public function testRegisterInstance(): void
{
$runner = new Runner();
$command = $this->createMock(CommandInterface::class);
$command->method('getName')->willReturn('test:command');
$runner->register($command);
$this->assertCount(3, $runner->getCommands()); // 2 built-in + 1 registered
$this->assertSame($command, $runner->find('test:command'));
}
public function testRegisterByClassName(): void
{
$runner = new Runner();
$runner->register(MockCommand::class);
$this->assertInstanceOf(MockCommand::class, $runner->find('mock:command'));
}
public function testRegisterByContainer(): void
{
$container = $this->createMock(ContainerInterface::class);
$mockCommand = new MockCommand();
$container->method('has')->with(MockCommand::class)->willReturn(true);
$container->method('get')->with(MockCommand::class)->willReturn($mockCommand);
$runner = new Runner($container);
$runner->register(MockCommand::class);
$this->assertSame($mockCommand, $runner->find('mock:command'));
}
public function testBuiltInCommands(): void
{
$runner = new Runner();
$this->assertArrayHasKey('list', $runner->getCommands());
$this->assertArrayHasKey('help', $runner->getCommands());
}
}
class MockCommand implements CommandInterface
{
public function getName(): string { return 'mock:command'; }
public function getDescription(): string { return 'Mock description'; }
public function getArguments(): array { return []; }
public function getOptions(): array { return []; }
public function execute(InputInterface $input, OutputInterface $output): int { return 0; }
}