generate-mac/tests/GenerateMacCommandTest.php

96 lines
3.5 KiB
PHP
Raw Normal View History

2018-11-29 09:12:26 -08:00
<?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();
2021-01-20 14:44:22 -08:00
$this->assertStringContainsString('// Generated 1 mac addresses', $display);
2018-11-29 09:12:26 -08:00
$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]);
2021-01-20 14:44:22 -08:00
$this->assertMatchesRegularExpression(self::REGEX, $this->commandTester->getDisplay());
2018-11-29 09:12:26 -08:00
$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));
2018-11-29 09:12:26 -08:00
// just to make sure trim it.
$this->assertSame(12, \strlen(trim($output)));
2018-11-29 09:12:26 -08:00
$this->commandTester->execute(['--separator' => 'dash', '--output' => 'plain']);
$output = $this->commandTester->getDisplay();
$this->assertNotFalse(strpos($output, '-'));
}
}