1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AbacaphiliacTest\Doctrine; |
4
|
|
|
|
5
|
|
|
use Abacaphiliac\Doctrine\PsrSqlParamsLogger; |
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\PsrSqlParamsLogger |
13
|
|
|
*/ |
14
|
|
|
class PsrSqlParamsLoggerTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** @var PsrSqlParamsLogger */ |
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 PsrSqlParamsLogger($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::assertInstanceOf(\stdClass::class, $log); |
60
|
|
|
self::assertSame(LogLevel::INFO, (string) $log->level); |
61
|
|
|
self::assertSame('Query started', (string) $log->message); |
62
|
|
|
self::assertNotEmpty($log->context['query_id']); |
63
|
|
|
self::assertSame($this->sql, $log->context['sql']); |
64
|
|
|
self::assertSame([':id' => 1234], $log->context['params']); |
65
|
|
|
self::assertSame([':id' => \PDO::PARAM_INT], $log->context['types']); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
View Code Duplication |
public function testLogsDuration() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
self::assertCount(0, $this->logger->records); |
|
|
|
|
71
|
|
|
|
72
|
|
|
$this->sut->startQuery( |
73
|
|
|
$this->sql, |
74
|
|
|
[ |
75
|
|
|
':id' => 1234, |
76
|
|
|
], |
77
|
|
|
[ |
78
|
|
|
':id' => \PDO::PARAM_INT, |
79
|
|
|
] |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$this->sut->stopQuery(); |
83
|
|
|
|
84
|
|
|
self::assertCount(2, $this->logger->records); |
|
|
|
|
85
|
|
|
|
86
|
|
|
$log = $this->getRecordByIndex(1); |
87
|
|
|
|
88
|
|
|
self::assertInstanceOf(\stdClass::class, $log); |
89
|
|
|
self::assertSame(LogLevel::INFO, (string) $log->level); |
90
|
|
|
self::assertSame('Query finished', (string) $log->message); |
91
|
|
|
self::assertNotEmpty($log->context['query_id']); |
92
|
|
|
self::assertIsFloat($log->context['start']); |
93
|
|
|
self::assertIsFloat($log->context['stop']); |
94
|
|
|
self::assertIsFloat($log->context['duration_s']); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
View Code Duplication |
public function testSharedQueryId() |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
self::assertCount(0, $this->logger->records); |
|
|
|
|
100
|
|
|
|
101
|
|
|
$this->sut->startQuery( |
102
|
|
|
$this->sql, |
103
|
|
|
[ |
104
|
|
|
':id' => 1234, |
105
|
|
|
], |
106
|
|
|
[ |
107
|
|
|
':id' => \PDO::PARAM_INT, |
108
|
|
|
] |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$this->sut->stopQuery(); |
112
|
|
|
|
113
|
|
|
self::assertCount(2, $this->logger->records); |
|
|
|
|
114
|
|
|
|
115
|
|
|
$startLog = $this->getRecordByIndex(0); |
116
|
|
|
|
117
|
|
|
self::assertInstanceOf(\stdClass::class, $startLog); |
118
|
|
|
|
119
|
|
|
$queryId = $startLog->context['query_id']; |
120
|
|
|
self::assertNotEmpty($queryId); |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
$stopLog = $this->getRecordByIndex(1); |
124
|
|
|
|
125
|
|
|
self::assertInstanceOf(\stdClass::class, $stopLog); |
126
|
|
|
self::assertSame($queryId, $stopLog->context['query_id']); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testQueryIdChanges() |
130
|
|
|
{ |
131
|
|
|
self::assertCount(0, $this->logger->records); |
|
|
|
|
132
|
|
|
|
133
|
|
|
$this->sut->startQuery( |
134
|
|
|
$this->sql, |
135
|
|
|
[ |
136
|
|
|
':id' => 1234, |
137
|
|
|
], |
138
|
|
|
[ |
139
|
|
|
':id' => \PDO::PARAM_INT, |
140
|
|
|
] |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
$this->sut->startQuery( |
144
|
|
|
$this->sql, |
145
|
|
|
[ |
146
|
|
|
':id' => 2345, |
147
|
|
|
], |
148
|
|
|
[ |
149
|
|
|
':id' => \PDO::PARAM_INT, |
150
|
|
|
] |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
self::assertCount(2, $this->logger->records); |
|
|
|
|
154
|
|
|
|
155
|
|
|
$firstLog = $this->getRecordByIndex(0); |
156
|
|
|
|
157
|
|
|
self::assertInstanceOf(\stdClass::class, $firstLog); |
158
|
|
|
|
159
|
|
|
$queryId = $firstLog->context['query_id']; |
160
|
|
|
self::assertNotEmpty($queryId); |
161
|
|
|
|
162
|
|
|
$secondLog = $this->getRecordByIndex(1); |
163
|
|
|
|
164
|
|
|
self::assertInstanceOf(\stdClass::class, $secondLog); |
165
|
|
|
self::assertNotEmpty($secondLog->context['query_id']); |
166
|
|
|
self::assertNotEquals($queryId, $secondLog->context['query_id']); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function testInvalidLogLevel() |
170
|
|
|
{ |
171
|
|
|
self:: expectException(\InvalidArgumentException::class); |
172
|
|
|
|
173
|
|
|
new PsrSqlParamsLogger(new NullLogger(), 'InvalidLevel'); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
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: