1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AbacaphiliacTest\Doctrine; |
4
|
|
|
|
5
|
|
|
use Abacaphiliac\Doctrine\LogLevelConfiguration; |
6
|
|
|
use Abacaphiliac\Doctrine\PsrSqlLoggerConfigurableLogLevels; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use Psr\Log\Test\TestLogger; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use Psr\Log\LogLevel; |
12
|
|
|
use TypeError; |
13
|
|
|
use stdClass; |
14
|
|
|
use function usleep; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @covers \Abacaphiliac\Doctrine\PsrSqlLoggerConfigurableLogLevels |
18
|
|
|
*/ |
19
|
|
|
class PsrSqlLoggerConfigurableLogLevelsTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var TestLogger |
23
|
|
|
*/ |
24
|
|
|
private $logger; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
private $sql = 'SELECT * FROM users WHERE id = :id'; |
28
|
|
|
|
29
|
|
|
public function testLogLevel() : void |
30
|
|
|
{ |
31
|
|
|
$defaultLogLevel = LogLevel::DEBUG; |
32
|
|
|
$logLevelAfterReachingThreshold = LogLevel::INFO; |
33
|
|
|
$thresholdInMilliseconds = 25; |
34
|
|
|
$psrSqlLoggerConfigurableLogLevels = new PsrSqlLoggerConfigurableLogLevels( |
35
|
|
|
$this->logger, |
36
|
|
|
new LogLevelConfiguration([ |
37
|
|
|
$logLevelAfterReachingThreshold => $thresholdInMilliseconds, |
38
|
|
|
]), |
39
|
|
|
$defaultLogLevel |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
$psrSqlLoggerConfigurableLogLevels->startQuery($this->sql); |
43
|
|
|
$psrSqlLoggerConfigurableLogLevels->stopQuery(); |
44
|
|
|
|
45
|
|
|
self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(0)->level); |
46
|
|
|
//No threshold is reached yet: default log level should be used |
47
|
|
|
self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(1)->level); |
48
|
|
|
|
49
|
|
|
$psrSqlLoggerConfigurableLogLevels->startQuery($this->sql); |
50
|
|
|
usleep($thresholdInMilliseconds * 1000); //Sleep to simulate query execution and reach the threshold |
51
|
|
|
$psrSqlLoggerConfigurableLogLevels->stopQuery(); |
52
|
|
|
|
53
|
|
|
self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(2)->level); |
54
|
|
|
self::assertSame($logLevelAfterReachingThreshold, (string) $this->getRecordByIndex(3)->level); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function getRecordByIndex(int $index): stdClass |
58
|
|
|
{ |
59
|
|
|
$record = $this->logger->records[$index]; |
60
|
|
|
|
61
|
|
|
self::assertInternalType('array', $record); |
62
|
|
|
|
63
|
|
|
return (object) $record; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
public function testFallbackToDefaultLogLevel() : void |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$defaultLogLevel = LogLevel::CRITICAL; |
69
|
|
|
$psrSqlLoggerConfigurableLogLevels = new PsrSqlLoggerConfigurableLogLevels( |
70
|
|
|
$this->logger, |
71
|
|
|
new LogLevelConfiguration([]), |
72
|
|
|
$defaultLogLevel |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
$psrSqlLoggerConfigurableLogLevels->startQuery($this->sql); |
76
|
|
|
$psrSqlLoggerConfigurableLogLevels->stopQuery(); |
77
|
|
|
|
78
|
|
|
self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(0)->level); |
79
|
|
|
self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(1)->level); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
public function testFallbackToDefaultLogLevelWhenNoThresholdIsReached() : void |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$defaultLogLevel = LogLevel::DEBUG; |
85
|
|
|
$psrSqlLoggerConfigurableLogLevels = new PsrSqlLoggerConfigurableLogLevels( |
86
|
|
|
$this->logger, |
87
|
|
|
new LogLevelConfiguration([ |
88
|
|
|
LogLevel::CRITICAL => 1000 * 60, //Use a huge threshold of one minute which should never be reached |
89
|
|
|
]), |
90
|
|
|
$defaultLogLevel |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
$psrSqlLoggerConfigurableLogLevels->startQuery($this->sql); |
94
|
|
|
$psrSqlLoggerConfigurableLogLevels->stopQuery(); |
95
|
|
|
|
96
|
|
|
self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(0)->level); |
97
|
|
|
self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(1)->level); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testInvalidConfiguration() : void |
101
|
|
|
{ |
102
|
|
|
$this->expectException(TypeError::class); |
103
|
|
|
$loggerWhichWillFailToInitialize = new PsrSqlLoggerConfigurableLogLevels( |
|
|
|
|
104
|
|
|
$this->logger, |
105
|
|
|
new LogLevelConfiguration([ |
106
|
|
|
0.12345 => LogLevel::DEBUG, //Inverted key / value tuple |
107
|
|
|
]) |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function testInvalidLogLevelUsedInConfiguration() : void |
112
|
|
|
{ |
113
|
|
|
$this->expectException(InvalidArgumentException::class); |
114
|
|
|
$loggerWhichWillFailToInitialize = new PsrSqlLoggerConfigurableLogLevels( |
|
|
|
|
115
|
|
|
$this->logger, |
116
|
|
|
new LogLevelConfiguration([ |
117
|
|
|
'SOME_INVALID_LOG_LEVEL' => 100, |
118
|
|
|
]) |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function testInvalidDefaultLogLevel() : void |
123
|
|
|
{ |
124
|
|
|
$this->expectException(InvalidArgumentException::class); |
125
|
|
|
new PsrSqlLoggerConfigurableLogLevels( |
126
|
|
|
new class implements LoggerInterface { |
127
|
|
|
public function emergency($message, array $context = array()) |
128
|
|
|
{ |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function alert($message, array $context = array()) |
132
|
|
|
{ |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function critical($message, array $context = array()) |
136
|
|
|
{ |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function error($message, array $context = array()) |
140
|
|
|
{ |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function warning($message, array $context = array()) |
144
|
|
|
{ |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function notice($message, array $context = array()) |
148
|
|
|
{ |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function info($message, array $context = array()) |
152
|
|
|
{ |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function debug($message, array $context = array()) |
156
|
|
|
{ |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function log($level, $message, array $context = array()) |
160
|
|
|
{ |
161
|
|
|
} |
162
|
|
|
}, |
163
|
|
|
new LogLevelConfiguration([ |
164
|
|
|
]), |
165
|
|
|
'InvalidLevel' |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
protected function setUp() |
170
|
|
|
{ |
171
|
|
|
$this->logger = new TestLogger(); |
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.