[MonologBundle] Fixed the fingerscrossed configuration and added unit tests
This commit is contained in:
parent
4372d5613e
commit
46c9d350b0
@ -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'],
|
||||
|
@ -0,0 +1,111 @@
|
||||
<?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;
|
||||
|
||||
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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user