forked from symfony/monolog-bundle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LoggerChannelPassTest.php
243 lines (192 loc) · 9.85 KB
/
LoggerChannelPassTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Compiler;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
class LoggerChannelPassTest extends TestCase
{
public function testProcess()
{
$container = $this->getContainer();
$this->assertTrue($container->hasDefinition('monolog.logger.test'), '->process adds a logger service for tagged service');
$service = $container->getDefinition('test');
$this->assertEquals('monolog.logger.test', (string) $service->getArgument(1), '->process replaces the logger by the new one');
// pushHandlers for service "test"
$expected = array(
'test' => array('monolog.handler.a', 'monolog.handler.b', 'monolog.handler.c'),
'foo' => array('monolog.handler.b'),
'bar' => array('monolog.handler.b', 'monolog.handler.c'),
);
foreach ($expected as $serviceName => $handlers) {
$service = $container->getDefinition($serviceName);
$channel = $container->getDefinition((string) $service->getArgument(1));
$calls = $channel->getMethodCalls();
$this->assertCount(count($handlers), $calls);
foreach ($handlers as $i => $handler) {
list($methodName, $arguments) = $calls[$i];
$this->assertEquals('pushHandler', $methodName);
$this->assertCount(1, $arguments);
$this->assertEquals($handler, (string) $arguments[0]);
}
}
$this->assertNotNull($container->getDefinition('monolog.logger.manualchan'));
}
public function testProcessSetters()
{
$container = $this->getContainerWithSetter();
$this->assertTrue($container->hasDefinition('monolog.logger.test'), '->process adds a logger service for tagged service');
$service = $container->getDefinition('foo');
$calls = $service->getMethodCalls();
$this->assertEquals('monolog.logger.test', (string) $calls[0][1][0], '->process replaces the logger by the new one in setters');
}
public function testAutowiredLoggerArgumentsAreReplacedWithChannelLogger()
{
if (!\method_exists('Symfony\Component\DependencyInjection\Definition', 'getBindings')) {
$this->markTestSkipped('Need DependencyInjection 3.4+ to autowire channel logger.');
}
$container = $this->getFunctionalContainer();
$dummyService = $container->register('dummy_service', 'Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Compiler\DummyService')
->setAutowired(true)
->setPublic(true)
->addTag('monolog.logger', array('channel' => 'test'));
$container->compile();
$this->assertEquals('monolog.logger.test', (string) $dummyService->getArgument(0));
}
public function testAutowiredLoggerArgumentsAreReplacedWithChannelLoggerWhenAutoconfigured()
{
if (!\method_exists('Symfony\Component\DependencyInjection\Definition', 'getBindings')) {
$this->markTestSkipped('Need DependencyInjection 3.4+ to autowire channel logger.');
}
$container = $this->getFunctionalContainer();
$container->registerForAutoconfiguration('Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Compiler\DummyService')
->setProperty('fake', 'dummy');
$container->register('dummy_service', 'Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Compiler\DummyService')
->setAutowired(true)
->setAutoconfigured(true)
->setPublic(true)
->addTag('monolog.logger', array('channel' => 'test'));
$container->compile();
$this->assertEquals('monolog.logger.test', (string) $container->getDefinition('dummy_service')->getArgument(0));
}
public function testAutowiredLoggerArgumentsAreNotReplacedWithChannelLoggerIfLoggerArgumentIsConfiguredExplicitly()
{
if (!\method_exists('Symfony\Component\DependencyInjection\Definition', 'getBindings')) {
$this->markTestSkipped('Need DependencyInjection 3.4+ to autowire channel logger.');
}
$container = $this->getFunctionalContainer();
$dummyService = $container->register('dummy_service', 'Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Compiler\DummyService')
->setAutowired(true)
->addArgument(new Reference('monolog.logger'))
->addTag('monolog.logger', array('channel' => 'test'));
$container->compile();
$this->assertEquals('monolog.logger', (string) $dummyService->getArgument(0));
}
public function testTagNotBreakingIfNoLogger()
{
$container = $this->getFunctionalContainer();
$dummyService = $container->register('dummy_service', 'stdClass')
->addTag('monolog.logger', array('channel' => 'test'));
$container->compile();
$this->assertEquals(array(), $dummyService->getArguments());
}
public function testChannelsConfigurationOptionSupportsAppChannel()
{
$container = $this->getFunctionalContainer();
$container->setParameter('monolog.additional_channels', array('app'));
$container->compile();
// the test ensures that the validation does not fail (i.e. it does not throw any exceptions)
$this->addToAssertionCount(1);
}
private function getContainer()
{
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config'));
$loader->load('monolog.xml');
$definition = $container->getDefinition('monolog.logger_prototype');
$container->set('monolog.handler.test', new Definition('%monolog.handler.null.class%', array(100, false)));
$definition->addMethodCall('pushHandler', array(new Reference('monolog.handler.test')));
// Handlers
$container->set('monolog.handler.a', new Definition('%monolog.handler.null.class%', array(100, false)));
$container->set('monolog.handler.b', new Definition('%monolog.handler.null.class%', array(100, false)));
$container->set('monolog.handler.c', new Definition('%monolog.handler.null.class%', array(100, false)));
// Channels
foreach (array('test', 'foo', 'bar') as $name) {
$service = new Definition('TestClass', array('false', new Reference('logger')));
$service->addTag('monolog.logger', array('channel' => $name));
$container->setDefinition($name, $service);
}
$container->setParameter('monolog.additional_channels', array('manualchan'));
$container->setParameter('monolog.handlers_to_channels', array(
'monolog.handler.a' => array(
'type' => 'inclusive',
'elements' => array('test')
),
'monolog.handler.b' => null,
'monolog.handler.c' => array(
'type' => 'exclusive',
'elements' => array('foo')
)
));
$container->getCompilerPassConfig()->setOptimizationPasses(array());
$container->getCompilerPassConfig()->setRemovingPasses(array());
$container->addCompilerPass(new LoggerChannelPass());
$container->compile();
return $container;
}
private function getContainerWithSetter()
{
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config'));
$loader->load('monolog.xml');
$definition = $container->getDefinition('monolog.logger_prototype');
$container->set('monolog.handler.test', new Definition('%monolog.handler.null.class%', array(100, false)));
$definition->addMethodCall('pushHandler', array(new Reference('monolog.handler.test')));
// Channels
$service = new Definition('TestClass');
$service->addTag('monolog.logger', array('channel' => 'test'));
$service->addMethodCall('setLogger', array(new Reference('logger')));
$container->setDefinition('foo', $service);
$container->setParameter('monolog.additional_channels', array('manualchan'));
$container->setParameter('monolog.handlers_to_channels', array());
$container->getCompilerPassConfig()->setOptimizationPasses(array());
$container->getCompilerPassConfig()->setRemovingPasses(array());
$container->addCompilerPass(new LoggerChannelPass());
$container->compile();
return $container;
}
/**
* @return ContainerBuilder
*/
private function getFunctionalContainer()
{
$container = new ContainerBuilder();
$container->setParameter('monolog.additional_channels', array());
$container->setParameter('monolog.handlers_to_channels', array());
$container->setParameter('monolog.use_microseconds', true);
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config'));
$loader->load('monolog.xml');
$container->addCompilerPass(new LoggerChannelPass());
// disable removing passes to be able to inspect the container before all the inlining optimizations
$container->getCompilerPassConfig()->setRemovingPasses(array());
return $container;
}
}
class DummyService
{
public function __construct(LoggerInterface $logger)
{
}
}