Skip to content

Commit

Permalink
[TASK] Add more unit tests (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabbelasichon committed Apr 2, 2020
1 parent 4ca43af commit be7a740
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 33 deletions.
10 changes: 0 additions & 10 deletions src/Cli/Symfony/Logger/ConsoleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,6 @@ public function handle(array $record): bool
return $this->updateLevel() && parent::handle($record);
}

/**
* {@inheritdoc}
*/
public function close()
{
$this->output = null;

parent::close();
}

/**
* {@inheritdoc}
*/
Expand Down
14 changes: 5 additions & 9 deletions src/Domain/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,22 @@
* file that was distributed with this source code.
*/

/**
* @codeCoverageIgnore
*/
class Filesystem implements FilesystemInterface
{

/**
* @param string $filename
* @param string $content
*
* @return bool|int
* @inheritDoc
*/
public function put($filename, $content)
{
return file_put_contents($filename, $content);
}

/**
* @param string $filename
* @param bool $includePath
* @param resource|null $streamContext
*
* @return false|string
* @inheritDoc
*/
public function get($filename, $includePath = false, $streamContext = null)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Domain/Generator/IdGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
* file that was distributed with this source code.
*/

/**
* @codeCoverageIgnore
*/
final class IdGenerator implements IdGeneratorInterface
{
public function generate(string $prefix): string
Expand Down
11 changes: 4 additions & 7 deletions src/Domain/Generator/RandomBytesGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@
* file that was distributed with this source code.
*/

/**
* @codeCoverageIgnore
*/
class RandomBytesGenerator implements RandomBytesGeneratorInterface
{

/**
* @param int $length
*
* @return string
*/
public function generate($length)
public function generate(int $length): string
{
return random_bytes($length);
}
Expand Down
8 changes: 1 addition & 7 deletions src/Domain/Generator/RandomBytesGeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,5 @@

interface RandomBytesGeneratorInterface
{

/**
* @param int $length
*
* @return string
*/
public function generate($length);
public function generate(int $length): string;
}
26 changes: 26 additions & 0 deletions tests/Unit/Cli/Symfony/ConsoleOutputFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace TYPO3\Surf\Tests\Unit\Cli\Symfony;

/*
* This file is part of TYPO3 Surf.
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Output\ConsoleOutput;
use TYPO3\Surf\Cli\Symfony\ConsoleOutputFactory;

class ConsoleOutputFactoryTest extends TestCase
{
/**
* @test
*/
public function createOutput(): void
{
$consoleOutputFactory = new ConsoleOutputFactory();
$this->assertInstanceOf(ConsoleOutput::class, $consoleOutputFactory->createOutput());
}
}
74 changes: 74 additions & 0 deletions tests/Unit/Cli/Symfony/Logger/ConsoleFormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace TYPO3\Surf\Tests\Unit\Cli\Symfony\Logger;

/*
* This file is part of TYPO3 Surf.
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use TYPO3\Surf\Cli\Symfony\Logger\ConsoleFormatter;

class ConsoleFormatterTest extends TestCase
{
/**
* @var ConsoleFormatter
*/
protected $subject;

public function records(): array
{
return [
[
[
'level' => Logger::ERROR,
'extra' => [],
'context' => []
],
"<error>%message%</error>\n"
],
[
[
'level' => Logger::NOTICE,
'extra' => [],
'context' => []
],
"<comment>%message%</comment>\n"
],
[
[
'level' => Logger::INFO,
'extra' => [],
'context' => []
],
"<info>%message%</info>\n"
],
[
[
'level' => Logger::DEBUG,
'extra' => [],
'context' => []
],
"<debug>%message%</debug>\n"
],
];
}

protected function setUp()
{
$this->subject = new ConsoleFormatter();
}

/**
* @test
* @dataProvider records
*/
public function format(array $record, string $expectedOutput): void
{
$this->assertEquals($expectedOutput, $this->subject->format($record));
}
}
61 changes: 61 additions & 0 deletions tests/Unit/Cli/Symfony/Logger/ConsoleHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace TYPO3\Surf\Tests\Unit\Cli\Symfony\Logger;

/*
* This file is part of TYPO3 Surf.
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Output\OutputInterface;
use TYPO3\Surf\Cli\Symfony\Logger\ConsoleFormatter;
use TYPO3\Surf\Cli\Symfony\Logger\ConsoleHandler;

class ConsoleHandlerTest extends TestCase
{
/**
* @test
*/
public function constructor(): void
{
$output = $this->prophesize(OutputInterface::class);
$handler = new ConsoleHandler($output->reveal(), false);
$this->assertFalse($handler->getBubble(), 'the bubble parameter gets propagated');
}

/**
* @test
*/
public function isHandlingReturnsTrue(): void
{
$output = $this->prophesize(OutputInterface::class);
$output->getVerbosity()->willReturn(OutputInterface::VERBOSITY_DEBUG)->shouldBeCalled();
$handler = new ConsoleHandler($output->reveal());
$this->assertTrue($handler->isHandling(['level' => Logger::ERROR]));
}

/**
* @test
*/
public function isHandlingReturnsFalseIfOutputIsQuiet(): void
{
$output = $this->prophesize(OutputInterface::class);
$output->getVerbosity()->willReturn(OutputInterface::VERBOSITY_QUIET)->shouldBeCalled();
$handler = new ConsoleHandler($output->reveal());
$this->assertFalse($handler->isHandling(['level' => Logger::ERROR]));
}

/**
* @test
*/
public function getFormatter(): void
{
$output = $this->prophesize(OutputInterface::class);
$handler = new ConsoleHandler($output->reveal());
$this->assertInstanceOf(ConsoleFormatter::class, $handler->getFormatter());
}
}
29 changes: 29 additions & 0 deletions tests/Unit/Cli/Symfony/Logger/LoggerFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace TYPO3\Surf\Tests\Unit\Cli\Symfony\Logger;

/*
* This file is part of TYPO3 Surf.
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use TYPO3\Surf\Cli\Symfony\Logger\ConsoleHandler;
use TYPO3\Surf\Cli\Symfony\Logger\LoggerFactory;

class LoggerFactoryTest extends TestCase
{
/**
* @test
*/
public function createLogger(): void
{
$consoleHandler = $this->prophesize(ConsoleHandler::class);
$loggerFactory = new LoggerFactory($consoleHandler->reveal());

$this->assertInstanceOf(Logger::class, $loggerFactory->createLogger());
}
}

0 comments on commit be7a740

Please sign in to comment.