generate-mac/tests/GenerateMacCommandTest.php

96 lines
3.4 KiB
PHP

<?php declare(strict_types=1);
/*
* Copyright (c) 2018 https://sikofitt.com sikofitt@sikofitt.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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, '-'));
}
}