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