|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace AbacaphiliacTest\Doctrine; |
| 4 | + |
| 5 | +use Abacaphiliac\Doctrine\LogLevelConfiguration; |
| 6 | +use InvalidArgumentException; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Psr\Log\LogLevel; |
| 9 | + |
| 10 | +/** |
| 11 | + * @covers \Abacaphiliac\Doctrine\LogLevelConfiguration |
| 12 | + */ |
| 13 | +class LogLevelConfigurationTest extends TestCase |
| 14 | +{ |
| 15 | + /** @var LogLevelConfiguration */ |
| 16 | + private $sut; |
| 17 | + |
| 18 | + protected function setUp() |
| 19 | + { |
| 20 | + $this->sut = new LogLevelConfiguration([ |
| 21 | + LogLevel::DEBUG => 0, |
| 22 | + LogLevel::INFO => 10, |
| 23 | + LogLevel::NOTICE => 20, |
| 24 | + LogLevel::WARNING => 30, |
| 25 | + LogLevel::ERROR => 40, |
| 26 | + LogLevel::CRITICAL => 50, |
| 27 | + LogLevel::ALERT => 60, |
| 28 | + LogLevel::EMERGENCY => 70, |
| 29 | + ]); |
| 30 | + } |
| 31 | + |
| 32 | + public static function dataValidLevels(): array |
| 33 | + { |
| 34 | + return [ |
| 35 | + [-1, null], |
| 36 | + [0, null], |
| 37 | + [1, LogLevel::DEBUG], |
| 38 | + [9, LogLevel::DEBUG], |
| 39 | + [10, LogLevel::INFO], |
| 40 | + [11, LogLevel::INFO], |
| 41 | + [19, LogLevel::INFO], |
| 42 | + [20, LogLevel::NOTICE], |
| 43 | + [21, LogLevel::NOTICE], |
| 44 | + [29, LogLevel::NOTICE], |
| 45 | + [30, LogLevel::WARNING], |
| 46 | + [31, LogLevel::WARNING], |
| 47 | + [39, LogLevel::WARNING], |
| 48 | + [40, LogLevel::ERROR], |
| 49 | + [41, LogLevel::ERROR], |
| 50 | + [49, LogLevel::ERROR], |
| 51 | + [50, LogLevel::CRITICAL], |
| 52 | + [51, LogLevel::CRITICAL], |
| 53 | + [59, LogLevel::CRITICAL], |
| 54 | + [60, LogLevel::ALERT], |
| 55 | + [61, LogLevel::ALERT], |
| 56 | + [69, LogLevel::ALERT], |
| 57 | + [70, LogLevel::EMERGENCY], |
| 58 | + [71, LogLevel::EMERGENCY], |
| 59 | + [79, LogLevel::EMERGENCY], |
| 60 | + ]; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @dataProvider dataValidLevels |
| 65 | + * @param float $durationMs |
| 66 | + * @param string | null $expected |
| 67 | + */ |
| 68 | + public function testValidLevels( |
| 69 | + float $durationMs, |
| 70 | + ?string $expected |
| 71 | + ): void { |
| 72 | + $actual = $this->sut->getApplicableLogLevel($durationMs / 1000); |
| 73 | + |
| 74 | + self::assertSame($expected, $actual); |
| 75 | + } |
| 76 | + |
| 77 | + public function testInvalidLogLevel(): void |
| 78 | + { |
| 79 | + self::expectException(InvalidArgumentException::class); |
| 80 | + self::expectExceptionMessage('invalid LogLevel detected: "InvalidLevel", please choose from: "' . print_r([ |
| 81 | + 'EMERGENCY' => 'emergency', |
| 82 | + 'ALERT' => 'alert', |
| 83 | + 'CRITICAL' => 'critical', |
| 84 | + 'ERROR' => 'error', |
| 85 | + 'WARNING' => 'warning', |
| 86 | + 'NOTICE' => 'notice', |
| 87 | + 'INFO' => 'info', |
| 88 | + 'DEBUG' => 'debug', |
| 89 | + ], true) . '"'); |
| 90 | + |
| 91 | + new LogLevelConfiguration([ |
| 92 | + 'InvalidLevel' => 1000, |
| 93 | + ]); |
| 94 | + } |
| 95 | +} |
0 commit comments