Completed
Push — master ( 1475ae...f0640d )
by Timothy
16:29 queued 12:04
created

PsrSqlLoggerTest::testSharedQueryId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29

Duplication

Lines 29
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 29
loc 29
rs 9.456
c 0
b 0
f 0
1
<?php
2
3
namespace AbacaphiliacTest\Doctrine;
4
5
use Abacaphiliac\Doctrine\PsrSqlLogger;
6
use PHPUnit\Framework\TestCase;
7
use Psr\Log\LogLevel;
8
use Psr\Log\NullLogger;
9
use Psr\Log\Test\TestLogger;
10
11
/**
12
 * @covers \Abacaphiliac\Doctrine\PsrSqlLogger
13
 */
14
class PsrSqlLoggerTest extends TestCase
15
{
16
    /** @var PsrSqlLogger */
17
    private $sut;
18
19
    /** @var TestLogger */
20
    private $logger;
21
22
    /** @var string */
23
    private $sql = 'SELECT * FROM users WHERE id = :id';
24
25
    protected function setUp()
26
    {
27
        $this->logger = new TestLogger();
28
29
        $this->sut = new PsrSqlLogger($this->logger);
30
    }
31
32
    private function getRecordByIndex(int $index): \stdClass
33
    {
34
        $record = $this->logger->records[$index];
35
36
        self::assertInternalType('array', $record);
37
38
        return (object) $record;
39
    }
40
41
    public function testLogsQuery()
42
    {
43
        self::assertCount(0, $this->logger->records);
44
45
        $this->sut->startQuery(
46
            $this->sql,
47
            [
48
                ':id' => 1234,
49
            ],
50
            [
51
                ':id' => \PDO::PARAM_INT,
52
            ]
53
        );
54
55
        self::assertCount(1, $this->logger->records);
56
57
        $log = $this->getRecordByIndex(0);
58
59
        self::assertSame(LogLevel::INFO, (string) $log->level);
60
        self::assertSame('Query started', (string) $log->message);
61
        self::assertNotEmpty($log->context['query_id']);
62
        self::assertSame($this->sql, $log->context['sql']);
63
        self::assertSame([':id' => \PDO::PARAM_INT], $log->context['types']);
64
    }
65
66 View Code Duplication
    public function testLogsDuration()
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...
67
    {
68
        self::assertCount(0, $this->logger->records);
69
70
        $this->sut->startQuery(
71
            $this->sql,
72
            [
73
                ':id' => 1234,
74
            ],
75
            [
76
                ':id' => \PDO::PARAM_INT,
77
            ]
78
        );
79
80
        $this->sut->stopQuery();
81
82
        self::assertCount(2, $this->logger->records);
83
84
        $log = $this->getRecordByIndex(1);
85
86
        self::assertSame(LogLevel::INFO, (string) $log->level);
87
        self::assertSame('Query finished', (string) $log->message);
88
        self::assertNotEmpty($log->context['query_id']);
89
        self::assertInternalType('float', $log->context['start']);
90
        self::assertInternalType('float', $log->context['stop']);
91
        self::assertInternalType('float', $log->context['duration_s']);
92
    }
93
94 View Code Duplication
    public function testSharedQueryId()
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...
95
    {
96
        self::assertCount(0, $this->logger->records);
97
98
        $this->sut->startQuery(
99
            $this->sql,
100
            [
101
                ':id' => 1234,
102
            ],
103
            [
104
                ':id' => \PDO::PARAM_INT,
105
            ]
106
        );
107
108
        $this->sut->stopQuery();
109
110
        self::assertCount(2, $this->logger->records);
111
112
        $startLog = $this->getRecordByIndex(0);
113
114
        self::assertInstanceOf(\stdClass::class, $startLog);
115
116
        $queryId = $startLog->context['query_id'];
117
        self::assertNotEmpty($queryId);
118
119
        $stopLog = $this->getRecordByIndex(1);
120
121
        self::assertSame($queryId, $stopLog->context['query_id']);
122
    }
123
124
    public function testQueryIdChanges()
125
    {
126
        self::assertCount(0, $this->logger->records);
127
128
        $this->sut->startQuery(
129
            $this->sql,
130
            [
131
                ':id' => 1234,
132
            ],
133
            [
134
                ':id' => \PDO::PARAM_INT,
135
            ]
136
        );
137
138
        $this->sut->startQuery(
139
            $this->sql,
140
            [
141
                ':id' => 2345,
142
            ],
143
            [
144
                ':id' => \PDO::PARAM_INT,
145
            ]
146
        );
147
148
        self::assertCount(2, $this->logger->records);
149
150
        $firstLog = $this->getRecordByIndex(0);
151
152
        $queryId = $firstLog->context['query_id'];
153
        self::assertNotEmpty($queryId);
154
155
        $secondLog = $this->getRecordByIndex(1);
156
157
        self::assertNotEmpty($secondLog->context['query_id']);
158
        self::assertNotEquals($queryId, $secondLog->context['query_id']);
159
    }
160
161
    /**
162
     * @expectedException \InvalidArgumentException
163
     */
164
    public function testInvalidLogLevel()
165
    {
166
        new PsrSqlLogger(new NullLogger(), 'InvalidLevel');
167
    }
168
}
169