1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoctrineORMModuleTest\Service; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use DoctrineORMModule\Service\SQLLoggerCollectorFactory; |
7
|
|
|
use Doctrine\DBAL\Logging\DebugStack; |
8
|
|
|
use Doctrine\ORM\Configuration as ORMConfiguration; |
9
|
|
|
use Laminas\ServiceManager\ServiceManager; |
10
|
|
|
|
11
|
|
|
class SQLLoggerCollectorFactoryTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ServiceManager |
15
|
|
|
*/ |
16
|
|
|
protected $services; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var SQLLoggerCollectorFactory |
20
|
|
|
*/ |
21
|
|
|
protected $factory; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* {@inheritDoc} |
25
|
|
|
*/ |
26
|
|
|
public function setUp() : void |
27
|
|
|
{ |
28
|
|
|
parent::setUp(); |
|
|
|
|
29
|
|
|
$this->services = new ServiceManager(); |
30
|
|
|
$this->factory = new SQLLoggerCollectorFactory('orm_default'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function testCreateSQLLoggerCollector() |
34
|
|
|
{ |
35
|
|
|
$configuration = new ORMConfiguration(); |
36
|
|
|
$this->services->setService('doctrine.configuration.orm_default', $configuration); |
37
|
|
|
$this->services->setService( |
38
|
|
|
'config', |
39
|
|
|
[ |
40
|
|
|
'doctrine' => [ |
41
|
|
|
'sql_logger_collector' => [ |
42
|
|
|
'orm_default' => [], |
43
|
|
|
], |
44
|
|
|
], |
45
|
|
|
] |
46
|
|
|
); |
47
|
|
|
$service = $this->factory->createService($this->services); |
48
|
|
|
$this->assertInstanceOf(\DoctrineORMModule\Collector\SQLLoggerCollector::class, $service); |
|
|
|
|
49
|
|
|
$this->assertInstanceOf(\Doctrine\DBAL\Logging\SQLLogger::class, $configuration->getSQLLogger()); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testCreateSQLLoggerWithCustomConfiguration() |
53
|
|
|
{ |
54
|
|
|
$configuration = new ORMConfiguration(); |
55
|
|
|
$this->services->setService('configuration_service_id', $configuration); |
56
|
|
|
$this->services->setService( |
57
|
|
|
'config', |
58
|
|
|
[ |
59
|
|
|
'doctrine' => [ |
60
|
|
|
'sql_logger_collector' => [ |
61
|
|
|
'orm_default' => [ |
62
|
|
|
'configuration' => 'configuration_service_id', |
63
|
|
|
], |
64
|
|
|
], |
65
|
|
|
], |
66
|
|
|
] |
67
|
|
|
); |
68
|
|
|
$this->factory->createService($this->services); |
69
|
|
|
$this->assertInstanceOf(\Doctrine\DBAL\Logging\SQLLogger::class, $configuration->getSQLLogger()); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testCreateSQLLoggerWithPreviousExistingLoggerChainsLoggers() |
73
|
|
|
{ |
74
|
|
|
$originalLogger = $this->createMock(\Doctrine\DBAL\Logging\SQLLogger::class); |
|
|
|
|
75
|
|
|
$originalLogger |
|
|
|
|
76
|
|
|
->expects($this->once()) |
|
|
|
|
77
|
|
|
->method('startQuery') |
78
|
|
|
->with($this->equalTo('test query')); |
|
|
|
|
79
|
|
|
$injectedLogger = $this->createMock(\Doctrine\DBAL\Logging\DebugStack::class); |
|
|
|
|
80
|
|
|
$injectedLogger |
|
|
|
|
81
|
|
|
->expects($this->once()) |
|
|
|
|
82
|
|
|
->method('startQuery') |
83
|
|
|
->with($this->equalTo('test query')); |
|
|
|
|
84
|
|
|
|
85
|
|
|
$configuration = new ORMConfiguration(); |
86
|
|
|
$configuration->setSQLLogger($originalLogger); |
87
|
|
|
$this->services->setService('doctrine.configuration.orm_default', $configuration); |
88
|
|
|
$this->services->setService('custom_logger', $injectedLogger); |
|
|
|
|
89
|
|
|
$this->services->setService( |
90
|
|
|
'config', |
91
|
|
|
[ |
92
|
|
|
'doctrine' => [ |
93
|
|
|
'sql_logger_collector' => [ |
94
|
|
|
'orm_default' => [ |
95
|
|
|
'sql_logger' => 'custom_logger', |
96
|
|
|
], |
97
|
|
|
], |
98
|
|
|
], |
99
|
|
|
] |
100
|
|
|
); |
101
|
|
|
$this->factory->createService($this->services); |
102
|
|
|
/* @var $logger \Doctrine\DBAL\Logging\SQLLogger */ |
103
|
|
|
$logger = $configuration->getSQLLogger(); |
104
|
|
|
$logger->startQuery('test query'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testCreateSQLLoggerWithCustomLogger() |
108
|
|
|
{ |
109
|
|
|
$configuration = new ORMConfiguration(); |
110
|
|
|
$logger = new DebugStack(); |
111
|
|
|
$this->services->setService('doctrine.configuration.orm_default', $configuration); |
112
|
|
|
$this->services->setService('logger_service_id', $logger); |
113
|
|
|
$this->services->setService( |
114
|
|
|
'config', |
115
|
|
|
[ |
116
|
|
|
'doctrine' => [ |
117
|
|
|
'sql_logger_collector' => [ |
118
|
|
|
'orm_default' => [ |
119
|
|
|
'sql_logger' => 'logger_service_id', |
120
|
|
|
], |
121
|
|
|
], |
122
|
|
|
], |
123
|
|
|
] |
124
|
|
|
); |
125
|
|
|
$this->factory->createService($this->services); |
126
|
|
|
$this->assertSame($logger, $configuration->getSQLLogger()); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testCreateSQLLoggerWithCustomName() |
130
|
|
|
{ |
131
|
|
|
$this->services->setService('doctrine.configuration.orm_default', new ORMConfiguration()); |
132
|
|
|
$this->services->setService( |
133
|
|
|
'config', |
134
|
|
|
[ |
135
|
|
|
'doctrine' => [ |
136
|
|
|
'sql_logger_collector' => [ |
137
|
|
|
'orm_default' => [ |
138
|
|
|
'name' => 'test_collector_name', |
139
|
|
|
], |
140
|
|
|
], |
141
|
|
|
], |
142
|
|
|
] |
143
|
|
|
); |
144
|
|
|
/* @var $service \DoctrineORMModule\Collector\SQLLoggerCollector */ |
145
|
|
|
$service = $this->factory->createService($this->services); |
146
|
|
|
$this->assertSame('doctrine.sql_logger_collector.test_collector_name', $service->getName()); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: