|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace TheCodingMachine\TDBM\Utils\Logs; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Psr\Log\LoggerInterface; |
| 7 | +use Psr\Log\LogLevel; |
| 8 | + |
| 9 | +class LevelFilterTest extends TestCase |
| 10 | +{ |
| 11 | + public function testIsPsrLog() |
| 12 | + { |
| 13 | + $levelFilter = new LevelFilter($this->getMockLoggerInterface(), LogLevel::DEBUG); |
| 14 | + $this->assertInstanceOf('\Psr\Log\LoggerInterface', $levelFilter); |
| 15 | + } |
| 16 | + |
| 17 | + public function testLog() |
| 18 | + { |
| 19 | + $loggerInterface = $this->getMockLoggerInterface(); |
| 20 | + |
| 21 | + // We expect this test to call the loggerInterface twice to log the ERROR and CRITICAL messages |
| 22 | + // and to ignore the WARNING log message |
| 23 | + $loggerInterface->expects($this->exactly(2)) |
| 24 | + ->method('log') |
| 25 | + ->withConsecutive( |
| 26 | + [LogLevel::ERROR], |
| 27 | + [LogLevel::CRITICAL] |
| 28 | + ); |
| 29 | + |
| 30 | + $levelFilter = new LevelFilter($loggerInterface, LogLevel::ERROR); |
| 31 | + |
| 32 | + // Log message with equal priority to the level filter (should be get logged) |
| 33 | + $levelFilter->log(LogLevel::ERROR, 'TEST ERROR MESSAGE'); |
| 34 | + |
| 35 | + // Log message with higher priority than the level filter (should be logged) |
| 36 | + $levelFilter->log(LogLevel::CRITICAL, 'TEST CRITICAL MESSAGE'); |
| 37 | + |
| 38 | + // Log message with lower priority than the level filter (should not be logged) |
| 39 | + $levelFilter->log(LogLevel::WARNING, 'TEST WARNING MESSAGE'); |
| 40 | + } |
| 41 | + |
| 42 | + public function testInvalidConstructorLogLevel() |
| 43 | + { |
| 44 | + // We expect an exception to be thrown when specifying an invalid logging level in the constructor |
| 45 | + $this->expectException('\Psr\Log\InvalidArgumentException'); |
| 46 | + $levelFilter = new LevelFilter($this->getMockLoggerInterface(), 'InvalidLogLevel'); |
| 47 | + } |
| 48 | + |
| 49 | + public function testInvalidLogLogLevel() |
| 50 | + { |
| 51 | + // We expect an exception to be thrown when specifying an invalid logging level in the log method parameter |
| 52 | + $levelFilter = new LevelFilter($this->getMockLoggerInterface(), LogLevel::DEBUG); |
| 53 | + |
| 54 | + $this->expectException('\Psr\Log\InvalidArgumentException'); |
| 55 | + $levelFilter->log('InvalidLogLevel', 'TEST LOG MESSAGE'); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @return LoggerInterface |
| 60 | + */ |
| 61 | + protected function getMockLoggerInterface() |
| 62 | + { |
| 63 | + $loggerInterface = $this->createMock('\Psr\Log\LoggerInterface'); |
| 64 | + return $loggerInterface; |
| 65 | + } |
| 66 | +} |
0 commit comments