1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AbacaphiliacTest\Doctrine; |
4
|
|
|
|
5
|
|
|
use Abacaphiliac\Doctrine\PsrSqlLogger; |
6
|
|
|
use Doctrine\DBAL\Connection; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use Psr\Log\Test\TestLogger; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @covers \Abacaphiliac\Doctrine\PsrSqlLogger |
12
|
|
|
*/ |
13
|
|
|
class PsrSqlLoggerIntegrationTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** @var TestLogger */ |
16
|
|
|
private $logger; |
17
|
|
|
|
18
|
|
|
/** @var Connection */ |
19
|
|
|
private $connection; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @throws \Doctrine\DBAL\DBALException |
23
|
|
|
*/ |
24
|
|
|
protected function setUp(): void |
25
|
|
|
{ |
26
|
|
|
$this->logger = new TestLogger(); |
27
|
|
|
|
28
|
|
|
$config = new \Doctrine\DBAL\Configuration(); |
29
|
|
|
$config->setSQLLogger(new PsrSqlLogger($this->logger)); |
30
|
|
|
|
31
|
|
|
$this->connection = \Doctrine\DBAL\DriverManager::getConnection( |
32
|
|
|
[ |
33
|
|
|
'driver' => 'pdo_sqlite', |
34
|
|
|
'memory' => true, |
35
|
|
|
], |
36
|
|
|
$config |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private function getRecordByIndex(int $index): \stdClass |
41
|
|
|
{ |
42
|
|
|
$record = $this->logger->records[$index]; |
43
|
|
|
|
44
|
|
|
self::assertIsArray($record); |
45
|
|
|
|
46
|
|
|
return (object) $record; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testLogsQuery() |
50
|
|
|
{ |
51
|
|
|
self::assertCount(0, $this->logger->records); |
|
|
|
|
52
|
|
|
|
53
|
|
|
$schema = $this->connection->getSchemaManager(); |
54
|
|
|
|
55
|
|
|
// Generates 2 logs with a query_id: |
56
|
|
|
$schema->listTables(); |
57
|
|
|
|
58
|
|
|
self::assertCount(2, $this->logger->records); |
|
|
|
|
59
|
|
|
|
60
|
|
|
$log = $this->getRecordByIndex(0); |
61
|
|
|
|
62
|
|
|
$queryId = $log->context['query_id']; |
63
|
|
|
|
64
|
|
|
self::assertCount(2, \array_filter($this->logger->records, function ($record) use ($queryId) { |
|
|
|
|
65
|
|
|
return $record['context']['query_id'] === $queryId; |
66
|
|
|
})); |
67
|
|
|
self::assertSame('Query started', $this->getRecordByIndex(0)->message); |
68
|
|
|
self::assertSame('Query finished', $this->getRecordByIndex(1)->message); |
69
|
|
|
|
70
|
|
|
// Generates 2 more logs with a new query_id: |
71
|
|
|
$schema->listTables(); |
72
|
|
|
|
73
|
|
|
self::assertCount(4, $this->logger->records); |
|
|
|
|
74
|
|
|
|
75
|
|
|
self::assertCount(2, \array_filter($this->logger->records, function ($record) use ($queryId) { |
|
|
|
|
76
|
|
|
return $record['context']['query_id'] === $queryId; |
77
|
|
|
})); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
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: