Passed
Push — master ( f0640d...d81e81 )
by Timothy
56s queued 11s
created

testLogLevel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace AbacaphiliacTest\Doctrine;
4
5
use Abacaphiliac\Doctrine\LogLevelConfiguration;
6
use Abacaphiliac\Doctrine\PsrSqlLoggerConfigurableLogLevels;
7
use Psr\Log\Test\TestLogger;
8
use InvalidArgumentException;
9
use PHPUnit\Framework\TestCase;
10
use Psr\Log\LogLevel;
11
use TypeError;
12
use stdClass;
13
use function usleep;
14
15
/**
16
 * @covers \Abacaphiliac\Doctrine\PsrSqlLogger
17
 */
18
class PsrSqlLoggerConfigurableLogLevelsTest extends TestCase
19
{
20
    /** @var PsrSqlLoggerConfigurableLogLevels */
21
    private $sut;
22
23
    /** @var TestLogger */
24
    private $logger;
25
26
    /** @var string */
27
    private $sql = 'SELECT * FROM users WHERE id = :id';
28
29
    public function testLogLevel() : void
30
    {
31
        $this->sut->startQuery($this->sql);
32
        $this->sut->stopQuery();
33
34
        self::assertSame(LogLevel::DEBUG, (string) $this->getRecordByIndex(0)->level);
35
        self::assertSame(LogLevel::INFO, (string) $this->getRecordByIndex(1)->level);
36
37
        $this->sut->startQuery($this->sql);
38
        usleep(50 * 1000); //Sleep 50 milliseconds to simulate query execution
39
        $this->sut->stopQuery();
40
41
        self::assertSame(LogLevel::DEBUG, (string) $this->getRecordByIndex(2)->level);
42
        self::assertSame(LogLevel::NOTICE, (string) $this->getRecordByIndex(3)->level);
43
    }
44
45
    private function getRecordByIndex(int $index): stdClass
46
    {
47
        $record = $this->logger->records[$index];
48
49
        self::assertInternalType('array', $record);
50
51
        return (object) $record;
52
    }
53
54
    public function testFallbackToDefaultLogLevel() : void
55
    {
56
        $defaultLogLevel = LogLevel::CRITICAL;
57
        $psrSqlLoggerConfigurableLogLevels = new PsrSqlLoggerConfigurableLogLevels(
58
            $this->logger,
59
            new LogLevelConfiguration([]),
60
            $defaultLogLevel
61
        );
62
63
        $psrSqlLoggerConfigurableLogLevels->startQuery($this->sql);
64
        $psrSqlLoggerConfigurableLogLevels->stopQuery();
65
66
        self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(0)->level);
67
        self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(1)->level);
68
    }
69
70
    public function testInvalidConfiguration() : void
71
    {
72
        $this->expectException(TypeError::class);
73
        $loggerWhichWillFailToInitialize = new PsrSqlLoggerConfigurableLogLevels(
0 ignored issues
show
Unused Code introduced by
$loggerWhichWillFailToInitialize is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
74
            $this->logger,
75
            new LogLevelConfiguration([
76
                0.12345 => LogLevel::DEBUG, //Inverted key / value tuple
77
            ])
78
        );
79
    }
80
81
    public function testInvalidLogLevelUsedInConfiration() : void
82
    {
83
        $this->expectException(InvalidArgumentException::class);
84
        $loggerWhichWillFailToInitialize = new PsrSqlLoggerConfigurableLogLevels(
0 ignored issues
show
Unused Code introduced by
$loggerWhichWillFailToInitialize is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
85
            $this->logger,
86
            new LogLevelConfiguration([
87
                'SOME_INVALID_LOG_LEVEL' => 100,
88
            ])
89
        );
90
    }
91
92
    protected function setUp()
93
    {
94
        $this->logger = new TestLogger();
95
        $this->sut = new PsrSqlLoggerConfigurableLogLevels(
96
            $this->logger,
97
            new LogLevelConfiguration([
98
                LogLevel::INFO => 0,
99
                LogLevel::NOTICE => 50,
100
                LogLevel::WARNING => 100,
101
                LogLevel::CRITICAL => 500
102
            ]),
103
            LogLevel::DEBUG
104
        );
105
    }
106
}
107