|
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(): void |
|
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::assertIsArray($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() |
|
|
|
|
|
|
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::assertIsFloat($log->context['start']); |
|
90
|
|
|
self::assertIsFloat($log->context['stop']); |
|
91
|
|
|
self::assertIsFloat($log->context['duration_s']); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
View Code Duplication |
public function testSharedQueryId() |
|
|
|
|
|
|
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
|
|
|
public function testInvalidLogLevel() |
|
162
|
|
|
{ |
|
163
|
|
|
self::expectException(\InvalidArgumentException::class); |
|
164
|
|
|
|
|
165
|
|
|
new PsrSqlLogger(new NullLogger(), 'InvalidLevel'); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: