1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AbacaphiliacTest\Doctrine; |
4
|
|
|
|
5
|
|
|
use Abacaphiliac\Doctrine\PsrSqlParamsLogger; |
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\PsrSqlParamsLogger |
14
|
|
|
*/ |
15
|
|
|
class PsrSqlParamsLoggerTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** @var PsrSqlParamsLogger */ |
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
|
|
|
|
30
|
|
|
$this->sut = new PsrSqlParamsLogger($this->logger); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param integer $index |
35
|
|
|
* @return Record |
36
|
|
|
*/ |
37
|
|
|
private function getRecordByIndex($index) |
38
|
|
|
{ |
39
|
|
|
$record = $this->logger->log[$index]; |
40
|
|
|
|
41
|
|
|
self::assertInstanceOf(Record::class, $record); |
42
|
|
|
|
43
|
|
|
return $record; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testLogsQuery() |
47
|
|
|
{ |
48
|
|
|
self::assertCount(0, $this->logger->log); |
49
|
|
|
|
50
|
|
|
$this->sut->startQuery( |
51
|
|
|
$this->sql, |
52
|
|
|
[ |
53
|
|
|
':id' => 1234, |
54
|
|
|
], |
55
|
|
|
[ |
56
|
|
|
':id' => \PDO::PARAM_INT, |
57
|
|
|
] |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
self::assertCount(1, $this->logger->log); |
61
|
|
|
|
62
|
|
|
$log = $this->getRecordByIndex(0); |
63
|
|
|
|
64
|
|
|
self::assertInstanceOf(Record::class, $log); |
65
|
|
|
self::assertSame(LogLevel::INFO, (string) $log->level); |
66
|
|
|
self::assertSame('Query started', (string) $log->message); |
67
|
|
|
self::assertNotEmpty($log->context->get('query_id')); |
68
|
|
|
self::assertSame($this->sql, $log->context->get('sql')); |
69
|
|
|
self::assertSame([':id' => 1234], $log->context->get('params')); |
70
|
|
|
self::assertSame([':id' => \PDO::PARAM_INT], $log->context->get('types')); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
View Code Duplication |
public function testLogsDuration() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
self::assertCount(0, $this->logger->log); |
76
|
|
|
|
77
|
|
|
$this->sut->startQuery( |
78
|
|
|
$this->sql, |
79
|
|
|
[ |
80
|
|
|
':id' => 1234, |
81
|
|
|
], |
82
|
|
|
[ |
83
|
|
|
':id' => \PDO::PARAM_INT, |
84
|
|
|
] |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
$this->sut->stopQuery(); |
88
|
|
|
|
89
|
|
|
self::assertCount(2, $this->logger->log); |
90
|
|
|
|
91
|
|
|
$log = $this->getRecordByIndex(1); |
92
|
|
|
|
93
|
|
|
self::assertInstanceOf(Record::class, $log); |
94
|
|
|
self::assertSame(LogLevel::INFO, (string) $log->level); |
95
|
|
|
self::assertSame('Query finished', (string) $log->message); |
96
|
|
|
self::assertNotEmpty($log->context->get('query_id')); |
97
|
|
|
self::assertInternalType('float', $log->context->get('start')); |
98
|
|
|
self::assertInternalType('float', $log->context->get('stop')); |
99
|
|
|
self::assertInternalType('float', $log->context->get('duration_s')); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
View Code Duplication |
public function testSharedQueryId() |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
self::assertCount(0, $this->logger->log); |
105
|
|
|
|
106
|
|
|
$this->sut->startQuery( |
107
|
|
|
$this->sql, |
108
|
|
|
[ |
109
|
|
|
':id' => 1234, |
110
|
|
|
], |
111
|
|
|
[ |
112
|
|
|
':id' => \PDO::PARAM_INT, |
113
|
|
|
] |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
$this->sut->stopQuery(); |
117
|
|
|
|
118
|
|
|
self::assertCount(2, $this->logger->log); |
119
|
|
|
|
120
|
|
|
$startLog = $this->getRecordByIndex(0); |
121
|
|
|
|
122
|
|
|
self::assertInstanceOf(Record::class, $startLog); |
123
|
|
|
|
124
|
|
|
$queryId = $startLog->context->get('query_id'); |
125
|
|
|
self::assertNotEmpty($queryId); |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
$stopLog = $this->getRecordByIndex(1); |
129
|
|
|
|
130
|
|
|
self::assertInstanceOf(Record::class, $stopLog); |
131
|
|
|
self::assertSame($queryId, $stopLog->context->get('query_id')); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testQueryIdChanges() |
135
|
|
|
{ |
136
|
|
|
self::assertCount(0, $this->logger->log); |
137
|
|
|
|
138
|
|
|
$this->sut->startQuery( |
139
|
|
|
$this->sql, |
140
|
|
|
[ |
141
|
|
|
':id' => 1234, |
142
|
|
|
], |
143
|
|
|
[ |
144
|
|
|
':id' => \PDO::PARAM_INT, |
145
|
|
|
] |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
$this->sut->startQuery( |
149
|
|
|
$this->sql, |
150
|
|
|
[ |
151
|
|
|
':id' => 2345, |
152
|
|
|
], |
153
|
|
|
[ |
154
|
|
|
':id' => \PDO::PARAM_INT, |
155
|
|
|
] |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
self::assertCount(2, $this->logger->log); |
159
|
|
|
|
160
|
|
|
$firstLog = $this->getRecordByIndex(0); |
161
|
|
|
|
162
|
|
|
self::assertInstanceOf(Record::class, $firstLog); |
163
|
|
|
|
164
|
|
|
$queryId = $firstLog->context->get('query_id'); |
165
|
|
|
self::assertNotEmpty($queryId); |
166
|
|
|
|
167
|
|
|
$secondLog = $this->getRecordByIndex(1); |
168
|
|
|
|
169
|
|
|
self::assertInstanceOf(Record::class, $secondLog); |
170
|
|
|
self::assertNotEmpty($secondLog->context->get('query_id')); |
171
|
|
|
self::assertNotEquals($queryId, $secondLog->context->get('query_id')); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @expectedException \InvalidArgumentException |
176
|
|
|
*/ |
177
|
|
|
public function testInvalidLogLevel() |
178
|
|
|
{ |
179
|
|
|
new PsrSqlParamsLogger(new NullLogger(), 'InvalidLevel'); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
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.