|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the Symfony package. |
|
5 * |
|
6 * (c) Fabien Potencier <fabien@symfony.com> |
|
7 * |
|
8 * For the full copyright and license information, please view the LICENSE |
|
9 * file that was distributed with this source code. |
|
10 */ |
|
11 |
|
12 namespace Symfony\Bundle\MonologBundle\Tests\DependencyInjection; |
|
13 |
|
14 use Symfony\Bundle\MonologBundle\Tests\TestCase; |
|
15 use Symfony\Bundle\MonologBundle\DependencyInjection\MonologExtension; |
|
16 use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
17 use Symfony\Component\DependencyInjection\Reference; |
|
18 |
|
19 class MonologExtensionTest extends TestCase |
|
20 { |
|
21 public function testLoadWithDefault() |
|
22 { |
|
23 $container = new ContainerBuilder(); |
|
24 $loader = new MonologExtension(); |
|
25 |
|
26 $loader->load(array(array('handlers' => array('main' => array('type' => 'stream')))), $container); |
|
27 $this->assertTrue($container->hasDefinition('monolog.logger')); |
|
28 $this->assertTrue($container->hasDefinition('monolog.handler.main')); |
|
29 |
|
30 $logger = $container->getDefinition('monolog.logger'); |
|
31 $this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.main'))); |
|
32 |
|
33 $handler = $container->getDefinition('monolog.handler.main'); |
|
34 $this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%'); |
|
35 $this->assertDICConstructorArguments($handler, array('%kernel.logs_dir%/%kernel.environment%.log', \Monolog\Logger::DEBUG, true)); |
|
36 } |
|
37 |
|
38 public function testLoadWithCustomValues() |
|
39 { |
|
40 $container = new ContainerBuilder(); |
|
41 $loader = new MonologExtension(); |
|
42 |
|
43 $loader->load(array(array('handlers' => array('custom' => array('type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => false, 'level' => 'ERROR')))), $container); |
|
44 $this->assertTrue($container->hasDefinition('monolog.logger')); |
|
45 $this->assertTrue($container->hasDefinition('monolog.handler.custom')); |
|
46 |
|
47 $logger = $container->getDefinition('monolog.logger'); |
|
48 $this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.custom'))); |
|
49 |
|
50 $handler = $container->getDefinition('monolog.handler.custom'); |
|
51 $this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%'); |
|
52 $this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::ERROR, false)); |
|
53 } |
|
54 |
|
55 public function testLoadWithSeveralHandlers() |
|
56 { |
|
57 $container = new ContainerBuilder(); |
|
58 $loader = new MonologExtension(); |
|
59 |
|
60 $loader->load(array(array('handlers' => array( |
|
61 'custom' => array('type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => false, 'level' => 'ERROR'), |
|
62 'main' => array('type' => 'fingers_crossed', 'action_level' => 'ERROR', 'handler' => 'nested'), |
|
63 'nested' => array('type' => 'stream') |
|
64 ))), $container); |
|
65 $this->assertTrue($container->hasDefinition('monolog.logger')); |
|
66 $this->assertTrue($container->hasDefinition('monolog.handler.custom')); |
|
67 $this->assertTrue($container->hasDefinition('monolog.handler.main')); |
|
68 $this->assertTrue($container->hasDefinition('monolog.handler.nested')); |
|
69 |
|
70 $logger = $container->getDefinition('monolog.logger'); |
|
71 $this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.custom'))); |
|
72 $this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.main'))); |
|
73 |
|
74 $handler = $container->getDefinition('monolog.handler.custom'); |
|
75 $this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%'); |
|
76 $this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::ERROR, false)); |
|
77 |
|
78 $handler = $container->getDefinition('monolog.handler.main'); |
|
79 $this->assertDICDefinitionClass($handler, '%monolog.handler.fingers_crossed.class%'); |
|
80 $this->assertDICConstructorArguments($handler, array(new Reference('monolog.handler.nested'), \Monolog\Logger::ERROR, 0, true, true)); |
|
81 } |
|
82 |
|
83 public function testLoadWithOverwriting() |
|
84 { |
|
85 $container = new ContainerBuilder(); |
|
86 $loader = new MonologExtension(); |
|
87 |
|
88 $loader->load(array( |
|
89 array('handlers' => array( |
|
90 'custom' => array('type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => false, 'level' => 'ERROR'), |
|
91 'main' => array('type' => 'fingers_crossed', 'action_level' => 'ERROR', 'handler' => 'nested'), |
|
92 'nested' => array('type' => 'stream') |
|
93 )), |
|
94 array('handlers' => array( |
|
95 'custom' => array('type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => true, 'level' => 'WARNING'), |
|
96 )) |
|
97 ), $container); |
|
98 $this->assertTrue($container->hasDefinition('monolog.logger')); |
|
99 $this->assertTrue($container->hasDefinition('monolog.handler.custom')); |
|
100 $this->assertTrue($container->hasDefinition('monolog.handler.main')); |
|
101 $this->assertTrue($container->hasDefinition('monolog.handler.nested')); |
|
102 |
|
103 $logger = $container->getDefinition('monolog.logger'); |
|
104 $this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.custom'))); |
|
105 $this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.main'))); |
|
106 |
|
107 $handler = $container->getDefinition('monolog.handler.custom'); |
|
108 $this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%'); |
|
109 $this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::WARNING, true)); |
|
110 |
|
111 $handler = $container->getDefinition('monolog.handler.main'); |
|
112 $this->assertDICDefinitionClass($handler, '%monolog.handler.fingers_crossed.class%'); |
|
113 $this->assertDICConstructorArguments($handler, array(new Reference('monolog.handler.nested'), \Monolog\Logger::ERROR, 0, true, true)); |
|
114 } |
|
115 |
|
116 public function testLoadWithNewAtEnd() |
|
117 { |
|
118 $container = new ContainerBuilder(); |
|
119 $loader = new MonologExtension(); |
|
120 |
|
121 $loader->load(array( |
|
122 array('handlers' => array( |
|
123 'custom' => array('type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => true, 'level' => 'ERROR'), |
|
124 'main' => array('type' => 'fingers_crossed', 'action_level' => 'ERROR', 'handler' => 'nested'), |
|
125 'nested' => array('type' => 'stream') |
|
126 )), |
|
127 array('handlers' => array( |
|
128 'custom' => array('type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => false, 'level' => 'WARNING'), |
|
129 'new' => array('type' => 'stream', 'path' => '/tmp/monolog.log', 'bubble' => true, 'level' => 'ERROR'), |
|
130 )) |
|
131 ), $container); |
|
132 $this->assertTrue($container->hasDefinition('monolog.logger')); |
|
133 $this->assertTrue($container->hasDefinition('monolog.handler.custom')); |
|
134 $this->assertTrue($container->hasDefinition('monolog.handler.main')); |
|
135 $this->assertTrue($container->hasDefinition('monolog.handler.nested')); |
|
136 $this->assertTrue($container->hasDefinition('monolog.handler.new')); |
|
137 |
|
138 $logger = $container->getDefinition('monolog.logger'); |
|
139 $this->assertDICDefinitionMethodCallAt(2, $logger, 'pushHandler', array(new Reference('monolog.handler.new'))); |
|
140 $this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.custom'))); |
|
141 $this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.main'))); |
|
142 |
|
143 $handler = $container->getDefinition('monolog.handler.new'); |
|
144 $this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%'); |
|
145 $this->assertDICConstructorArguments($handler, array('/tmp/monolog.log', \Monolog\Logger::ERROR, true)); |
|
146 } |
|
147 |
|
148 public function testLoadWithNewAndPriority() |
|
149 { |
|
150 $container = new ContainerBuilder(); |
|
151 $loader = new MonologExtension(); |
|
152 |
|
153 $loader->load(array( |
|
154 array('handlers' => array( |
|
155 'custom' => array('type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => true, 'level' => 'ERROR'), |
|
156 'main' => array('type' => 'buffer', 'level' => 'INFO', 'handler' => 'nested'), |
|
157 'nested' => array('type' => 'stream') |
|
158 )), |
|
159 array('handlers' => array( |
|
160 'custom' => array('type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => true, 'level' => 'WARNING'), |
|
161 'first' => array('type' => 'rotating_file', 'path' => '/tmp/monolog.log', 'bubble' => true, 'level' => 'ERROR', 'priority' => 3), |
|
162 'last' => array('type' => 'stream', 'path' => '/tmp/last.log', 'bubble' => true, 'level' => 'ERROR', 'priority' => -3), |
|
163 )) |
|
164 ), $container); |
|
165 $this->assertTrue($container->hasDefinition('monolog.logger')); |
|
166 $this->assertTrue($container->hasDefinition('monolog.handler.custom')); |
|
167 $this->assertTrue($container->hasDefinition('monolog.handler.main')); |
|
168 $this->assertTrue($container->hasDefinition('monolog.handler.nested')); |
|
169 $this->assertTrue($container->hasDefinition('monolog.handler.first')); |
|
170 $this->assertTrue($container->hasDefinition('monolog.handler.last')); |
|
171 |
|
172 $logger = $container->getDefinition('monolog.logger'); |
|
173 $this->assertDICDefinitionMethodCallAt(2, $logger, 'pushHandler', array(new Reference('monolog.handler.last'))); |
|
174 $this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.custom'))); |
|
175 $this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.main'))); |
|
176 $this->assertDICDefinitionMethodCallAt(2, $logger, 'pushHandler', array(new Reference('monolog.handler.first'))); |
|
177 |
|
178 $handler = $container->getDefinition('monolog.handler.main'); |
|
179 $this->assertDICDefinitionClass($handler, '%monolog.handler.buffer.class%'); |
|
180 $this->assertDICConstructorArguments($handler, array(new Reference('monolog.handler.nested'), 0, \Monolog\Logger::INFO, true)); |
|
181 |
|
182 $handler = $container->getDefinition('monolog.handler.first'); |
|
183 $this->assertDICDefinitionClass($handler, '%monolog.handler.rotating_file.class%'); |
|
184 $this->assertDICConstructorArguments($handler, array('/tmp/monolog.log', 0, \Monolog\Logger::ERROR, true)); |
|
185 |
|
186 $handler = $container->getDefinition('monolog.handler.last'); |
|
187 $this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%'); |
|
188 $this->assertDICConstructorArguments($handler, array('/tmp/last.log', \Monolog\Logger::ERROR, true)); |
|
189 } |
|
190 |
|
191 /** |
|
192 * @expectedException InvalidArgumentException |
|
193 */ |
|
194 public function testExceptionWhenInvalidHandler() |
|
195 { |
|
196 $container = new ContainerBuilder(); |
|
197 $loader = new MonologExtension(); |
|
198 |
|
199 $loader->load(array(array('handlers' => array('main' => array('type' => 'invalid_handler')))), $container); |
|
200 } |
|
201 |
|
202 /** |
|
203 * @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
|
204 */ |
|
205 public function testExceptionWhenUsingFingerscrossedWithoutHandler() |
|
206 { |
|
207 $container = new ContainerBuilder(); |
|
208 $loader = new MonologExtension(); |
|
209 |
|
210 $loader->load(array(array('handlers' => array('main' => array('type' => 'fingers_crossed')))), $container); |
|
211 } |
|
212 |
|
213 /** |
|
214 * @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
|
215 */ |
|
216 public function testExceptionWhenUsingBufferWithoutHandler() |
|
217 { |
|
218 $container = new ContainerBuilder(); |
|
219 $loader = new MonologExtension(); |
|
220 |
|
221 $loader->load(array(array('handlers' => array('main' => array('type' => 'buffer')))), $container); |
|
222 } |
|
223 |
|
224 /** |
|
225 * @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
|
226 */ |
|
227 public function testExceptionWhenUsingServiceWithoutId() |
|
228 { |
|
229 $container = new ContainerBuilder(); |
|
230 $loader = new MonologExtension(); |
|
231 |
|
232 $loader->load(array(array('handlers' => array('main' => array('type' => 'service')))), $container); |
|
233 } |
|
234 |
|
235 /** |
|
236 * @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
|
237 */ |
|
238 public function testExceptionWhenUsingDebugName() |
|
239 { |
|
240 // logger |
|
241 $container = new ContainerBuilder(); |
|
242 $loader = new MonologExtension(); |
|
243 |
|
244 $loader->load(array(array('handlers' => array('debug' => array('type' => 'stream')))), $container); |
|
245 } |
|
246 |
|
247 /** |
|
248 * Assertion on the Class of a DIC Service Definition. |
|
249 * |
|
250 * @param \Symfony\Component\DependencyInjection\Definition $definition |
|
251 * @param string $expectedClass |
|
252 */ |
|
253 protected function assertDICDefinitionClass($definition, $expectedClass) |
|
254 { |
|
255 $this->assertEquals($expectedClass, $definition->getClass(), "Expected Class of the DIC Container Service Definition is wrong."); |
|
256 } |
|
257 |
|
258 protected function assertDICConstructorArguments($definition, $args) |
|
259 { |
|
260 $this->assertEquals($args, $definition->getArguments(), "Expected and actual DIC Service constructor arguments of definition '".$definition->getClass()."' don't match."); |
|
261 } |
|
262 |
|
263 protected function assertDICDefinitionMethodCallAt($pos, $definition, $methodName, array $params = null) |
|
264 { |
|
265 $calls = $definition->getMethodCalls(); |
|
266 if (isset($calls[$pos][0])) { |
|
267 $this->assertEquals($methodName, $calls[$pos][0], "Method '".$methodName."' is expected to be called at position $pos."); |
|
268 |
|
269 if ($params !== null) { |
|
270 $this->assertEquals($params, $calls[$pos][1], "Expected parameters to methods '".$methodName."' do not match the actual parameters."); |
|
271 } |
|
272 } |
|
273 } |
|
274 } |