|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\DependencyInjection\MonologExtension; |
| 15 | +use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass; |
| 16 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 17 | +use Symfony\Component\DependencyInjection\Reference; |
| 18 | + |
| 19 | +abstract class FixtureMonologExtensionTest extends DependencyInjectionTest |
| 20 | +{ |
| 21 | + public function testLoadWithSeveralHandlers() |
| 22 | + { |
| 23 | + $container = $this->getContainer('multiple_handlers'); |
| 24 | + |
| 25 | + $this->assertTrue($container->hasDefinition('monolog.logger')); |
| 26 | + $this->assertTrue($container->hasDefinition('monolog.handler.custom')); |
| 27 | + $this->assertTrue($container->hasDefinition('monolog.handler.main')); |
| 28 | + $this->assertTrue($container->hasDefinition('monolog.handler.nested')); |
| 29 | + |
| 30 | + $logger = $container->getDefinition('monolog.logger'); |
| 31 | + $this->assertCount(2, $logger->getMethodCalls()); |
| 32 | + $this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.custom'))); |
| 33 | + $this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.main'))); |
| 34 | + |
| 35 | + $handler = $container->getDefinition('monolog.handler.custom'); |
| 36 | + $this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%'); |
| 37 | + $this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::ERROR, false)); |
| 38 | + |
| 39 | + $handler = $container->getDefinition('monolog.handler.main'); |
| 40 | + $this->assertDICDefinitionClass($handler, '%monolog.handler.fingers_crossed.class%'); |
| 41 | + $this->assertDICConstructorArguments($handler, array(new Reference('monolog.handler.nested'), \Monolog\Logger::ERROR, 0, true, true)); |
| 42 | + } |
| 43 | + |
| 44 | + public function testLoadWithOverwriting() |
| 45 | + { |
| 46 | + $container = $this->getContainer('overwriting'); |
| 47 | + |
| 48 | + $this->assertTrue($container->hasDefinition('monolog.logger')); |
| 49 | + $this->assertTrue($container->hasDefinition('monolog.handler.custom')); |
| 50 | + $this->assertTrue($container->hasDefinition('monolog.handler.main')); |
| 51 | + $this->assertTrue($container->hasDefinition('monolog.handler.nested')); |
| 52 | + |
| 53 | + $logger = $container->getDefinition('monolog.logger'); |
| 54 | + $this->assertCount(2, $logger->getMethodCalls()); |
| 55 | + $this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.custom'))); |
| 56 | + $this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.main'))); |
| 57 | + |
| 58 | + $handler = $container->getDefinition('monolog.handler.custom'); |
| 59 | + $this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%'); |
| 60 | + $this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::WARNING, true)); |
| 61 | + |
| 62 | + $handler = $container->getDefinition('monolog.handler.main'); |
| 63 | + $this->assertDICDefinitionClass($handler, '%monolog.handler.fingers_crossed.class%'); |
| 64 | + $this->assertDICConstructorArguments($handler, array(new Reference('monolog.handler.nested'), \Monolog\Logger::ERROR, 0, true, true)); |
| 65 | + } |
| 66 | + |
| 67 | + public function testLoadWithNewAtEnd() |
| 68 | + { |
| 69 | + $container = $this->getContainer('new_at_end'); |
| 70 | + |
| 71 | + $this->assertTrue($container->hasDefinition('monolog.logger')); |
| 72 | + $this->assertTrue($container->hasDefinition('monolog.handler.custom')); |
| 73 | + $this->assertTrue($container->hasDefinition('monolog.handler.main')); |
| 74 | + $this->assertTrue($container->hasDefinition('monolog.handler.nested')); |
| 75 | + $this->assertTrue($container->hasDefinition('monolog.handler.new')); |
| 76 | + |
| 77 | + $logger = $container->getDefinition('monolog.logger'); |
| 78 | + $this->assertCount(3, $logger->getMethodCalls()); |
| 79 | + $this->assertDICDefinitionMethodCallAt(2, $logger, 'pushHandler', array(new Reference('monolog.handler.custom'))); |
| 80 | + $this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.main'))); |
| 81 | + $this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.new'))); |
| 82 | + |
| 83 | + $handler = $container->getDefinition('monolog.handler.new'); |
| 84 | + $this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%'); |
| 85 | + $this->assertDICConstructorArguments($handler, array('/tmp/monolog.log', \Monolog\Logger::ERROR, true)); |
| 86 | + } |
| 87 | + |
| 88 | + public function testLoadWithNewAndPriority() |
| 89 | + { |
| 90 | + $container = $this->getContainer('new_and_priority'); |
| 91 | + |
| 92 | + $this->assertTrue($container->hasDefinition('monolog.logger')); |
| 93 | + $this->assertTrue($container->hasDefinition('monolog.handler.custom')); |
| 94 | + $this->assertTrue($container->hasDefinition('monolog.handler.main')); |
| 95 | + $this->assertTrue($container->hasDefinition('monolog.handler.nested')); |
| 96 | + $this->assertTrue($container->hasDefinition('monolog.handler.first')); |
| 97 | + $this->assertTrue($container->hasDefinition('monolog.handler.last')); |
| 98 | + |
| 99 | + $logger = $container->getDefinition('monolog.logger'); |
| 100 | + $this->assertCount(4, $logger->getMethodCalls()); |
| 101 | + $this->assertDICDefinitionMethodCallAt(3, $logger, 'pushHandler', array(new Reference('monolog.handler.first'))); |
| 102 | + $this->assertDICDefinitionMethodCallAt(2, $logger, 'pushHandler', array(new Reference('monolog.handler.custom'))); |
| 103 | + $this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.main'))); |
| 104 | + $this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.last'))); |
| 105 | + |
| 106 | + $handler = $container->getDefinition('monolog.handler.main'); |
| 107 | + $this->assertDICDefinitionClass($handler, '%monolog.handler.buffer.class%'); |
| 108 | + $this->assertDICConstructorArguments($handler, array(new Reference('monolog.handler.nested'), 0, \Monolog\Logger::INFO, true)); |
| 109 | + |
| 110 | + $handler = $container->getDefinition('monolog.handler.first'); |
| 111 | + $this->assertDICDefinitionClass($handler, '%monolog.handler.rotating_file.class%'); |
| 112 | + $this->assertDICConstructorArguments($handler, array('/tmp/monolog.log', 0, \Monolog\Logger::ERROR, true)); |
| 113 | + |
| 114 | + $handler = $container->getDefinition('monolog.handler.last'); |
| 115 | + $this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%'); |
| 116 | + $this->assertDICConstructorArguments($handler, array('/tmp/last.log', \Monolog\Logger::ERROR, true)); |
| 117 | + } |
| 118 | + |
| 119 | + public function testHandlersWithChannels() |
| 120 | + { |
| 121 | + $container = $this->getContainer('handlers_with_channels'); |
| 122 | + |
| 123 | + $this->assertEquals( |
| 124 | + array( |
| 125 | + 'monolog.handler.custom' => array('type' => 'inclusive', 'elements' => array('foo')), |
| 126 | + 'monolog.handler.main' => array('type' => 'exclusive', 'elements' => array('foo', 'bar')), |
| 127 | + 'monolog.handler.extra' => null, |
| 128 | + 'monolog.handler.more' => array('type' => 'inclusive', 'elements' => array('security', 'doctrine')), |
| 129 | + ), |
| 130 | + $container->getParameter('monolog.handlers_to_channels') |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + protected function getContainer($fixture) |
| 135 | + { |
| 136 | + $container = new ContainerBuilder(); |
| 137 | + $container->registerExtension(new MonologExtension()); |
| 138 | + |
| 139 | + $this->loadFixture($container, $fixture); |
| 140 | + |
| 141 | + $container->getCompilerPassConfig()->setOptimizationPasses(array()); |
| 142 | + $container->getCompilerPassConfig()->setRemovingPasses(array()); |
| 143 | + $container->addCompilerPass(new LoggerChannelPass()); |
| 144 | + $container->compile(); |
| 145 | + |
| 146 | + return $container; |
| 147 | + } |
| 148 | + |
| 149 | + abstract protected function loadFixture(ContainerBuilder $container, $fixture); |
| 150 | +} |
0 commit comments