Passed
Push — master ( f5a705...0bf991 )
by Timothy
01:08 queued 11s
created

testFallbackToDefaultLogLevelWhenNoThresholdIsReached()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 9.7
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
    /**
21
     * @var TestLogger
22
     */
23
    private $logger;
24
    
25
    /** @var string */
26
    private $sql = 'SELECT * FROM users WHERE id = :id';
27
28
    public function testLogLevel() : void
29
    {
30
        $defaultLogLevel = LogLevel::DEBUG;
31
        $logLevelAfterReachingThreshold = LogLevel::INFO;
32
        $thresholdInMilliseconds = 25;
33
        $psrSqlLoggerConfigurableLogLevels = new PsrSqlLoggerConfigurableLogLevels(
34
            $this->logger,
35
            new LogLevelConfiguration([
36
                $logLevelAfterReachingThreshold => $thresholdInMilliseconds,
37
            ]),
38
            $defaultLogLevel
39
        );
40
41
        $psrSqlLoggerConfigurableLogLevels->startQuery($this->sql);
42
        $psrSqlLoggerConfigurableLogLevels->stopQuery();
43
44
        self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(0)->level);
45
        //No threshold is reached yet: default log level should be used
46
        self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(1)->level);
47
48
        $psrSqlLoggerConfigurableLogLevels->startQuery($this->sql);
49
        usleep($thresholdInMilliseconds * 1000); //Sleep to simulate query execution and reach the threshold
50
        $psrSqlLoggerConfigurableLogLevels->stopQuery();
51
52
        self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(2)->level);
53
        self::assertSame($logLevelAfterReachingThreshold, (string) $this->getRecordByIndex(3)->level);
54
    }
55
56
    private function getRecordByIndex(int $index): stdClass
57
    {
58
        $record = $this->logger->records[$index];
59
60
        self::assertInternalType('array', $record);
61
62
        return (object) $record;
63
    }
64
65 View Code Duplication
    public function testFallbackToDefaultLogLevel() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
    {
67
        $defaultLogLevel = LogLevel::CRITICAL;
68
        $psrSqlLoggerConfigurableLogLevels = new PsrSqlLoggerConfigurableLogLevels(
69
            $this->logger,
70
            new LogLevelConfiguration([]),
71
            $defaultLogLevel
72
        );
73
74
        $psrSqlLoggerConfigurableLogLevels->startQuery($this->sql);
75
        $psrSqlLoggerConfigurableLogLevels->stopQuery();
76
77
        self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(0)->level);
78
        self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(1)->level);
79
    }
80
81 View Code Duplication
    public function testFallbackToDefaultLogLevelWhenNoThresholdIsReached() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
    {
83
        $defaultLogLevel = LogLevel::DEBUG;
84
        $psrSqlLoggerConfigurableLogLevels = new PsrSqlLoggerConfigurableLogLevels(
85
            $this->logger,
86
            new LogLevelConfiguration([
87
                LogLevel::CRITICAL => 1000 * 60, //Use a huge threshold of one minute which should never be reached
88
            ]),
89
            $defaultLogLevel
90
        );
91
92
        $psrSqlLoggerConfigurableLogLevels->startQuery($this->sql);
93
        $psrSqlLoggerConfigurableLogLevels->stopQuery();
94
95
        self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(0)->level);
96
        self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(1)->level);
97
    }
98
99
    public function testInvalidConfiguration() : void
100
    {
101
        $this->expectException(TypeError::class);
102
        $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...
103
            $this->logger,
104
            new LogLevelConfiguration([
105
                0.12345 => LogLevel::DEBUG, //Inverted key / value tuple
106
            ])
107
        );
108
    }
109
110
    public function testInvalidLogLevelUsedInConfiguration() : void
111
    {
112
        $this->expectException(InvalidArgumentException::class);
113
        $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...
114
            $this->logger,
115
            new LogLevelConfiguration([
116
                'SOME_INVALID_LOG_LEVEL' => 100,
117
            ])
118
        );
119
    }
120
121
    protected function setUp()
122
    {
123
        $this->logger = new TestLogger();
124
    }
125
}
126