1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Configuration; |
6
|
|
|
use Doctrine\DBAL\Logging\LoggerChain; |
7
|
|
|
use Doctrine\DBAL\Logging\SQLLogger; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
10
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
11
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
12
|
|
|
|
13
|
|
|
final class DoctrineDBALLoggerPassTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
public function testProcess() |
16
|
|
|
{ |
17
|
|
|
$container = $this->loadContainer('my-logger', MySQLLogger::class); |
18
|
|
|
|
19
|
|
|
self::assertEquals( |
20
|
|
|
[ |
21
|
|
|
[ |
22
|
|
|
'addLogger', [ |
23
|
|
|
new Reference('my-logger'), |
24
|
|
|
], |
25
|
|
|
], |
26
|
|
|
], |
27
|
|
|
$container->getDefinition('doctrine.dbal.logger.chain.tagged')->getMethodCalls() |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException |
33
|
|
|
* @expectedExceptionMessage The service "not-a-logger" tagged "doctrine.dbal.logger" must implement "Doctrine\DBAL\Logging\SQLLogger". |
34
|
|
|
*/ |
35
|
|
|
public function testProcessWithoutSQLLoger() |
36
|
|
|
{ |
37
|
|
|
$this->loadContainer('not-a-logger', NotASQLLogger::class); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException |
42
|
|
|
* @expectedExceptionMessage The service "abstract-logger" tagged "doctrine.dbal.logger" must not be abstract. |
43
|
|
|
*/ |
44
|
|
|
public function testProcessWithAbstractLogger() |
45
|
|
|
{ |
46
|
|
|
$this->loadContainer('abstract-logger', AbstractSQLLogger::class); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private function loadContainer(string $loggerName, string $loggerClass): ContainerBuilder |
50
|
|
|
{ |
51
|
|
|
$configurationDefinition = new Definition(Configuration::class); |
52
|
|
|
|
53
|
|
|
$chainDefinition = new Definition(LoggerChain::class); |
54
|
|
|
|
55
|
|
|
$loggerDefinition = new Definition($loggerClass); |
56
|
|
|
$loggerDefinition->addTag('doctrine.dbal.logger'); |
57
|
|
|
|
58
|
|
|
$container = new ContainerBuilder(); |
59
|
|
|
$container->setParameter('doctrine.connections', [ |
60
|
|
|
'tagged' => 'doctrine.dbal.tagged_connection', |
61
|
|
|
]); |
62
|
|
|
$container->setDefinition('doctrine.dbal.tagged_connection.configuration', $configurationDefinition); |
63
|
|
|
$container->setDefinition('doctrine.dbal.logger.chain', $chainDefinition); |
64
|
|
|
$container->setDefinition($loggerName, $loggerDefinition); |
65
|
|
|
|
66
|
|
|
$compilerPass = new DoctrineDBALLoggerPass; |
67
|
|
|
$compilerPass->process($container); |
68
|
|
|
|
69
|
|
|
return $container; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
abstract class AbstractSQLLogger implements SQLLogger |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
public function startQuery($sql, array $params = null, array $types = null) |
76
|
|
|
{ |
77
|
|
|
// no-op |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function stopQuery() |
81
|
|
|
{ |
82
|
|
|
// no-op |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
final class MySQLLogger extends AbstractSQLLogger {} |
|
|
|
|
87
|
|
|
|
88
|
|
|
final class NotASQLLogger {} |
|
|
|
|
89
|
|
|
|
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.