Passed
Pull Request — master (#9)
by
unknown
02:05
created

PsrSqlLoggerConfigurableLogLevelsTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 48
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 11 1
A getRecordByIndex() 0 8 1
A testLogLevel() 0 15 1
1
<?php
2
3
namespace AbacaphiliacTest\test;
4
5
use Abacaphiliac\Doctrine\PsrSqlLogger;
6
use Gamez\Psr\Log\Record;
7
use Gamez\Psr\Log\TestLogger;
8
use PHPUnit\Framework\TestCase;
9
use Psr\Log\LogLevel;
10
use Psr\Log\NullLogger;
11
12
/**
13
 * @covers \Abacaphiliac\Doctrine\PsrSqlLogger
14
 */
15
class PsrSqlLoggerConfigurableLogLevelsTest extends TestCase
16
{
17
    /** @var PsrSqlLogger */
18
    private $sut;
19
20
    /** @var TestLogger */
21
    private $logger;
22
23
    /** @var string */
24
    private $sql = 'SELECT * FROM users WHERE id = :id';
25
26
    protected function setUp()
27
    {
28
        $this->logger = new TestLogger();
29
        $logLevelsForQueryDurationsInMilliseconds = [
30
            LogLevel::INFO => 0,
31
            LogLevel::NOTICE => 50,
32
            LogLevel::WARNING => 100,
33
            LogLevel::CRITICAL => 500
34
        ];
35
        $this->sut = new PsrSqlLogger($this->logger, LogLevel::DEBUG, $logLevelsForQueryDurationsInMilliseconds);
36
    }
37
38
    private function getRecordByIndex(int $index): Record
39
    {
40
        $record = $this->logger->log[$index];
41
42
        self::assertInstanceOf(Record::class, $record);
43
44
        return $record;
45
    }
46
47
    public function testLogLevel()
48
    {
49
        $this->sut->startQuery($this->sql);
50
        $this->sut->stopQuery();
51
52
        self::assertSame(LogLevel::DEBUG, (string) $this->getRecordByIndex(0)->level);
53
        self::assertSame(LogLevel::INFO, (string) $this->getRecordByIndex(1)->level);
54
55
        $this->sut->startQuery($this->sql);
56
        \usleep(50 * 1000); //Sleep 50 milliseconds to simulate query execution
57
        $this->sut->stopQuery();
58
59
        self::assertSame(LogLevel::DEBUG, (string) $this->getRecordByIndex(2)->level);
60
        self::assertSame(LogLevel::NOTICE, (string) $this->getRecordByIndex(3)->level);
61
    }
62
}
63