. */ namespace Sikofitt\GenerateMac\Tests; use PHPUnit\Framework\TestCase; use Sikofitt\GenerateMac\Command\GenerateMacCommand; use Symfony\Component\Console\Application; use Symfony\Component\Console\Exception\InvalidArgumentException; use Symfony\Component\Console\Tester\CommandTester; class GenerateMacCommandTest extends TestCase { private const REGEX = '/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/'; /** * @var CommandTester */ private $commandTester; public function setUp(): void { $application = new Application(); $application->add(new GenerateMacCommand()); $application->setDefaultCommand('generate-mac', true); $this->commandTester = new CommandTester(new GenerateMacCommand()); parent::setUp(); // TODO: Change the autogenerated stub } public function testCommand(): void { $this->commandTester->execute([]); $display = $this->commandTester->getDisplay(); $this->assertContains('// Generated 1 mac addresses', $display); $this->assertSame(0, $this->commandTester->getStatusCode()); $this->expectException(\RuntimeException::class); $this->commandTester->execute(['--count' => -1]); } public function testJson(): void { $this->commandTester->execute(['--output' => 'json']); $this->assertJson($this->commandTester->getDisplay()); $this->assertSame(0, $this->commandTester->getStatusCode()); } public function testPlain(): void { $this->commandTester->execute(['--output' => 'plain', '--count' => 1]); $this->assertRegExp(self::REGEX, $this->commandTester->getDisplay()); $this->assertSame(0, $this->commandTester->getStatusCode()); } public function testInvalidSeparator(): void { $this->expectException(InvalidArgumentException::class); $this->commandTester->execute(['--separator' => 'invalid']); } public function testSeparators(): void { $this->commandTester->execute(['--separator' => 'colon', '--output' => 'plain']); $output = $this->commandTester->getDisplay(); $this->assertNotFalse(strpos($output, ':')); $this->commandTester->execute(['--separator' => 'none', '--output' => 'plain']); $output = $this->commandTester->getDisplay(true); // 13 because it adds a new line with SymfonyStyle::writeLn(); $this->assertSame(13, \strlen($output)); // just to make sure trim it. $this->assertSame(12, \strlen(trim($output))); $this->commandTester->execute(['--separator' => 'dash', '--output' => 'plain']); $output = $this->commandTester->getDisplay(); $this->assertNotFalse(strpos($output, '-')); } }