1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AbacaphiliacTest\Doctrine; |
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 PsrSqlLoggerTest 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
|
|
|
|
30
|
|
|
$this->sut = new PsrSqlLogger($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::assertSame(LogLevel::INFO, (string) $log->level); |
65
|
|
|
self::assertSame('Query started', (string) $log->message); |
66
|
|
|
self::assertNotEmpty($log->context->get('query_id')); |
67
|
|
|
self::assertSame($this->sql, $log->context->get('sql')); |
68
|
|
|
self::assertSame([':id' => \PDO::PARAM_INT], $log->context->get('types')); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
public function testLogsDuration() |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
self::assertCount(0, $this->logger->log); |
74
|
|
|
|
75
|
|
|
$this->sut->startQuery( |
76
|
|
|
$this->sql, |
77
|
|
|
[ |
78
|
|
|
':id' => 1234, |
79
|
|
|
], |
80
|
|
|
[ |
81
|
|
|
':id' => \PDO::PARAM_INT, |
82
|
|
|
] |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
$this->sut->stopQuery(); |
86
|
|
|
|
87
|
|
|
self::assertCount(2, $this->logger->log); |
88
|
|
|
|
89
|
|
|
$log = $this->getRecordByIndex(1); |
90
|
|
|
|
91
|
|
|
self::assertSame(LogLevel::INFO, (string) $log->level); |
92
|
|
|
self::assertSame('Query finished', (string) $log->message); |
93
|
|
|
self::assertNotEmpty($log->context->get('query_id')); |
94
|
|
|
self::assertInternalType('float', $log->context->get('start')); |
95
|
|
|
self::assertInternalType('float', $log->context->get('stop')); |
96
|
|
|
self::assertInternalType('float', $log->context->get('duration_s')); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
public function testSharedQueryId() |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
self::assertCount(0, $this->logger->log); |
102
|
|
|
|
103
|
|
|
$this->sut->startQuery( |
104
|
|
|
$this->sql, |
105
|
|
|
[ |
106
|
|
|
':id' => 1234, |
107
|
|
|
], |
108
|
|
|
[ |
109
|
|
|
':id' => \PDO::PARAM_INT, |
110
|
|
|
] |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
$this->sut->stopQuery(); |
114
|
|
|
|
115
|
|
|
self::assertCount(2, $this->logger->log); |
116
|
|
|
|
117
|
|
|
$startLog = $this->getRecordByIndex(0); |
118
|
|
|
|
119
|
|
|
self::assertInstanceOf(Record::class, $startLog); |
120
|
|
|
|
121
|
|
|
$queryId = $startLog->context->get('query_id'); |
122
|
|
|
self::assertNotEmpty($queryId); |
123
|
|
|
|
124
|
|
|
$stopLog = $this->getRecordByIndex(1); |
125
|
|
|
|
126
|
|
|
self::assertSame($queryId, $stopLog->context->get('query_id')); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testQueryIdChanges() |
130
|
|
|
{ |
131
|
|
|
self::assertCount(0, $this->logger->log); |
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->log); |
154
|
|
|
|
155
|
|
|
$firstLog = $this->getRecordByIndex(0); |
156
|
|
|
|
157
|
|
|
$queryId = $firstLog->context->get('query_id'); |
158
|
|
|
self::assertNotEmpty($queryId); |
159
|
|
|
|
160
|
|
|
$secondLog = $this->getRecordByIndex(1); |
161
|
|
|
|
162
|
|
|
self::assertNotEmpty($secondLog->context->get('query_id')); |
163
|
|
|
self::assertNotEquals($queryId, $secondLog->context->get('query_id')); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @expectedException \InvalidArgumentException |
168
|
|
|
*/ |
169
|
|
|
public function testInvalidLogLevel() |
170
|
|
|
{ |
171
|
|
|
new PsrSqlLogger(new NullLogger(), 'InvalidLevel'); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
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.