From 46c9d350b0709195af7da9db34503b6454f98b8b Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Fri, 18 Mar 2011 13:44:03 +0100 Subject: [PATCH] [MonologBundle] Fixed the fingerscrossed configuration and added unit tests --- .../DependencyInjection/MonologExtension.php | 4 +- .../MonologExtensionTest.php | 111 ++++++++++++++++++ 2 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 src/Symfony/Bundle/MonologBundle/Tests/DependencyInjection/MonologExtensionTest.php diff --git a/src/Symfony/Bundle/MonologBundle/DependencyInjection/MonologExtension.php b/src/Symfony/Bundle/MonologBundle/DependencyInjection/MonologExtension.php index 1ca3881d55..3bc1c5db8d 100644 --- a/src/Symfony/Bundle/MonologBundle/DependencyInjection/MonologExtension.php +++ b/src/Symfony/Bundle/MonologBundle/DependencyInjection/MonologExtension.php @@ -59,8 +59,6 @@ class MonologExtension extends Extension $handlers[] = $this->buildHandler($container, $name, $handler); } - // TODO somehow the DebugLogger should be pushed on the stack as well, or that concept needs to be changed - // didn't have to investigate yet what it is exactly $handlers = array_reverse($handlers); foreach ($handlers as $handler) { $logger->addMethodCall('pushHandler', array(new Reference($handler))); @@ -94,7 +92,7 @@ class MonologExtension extends Extension $handler['action_level'] = is_int($handler['action_level']) ? $handler['action_level'] : constant('Monolog\Logger::'.strtoupper($handler['action_level'])); $definition->setArguments(array( - $this->buildHandler($container, $handler['handler']), + new Reference($this->buildHandler($container, sprintf('%s.real', $name), $handler['handler'])), $handler['action_level'], isset($handler['buffer_size']) ? $handler['buffer_size'] : 0, $handler['bubble'], diff --git a/src/Symfony/Bundle/MonologBundle/Tests/DependencyInjection/MonologExtensionTest.php b/src/Symfony/Bundle/MonologBundle/Tests/DependencyInjection/MonologExtensionTest.php new file mode 100644 index 0000000000..92c5193aeb --- /dev/null +++ b/src/Symfony/Bundle/MonologBundle/Tests/DependencyInjection/MonologExtensionTest.php @@ -0,0 +1,111 @@ + + * + * 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; + +use Symfony\Bundle\MonologBundle\Tests\TestCase; +use Symfony\Bundle\MonologBundle\DependencyInjection\MonologExtension; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Reference; + +class MonologExtensionTest extends TestCase +{ + public function testLoadWithDefault() + { + // logger + $container = new ContainerBuilder(); + $loader = new MonologExtension(); + + $loader->load(array(array('handlers' => array('main' => array('type' => 'stream')))), $container); + $this->assertTrue($container->hasDefinition('monolog.logger')); + $this->assertTrue($container->hasDefinition('monolog.handler.main')); + + $logger = $container->getDefinition('monolog.logger'); + $this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.main'))); + + $handler = $container->getDefinition('monolog.handler.main'); + $this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%'); + $this->assertDICConstructorArguments($handler, array('%kernel.logs_dir%/%kernel.environment%.log', \Monolog\Logger::DEBUG, false)); + } + + public function testLoadWithCustomValues() + { + // logger + $container = new ContainerBuilder(); + $loader = new MonologExtension(); + + $loader->load(array(array('handlers' => array('custom' => array('type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => true, 'level' => 'ERROR')))), $container); + $this->assertTrue($container->hasDefinition('monolog.logger')); + $this->assertTrue($container->hasDefinition('monolog.handler.custom')); + + $logger = $container->getDefinition('monolog.logger'); + $this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.custom'))); + + $handler = $container->getDefinition('monolog.handler.custom'); + $this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%'); + $this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::ERROR, true)); + } + + public function testLoadWithSeveralHandlers() + { + // logger + $container = new ContainerBuilder(); + $loader = new MonologExtension(); + + $loader->load(array(array('handlers' => array( + 'custom' => array('type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => true, 'level' => 'ERROR'), + 'main' => array('type' => 'fingerscrossed', 'action_level' => 'ERROR', 'handler' => array('type' => 'stream')) + ))), $container); + $this->assertTrue($container->hasDefinition('monolog.logger')); + $this->assertTrue($container->hasDefinition('monolog.handler.custom')); + $this->assertTrue($container->hasDefinition('monolog.handler.main')); + + $logger = $container->getDefinition('monolog.logger'); + $this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.custom'))); + $this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.main'))); + + $handler = $container->getDefinition('monolog.handler.custom'); + $this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%'); + $this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::ERROR, true)); + + $handler = $container->getDefinition('monolog.handler.main'); + $this->assertDICDefinitionClass($handler, '%monolog.handler.fingerscrossed.class%'); + $this->assertDICConstructorArguments($handler, array(new Reference('monolog.handler.main.real'), \Monolog\Logger::ERROR, 0, false)); + } + + /** + * Assertion on the Class of a DIC Service Definition. + * + * @param \Symfony\Component\DependencyInjection\Definition $definition + * @param string $expectedClass + */ + protected function assertDICDefinitionClass($definition, $expectedClass) + { + $this->assertEquals($expectedClass, $definition->getClass(), "Expected Class of the DIC Container Service Definition is wrong."); + } + + protected function assertDICConstructorArguments($definition, $args) + { + $this->assertEquals($args, $definition->getArguments(), "Expected and actual DIC Service constructor arguments of definition '" . $definition->getClass()."' don't match."); + } + + protected function assertDICDefinitionMethodCallAt($pos, $definition, $methodName, array $params = null) + { + $calls = $definition->getMethodCalls(); + if (isset($calls[$pos][0])) { + $this->assertEquals($methodName, $calls[$pos][0], "Method '".$methodName."' is expected to be called at position $pos."); + + if ($params !== null) { + $this->assertEquals($params, $calls[$pos][1], "Expected parameters to methods '" . $methodName . "' do not match the actual parameters."); + } + } + } +}