Completed
Push — master ( 36c690...2340c9 )
by David
33s queued 30s
created

LevelFilterTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 20
c 1
b 0
f 0
dl 0
loc 56
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testLog() 0 23 1
A testInvalidConstructorLogLevel() 0 5 1
A testInvalidLogLogLevel() 0 7 1
A getMockLoggerInterface() 0 4 1
A testIsPsrLog() 0 4 1
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))
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Psr\Log\LoggerInterface. It seems like you code against a sub-type of Psr\Log\LoggerInterface such as Psr\Log\Test\TestLogger. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
        $loggerInterface->/** @scrutinizer ignore-call */ 
24
                          expects($this->exactly(2))
Loading history...
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');
0 ignored issues
show
Unused Code introduced by
The assignment to $levelFilter is dead and can be removed.
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $loggerInterface returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the documented return type Psr\Log\LoggerInterface.
Loading history...
65
    }
66
}
67