|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AbacaphiliacTest\Doctrine; |
|
4
|
|
|
|
|
5
|
|
|
use Abacaphiliac\Doctrine\LogLevelConfiguration; |
|
6
|
|
|
use Abacaphiliac\Doctrine\PsrSqlLoggerConfigurableLogLevels; |
|
7
|
|
|
use Psr\Log\Test\TestLogger; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
use PHPUnit\Framework\TestCase; |
|
10
|
|
|
use Psr\Log\LogLevel; |
|
11
|
|
|
use TypeError; |
|
12
|
|
|
use stdClass; |
|
13
|
|
|
use function usleep; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @covers \Abacaphiliac\Doctrine\PsrSqlLogger |
|
17
|
|
|
*/ |
|
18
|
|
|
class PsrSqlLoggerConfigurableLogLevelsTest extends TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var PsrSqlLoggerConfigurableLogLevels */ |
|
21
|
|
|
private $sut; |
|
22
|
|
|
|
|
23
|
|
|
/** @var TestLogger */ |
|
24
|
|
|
private $logger; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
private $sql = 'SELECT * FROM users WHERE id = :id'; |
|
28
|
|
|
|
|
29
|
|
|
public function testLogLevel() : void |
|
30
|
|
|
{ |
|
31
|
|
|
$this->sut->startQuery($this->sql); |
|
32
|
|
|
$this->sut->stopQuery(); |
|
33
|
|
|
|
|
34
|
|
|
self::assertSame(LogLevel::DEBUG, (string) $this->getRecordByIndex(0)->level); |
|
35
|
|
|
self::assertSame(LogLevel::INFO, (string) $this->getRecordByIndex(1)->level); |
|
36
|
|
|
|
|
37
|
|
|
$this->sut->startQuery($this->sql); |
|
38
|
|
|
usleep(50 * 1000); //Sleep 50 milliseconds to simulate query execution |
|
39
|
|
|
$this->sut->stopQuery(); |
|
40
|
|
|
|
|
41
|
|
|
self::assertSame(LogLevel::DEBUG, (string) $this->getRecordByIndex(2)->level); |
|
42
|
|
|
self::assertSame(LogLevel::NOTICE, (string) $this->getRecordByIndex(3)->level); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private function getRecordByIndex(int $index): stdClass |
|
46
|
|
|
{ |
|
47
|
|
|
$record = $this->logger->records[$index]; |
|
48
|
|
|
|
|
49
|
|
|
self::assertInternalType('array', $record); |
|
50
|
|
|
|
|
51
|
|
|
return (object) $record; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function testFallbackToDefaultLogLevel() : void |
|
55
|
|
|
{ |
|
56
|
|
|
$defaultLogLevel = LogLevel::CRITICAL; |
|
57
|
|
|
$psrSqlLoggerConfigurableLogLevels = new PsrSqlLoggerConfigurableLogLevels( |
|
58
|
|
|
$this->logger, |
|
59
|
|
|
new LogLevelConfiguration([]), |
|
60
|
|
|
$defaultLogLevel |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
$psrSqlLoggerConfigurableLogLevels->startQuery($this->sql); |
|
64
|
|
|
$psrSqlLoggerConfigurableLogLevels->stopQuery(); |
|
65
|
|
|
|
|
66
|
|
|
self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(0)->level); |
|
67
|
|
|
self::assertSame($defaultLogLevel, (string) $this->getRecordByIndex(1)->level); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function testInvalidConfiguration() : void |
|
71
|
|
|
{ |
|
72
|
|
|
$this->expectException(TypeError::class); |
|
73
|
|
|
$loggerWhichWillFailToInitialize = new PsrSqlLoggerConfigurableLogLevels( |
|
|
|
|
|
|
74
|
|
|
$this->logger, |
|
75
|
|
|
new LogLevelConfiguration([ |
|
76
|
|
|
0.12345 => LogLevel::DEBUG, //Inverted key / value tuple |
|
77
|
|
|
]) |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testInvalidLogLevelUsedInConfiration() : void |
|
82
|
|
|
{ |
|
83
|
|
|
$this->expectException(InvalidArgumentException::class); |
|
84
|
|
|
$loggerWhichWillFailToInitialize = new PsrSqlLoggerConfigurableLogLevels( |
|
|
|
|
|
|
85
|
|
|
$this->logger, |
|
86
|
|
|
new LogLevelConfiguration([ |
|
87
|
|
|
'SOME_INVALID_LOG_LEVEL' => 100, |
|
88
|
|
|
]) |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function setUp() |
|
93
|
|
|
{ |
|
94
|
|
|
$this->logger = new TestLogger(); |
|
95
|
|
|
$this->sut = new PsrSqlLoggerConfigurableLogLevels( |
|
96
|
|
|
$this->logger, |
|
97
|
|
|
new LogLevelConfiguration([ |
|
98
|
|
|
LogLevel::INFO => 0, |
|
99
|
|
|
LogLevel::NOTICE => 50, |
|
100
|
|
|
LogLevel::WARNING => 100, |
|
101
|
|
|
LogLevel::CRITICAL => 500 |
|
102
|
|
|
]), |
|
103
|
|
|
LogLevel::DEBUG |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.