Skip to content

Commit

Permalink
Merge pull request #16 from abacaphiliac/coverage
Browse files Browse the repository at this point in the history
increase coverage
  • Loading branch information
abacaphiliac authored Jan 29, 2020
2 parents 0bf991b + cdc48b2 commit cb753f9
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 1 deletion.
95 changes: 95 additions & 0 deletions test/LogLevelConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace AbacaphiliacTest\Doctrine;

use Abacaphiliac\Doctrine\LogLevelConfiguration;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Psr\Log\LogLevel;

/**
* @covers \Abacaphiliac\Doctrine\LogLevelConfiguration
*/
class LogLevelConfigurationTest extends TestCase
{
/** @var LogLevelConfiguration */
private $sut;

protected function setUp()
{
$this->sut = new LogLevelConfiguration([
LogLevel::DEBUG => 0,
LogLevel::INFO => 10,
LogLevel::NOTICE => 20,
LogLevel::WARNING => 30,
LogLevel::ERROR => 40,
LogLevel::CRITICAL => 50,
LogLevel::ALERT => 60,
LogLevel::EMERGENCY => 70,
]);
}

public static function dataValidLevels(): array
{
return [
[-1, null],
[0, null],
[1, LogLevel::DEBUG],
[9, LogLevel::DEBUG],
[10, LogLevel::INFO],
[11, LogLevel::INFO],
[19, LogLevel::INFO],
[20, LogLevel::NOTICE],
[21, LogLevel::NOTICE],
[29, LogLevel::NOTICE],
[30, LogLevel::WARNING],
[31, LogLevel::WARNING],
[39, LogLevel::WARNING],
[40, LogLevel::ERROR],
[41, LogLevel::ERROR],
[49, LogLevel::ERROR],
[50, LogLevel::CRITICAL],
[51, LogLevel::CRITICAL],
[59, LogLevel::CRITICAL],
[60, LogLevel::ALERT],
[61, LogLevel::ALERT],
[69, LogLevel::ALERT],
[70, LogLevel::EMERGENCY],
[71, LogLevel::EMERGENCY],
[79, LogLevel::EMERGENCY],
];
}

/**
* @dataProvider dataValidLevels
* @param float $durationMs
* @param string | null $expected
*/
public function testValidLevels(
float $durationMs,
?string $expected
): void {
$actual = $this->sut->getApplicableLogLevel($durationMs / 1000);

self::assertSame($expected, $actual);
}

public function testInvalidLogLevel(): void
{
self::expectException(InvalidArgumentException::class);
self::expectExceptionMessage('invalid LogLevel detected: "InvalidLevel", please choose from: "' . print_r([
'EMERGENCY' => 'emergency',
'ALERT' => 'alert',
'CRITICAL' => 'critical',
'ERROR' => 'error',
'WARNING' => 'warning',
'NOTICE' => 'notice',
'INFO' => 'info',
'DEBUG' => 'debug',
], true) . '"');

new LogLevelConfiguration([
'InvalidLevel' => 1000,
]);
}
}
50 changes: 49 additions & 1 deletion test/PsrSqlLoggerConfigurableLogLevelsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Abacaphiliac\Doctrine\LogLevelConfiguration;
use Abacaphiliac\Doctrine\PsrSqlLoggerConfigurableLogLevels;
use Psr\Log\LoggerInterface;
use Psr\Log\Test\TestLogger;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
Expand All @@ -13,7 +14,7 @@
use function usleep;

/**
* @covers \Abacaphiliac\Doctrine\PsrSqlLogger
* @covers \Abacaphiliac\Doctrine\PsrSqlLoggerConfigurableLogLevels
*/
class PsrSqlLoggerConfigurableLogLevelsTest extends TestCase
{
Expand Down Expand Up @@ -118,6 +119,53 @@ public function testInvalidLogLevelUsedInConfiguration() : void
);
}

public function testInvalidDefaultLogLevel() : void
{
$this->expectException(InvalidArgumentException::class);
new PsrSqlLoggerConfigurableLogLevels(
new class implements LoggerInterface {
public function emergency($message, array $context = array())
{
}

public function alert($message, array $context = array())
{
}

public function critical($message, array $context = array())
{
}

public function error($message, array $context = array())
{
}

public function warning($message, array $context = array())
{
}

public function notice($message, array $context = array())
{
}

public function info($message, array $context = array())
{
}

public function debug($message, array $context = array())
{
}

public function log($level, $message, array $context = array())
{
}
},
new LogLevelConfiguration([
]),
'InvalidLevel'
);
}

protected function setUp()
{
$this->logger = new TestLogger();
Expand Down

0 comments on commit cb753f9

Please sign in to comment.