Initial commit with Symfony 2.1+Vendors

Signed-off-by: Gergely POLONKAI (W00d5t0ck) <polesz@w00d5t0ck.info>
This commit is contained in:
Polonkai Gergely
2012-07-01 09:52:20 +02:00
commit 082a0130c2
5381 changed files with 416709 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
<?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 Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\AddProcessorsPass;
use Symfony\Bundle\MonologBundle\Tests\TestCase;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
class AddProcessorsPassTest extends TestCase
{
public function testHandlerProcessors()
{
$container = $this->getContainer();
$service = $container->getDefinition('monolog.handler.test');
$calls = $service->getMethodCalls();
$this->assertCount(1, $calls);
$this->assertEquals(array('pushProcessor', array(new Reference('test'))), $calls[0]);
$service = $container->getDefinition('handler_test');
$calls = $service->getMethodCalls();
$this->assertCount(1, $calls);
$this->assertEquals(array('pushProcessor', array(new Reference('test2'))), $calls[0]);
}
protected 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->setDefinition('monolog.handler.test', new Definition('%monolog.handler.null.class%', array (100, false)));
$container->setDefinition('handler_test', new Definition('%monolog.handler.null.class%', array (100, false)));
$container->setAlias('monolog.handler.test2', 'handler_test');
$definition->addMethodCall('pushHandler', array(new Reference('monolog.handler.test')));
$definition->addMethodCall('pushHandler', array(new Reference('monolog.handler.test2')));
$service = new Definition('TestClass', array('false', new Reference('logger')));
$service->addTag('monolog.processor', array ('handler' => 'test'));
$container->setDefinition('test', $service);
$service = new Definition('TestClass', array('false', new Reference('logger')));
$service->addTag('monolog.processor', array ('handler' => 'test2'));
$container->setDefinition('test2', $service);
$container->getCompilerPassConfig()->setOptimizationPasses(array());
$container->getCompilerPassConfig()->setRemovingPasses(array());
$container->addCompilerPass(new AddProcessorsPass());
$container->compile();
return $container;
}
}

View File

@@ -0,0 +1,130 @@
<?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 Symfony\Bundle\MonologBundle\Tests\TestCase;
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]);
}
}
}
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');
}
protected 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.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;
}
protected 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.handlers_to_channels', array());
$container->getCompilerPassConfig()->setOptimizationPasses(array());
$container->getCompilerPassConfig()->setRemovingPasses(array());
$container->addCompilerPass(new LoggerChannelPass());
$container->compile();
return $container;
}
}

View File

@@ -0,0 +1,228 @@
<?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\DependencyInjection\Configuration;
use Symfony\Component\Config\Definition\Processor;
class ConfigurationTest extends \PHPUnit_Framework_TestCase
{
/**
* Some basic tests to make sure the configuration is correctly processed in
* the standard case.
*/
public function testProcessSimpleCase()
{
$configs = array(
array(
'handlers' => array('foobar' => array('type' => 'stream', 'path' => '/foo/bar'))
)
);
$config = $this->process($configs);
$this->assertArrayHasKey('handlers', $config);
$this->assertArrayHasKey('foobar', $config['handlers']);
$this->assertEquals('stream', $config['handlers']['foobar']['type']);
$this->assertEquals('/foo/bar', $config['handlers']['foobar']['path']);
}
public function provideProcessStringChannels()
{
return array(
array('foo', 'foo', true),
array('!foo', 'foo', false)
);
}
/**
* @dataProvider provideProcessStringChannels
*/
public function testProcessStringChannels($string, $expectedString, $isInclusive)
{
$configs = array(
array(
'handlers' => array(
'foobar' => array(
'type' => 'stream',
'path' => '/foo/bar',
'channels' => $string
)
)
)
);
$config = $this->process($configs);
$this->assertEquals($isInclusive ? 'inclusive' : 'exclusive', $config['handlers']['foobar']['channels']['type']);
$this->assertCount(1, $config['handlers']['foobar']['channels']['elements']);
$this->assertEquals($expectedString, $config['handlers']['foobar']['channels']['elements'][0]);
}
public function provideGelfPublisher()
{
return array(
array(
'gelf.publisher'
),
array(
array(
'id' => 'gelf.publisher'
)
)
);
}
/**
* @dataProvider provideGelfPublisher
*/
public function testGelfPublisherService($publisher)
{
$configs = array(
array(
'handlers' => array(
'gelf' => array(
'type' => 'gelf',
'publisher' => $publisher,
),
)
)
);
$config = $this->process($configs);
$this->assertArrayHasKey('id', $config['handlers']['gelf']['publisher']);
$this->assertArrayNotHasKey('hostname', $config['handlers']['gelf']['publisher']);
$this->assertEquals('gelf.publisher', $config['handlers']['gelf']['publisher']['id']);
}
public function testArrays()
{
$configs = array(
array(
'handlers' => array(
'foo' => array(
'type' => 'stream',
'path' => '/foo',
'channels' => array('A', 'B')
),
'bar' => array(
'type' => 'stream',
'path' => '/foo',
'channels' => array('!C', '!D')
),
)
)
);
$config = $this->process($configs);
// Check foo
$this->assertCount(2, $config['handlers']['foo']['channels']['elements']);
$this->assertEquals('inclusive', $config['handlers']['foo']['channels']['type']);
$this->assertEquals('A', $config['handlers']['foo']['channels']['elements'][0]);
$this->assertEquals('B', $config['handlers']['foo']['channels']['elements'][1]);
// Check bar
$this->assertCount(2, $config['handlers']['bar']['channels']['elements']);
$this->assertEquals('exclusive', $config['handlers']['bar']['channels']['type']);
$this->assertEquals('C', $config['handlers']['bar']['channels']['elements'][0]);
$this->assertEquals('D', $config['handlers']['bar']['channels']['elements'][1]);
}
/**
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testInvalidArrays()
{
$configs = array(
array(
'handlers' => array(
'foo' => array(
'type' => 'stream',
'path' => '/foo',
'channels' => array('A', '!B')
)
)
)
);
$config = $this->process($configs);
}
/**
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testMergingInvalidChannels()
{
$configs = array(
array(
'handlers' => array(
'foo' => array(
'type' => 'stream',
'path' => '/foo',
'channels' => 'A',
)
)
),
array(
'handlers' => array(
'foo' => array(
'channels' => '!B',
)
)
)
);
$config = $this->process($configs);
}
public function testWithType()
{
$configs = array(
array(
'handlers' => array(
'foo' => array(
'type' => 'stream',
'path' => '/foo',
'channels' => array(
'type' => 'inclusive',
'elements' => array('A', 'B')
)
)
)
)
);
$config = $this->process($configs);
// Check foo
$this->assertCount(2, $config['handlers']['foo']['channels']['elements']);
$this->assertEquals('inclusive', $config['handlers']['foo']['channels']['type']);
$this->assertEquals('A', $config['handlers']['foo']['channels']['elements'][0]);
$this->assertEquals('B', $config['handlers']['foo']['channels']['elements'][1]);
}
/**
* Processes an array of configurations and returns a compiled version.
*
* @param array $configs An array of raw configurations
*
* @return array A normalized array
*/
protected function process($configs)
{
$processor = new Processor();
return $processor->processConfiguration(new Configuration(), $configs);
}
}

View File

@@ -0,0 +1,31 @@
<?xml version="1.0" ?>
<srv:container xmlns="http://symfony.com/schema/dic/monolog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:srv="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/monolog http://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
<config>
<handler name="custom" type="stream" path="/tmp/symfony.log" bubble="false" level="ERROR">
<channels>
<channel>foo</channel>
</channels>
</handler>
<handler name="main" type="group" handler="nested">
<member>nested</member>
<channels>
<channel>!foo</channel>
<channel>!bar</channel>
</channels>
</handler>
<handler name="nested" type="stream" />
<handler name="extra" type="syslog" ident="monolog" facility="user" level="ALERT" />
<handler name="more" type="native_mailer" to-email="monitoring@example.org" from-email="webmaster@example.org" subject="Monolog report" level="CRITICAL">
<channels type="inclusive">
<channel>security</channel>
<channel>doctrine</channel>
</channels>
</handler>
</config>
</srv:container>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" ?>
<srv:container xmlns="http://symfony.com/schema/dic/monolog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:srv="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/monolog http://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
<config>
<handler name="custom" type="stream" path="/tmp/symfony.log" bubble="false" level="ERROR" />
<handler name="main" type="fingers_crossed" action-level="ERROR" handler="nested" />
<handler name="nested" type="stream" />
</config>
</srv:container>

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" ?>
<srv:container xmlns="http://symfony.com/schema/dic/monolog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:srv="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/monolog http://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
<srv:imports>
<srv:import resource="new_and_priority_import.xml" />
</srv:imports>
<config>
<handler name="custom" type="stream" path="/tmp/symfony.log" bubble="true" level="WARNING" />
<handler name="first" type="rotating_file" path="/tmp/monolog.log" bubble="true" level="ERROR" priority="3" />
<handler name="last" type="stream" path="/tmp/last.log" bubble="true" level="ERROR" priority="-3" />
</config>
</srv:container>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" ?>
<srv:container xmlns="http://symfony.com/schema/dic/monolog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:srv="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/monolog http://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
<config>
<handler name="custom" type="stream" path="/tmp/symfony.log" bubble="true" level="ERROR" />
<handler name="main" type="buffer" level="INFO" handler="nested" />
<handler name="nested" type="stream" />
</config>
</srv:container>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" ?>
<srv:container xmlns="http://symfony.com/schema/dic/monolog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:srv="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/monolog http://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
<srv:imports>
<srv:import resource="new_at_end_import.xml" />
</srv:imports>
<config>
<handler name="custom" type="stream" path="/tmp/symfony.log" bubble="false" level="WARNING" />
<handler name="new" type="stream" path="/tmp/monolog.log" bubble="true" level="ERROR" />
</config>
</srv:container>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" ?>
<srv:container xmlns="http://symfony.com/schema/dic/monolog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:srv="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/monolog http://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
<config>
<handler name="custom" type="stream" path="/tmp/symfony.log" bubble="true" level="ERROR" />
<handler name="main" type="fingers_crossed" action-level="ERROR" handler="nested" />
<handler name="nested" type="stream" />
</config>
</srv:container>

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" ?>
<srv:container xmlns="http://symfony.com/schema/dic/monolog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:srv="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/monolog http://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
<srv:imports>
<srv:import resource="overwriting_import.xml" />
</srv:imports>
<config>
<handler name="custom" type="stream" path="/tmp/symfony.log" bubble="true" level="WARNING" />
</config>
</srv:container>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" ?>
<srv:container xmlns="http://symfony.com/schema/dic/monolog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:srv="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/monolog http://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
<config>
<handler name="custom" type="stream" path="/tmp/symfony.log" bubble="false" level="ERROR" />
<handler name="main" type="fingers_crossed" action-level="ERROR" handler="nested" />
<handler name="nested" type="stream" />
</config>
</srv:container>

View File

@@ -0,0 +1,30 @@
monolog:
handlers:
custom:
type: stream
path: /tmp/symfony.log
bubble: false
level: ERROR
channels: foo
main:
type: group
members: [nested]
channels: ["!foo", "!bar"]
nested:
type: stream
extra:
type: syslog
ident: monolog
facility: user
level: ALERT
more:
type: native_mailer
to_email: monitoring@example.org
from_email: webmaster@example.org
subject: Monolog report
level: CRITICAL
channels:
type: inclusive
elements:
- security
- doctrine

View File

@@ -0,0 +1,13 @@
monolog:
handlers:
custom:
type: stream
path: /tmp/symfony.log
bubble: false
level: ERROR
main:
type: fingers_crossed
action_level: ERROR
handler: nested
nested:
type: stream

View File

@@ -0,0 +1,22 @@
imports:
- { resource: new_and_priority_import.yml }
monolog:
handlers:
custom:
type: stream
path: /tmp/symfony.log
bubble: true
level: WARNING
first:
type: rotating_file
path: /tmp/monolog.log
bubble: true
level: ERROR
priority: 3
last:
type: stream
path: /tmp/last.log
bubble: true
level: ERROR
priority: -3

View File

@@ -0,0 +1,13 @@
monolog:
handlers:
custom:
type: stream
path: /tmp/symfony.log
bubble: true
level: ERROR
main:
type: buffer
level: INFO
handler: nested
nested:
type: stream

View File

@@ -0,0 +1,15 @@
imports:
- { resource: new_at_end_import.yml }
monolog:
handlers:
custom:
type: stream
path: /tmp/symfony.log
bubble: false
level: WARNING
new:
type: stream
path: /tmp/monolog.log
bubble: true
level: ERROR

View File

@@ -0,0 +1,13 @@
monolog:
handlers:
custom:
type: stream
path: /tmp/symfony.log
bubble: true
level: ERROR
main:
type: fingers_crossed
action_level: ERROR
handler: nested
nested:
type: stream

View File

@@ -0,0 +1,10 @@
imports:
- { resource: overwriting_import.yml }
monolog:
handlers:
custom:
type: stream
path: /tmp/symfony.log
bubble: true
level: WARNING

View File

@@ -0,0 +1,13 @@
monolog:
handlers:
custom:
type: stream
path: /tmp/symfony.log
bubble: false
level: ERROR
main:
type: fingers_crossed
action_level: ERROR
handler: nested
nested:
type: stream

View File

@@ -0,0 +1,285 @@
<?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;
abstract class MonologExtensionTest extends TestCase
{
public function testLoadWithDefault()
{
$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, true));
}
public function testLoadWithCustomValues()
{
$container = new ContainerBuilder();
$loader = new MonologExtension();
$loader->load(array(array('handlers' => array('custom' => array('type' => 'stream', 'path' => '/tmp/symfony.log', 'bubble' => false, '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, false));
}
public function testLoadWithSeveralHandlers()
{
$container = $this->getContainer('multiple_handlers');
$this->assertTrue($container->hasDefinition('monolog.logger'));
$this->assertTrue($container->hasDefinition('monolog.handler.custom'));
$this->assertTrue($container->hasDefinition('monolog.handler.main'));
$this->assertTrue($container->hasDefinition('monolog.handler.nested'));
$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, false));
$handler = $container->getDefinition('monolog.handler.main');
$this->assertDICDefinitionClass($handler, '%monolog.handler.fingers_crossed.class%');
$this->assertDICConstructorArguments($handler, array(new Reference('monolog.handler.nested'), \Monolog\Logger::ERROR, 0, true, true));
}
public function testLoadWithOverwriting()
{
$container = $this->getContainer('overwriting');
$this->assertTrue($container->hasDefinition('monolog.logger'));
$this->assertTrue($container->hasDefinition('monolog.handler.custom'));
$this->assertTrue($container->hasDefinition('monolog.handler.main'));
$this->assertTrue($container->hasDefinition('monolog.handler.nested'));
$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::WARNING, true));
$handler = $container->getDefinition('monolog.handler.main');
$this->assertDICDefinitionClass($handler, '%monolog.handler.fingers_crossed.class%');
$this->assertDICConstructorArguments($handler, array(new Reference('monolog.handler.nested'), \Monolog\Logger::ERROR, 0, true, true));
}
public function testLoadWithNewAtEnd()
{
$container = $this->getContainer('new_at_end');
$this->assertTrue($container->hasDefinition('monolog.logger'));
$this->assertTrue($container->hasDefinition('monolog.handler.custom'));
$this->assertTrue($container->hasDefinition('monolog.handler.main'));
$this->assertTrue($container->hasDefinition('monolog.handler.nested'));
$this->assertTrue($container->hasDefinition('monolog.handler.new'));
$logger = $container->getDefinition('monolog.logger');
$this->assertDICDefinitionMethodCallAt(2, $logger, 'pushHandler', array(new Reference('monolog.handler.new')));
$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.new');
$this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%');
$this->assertDICConstructorArguments($handler, array('/tmp/monolog.log', \Monolog\Logger::ERROR, true));
}
public function testLoadWithNewAndPriority()
{
$container = $this->getContainer('new_and_priority');
$this->assertTrue($container->hasDefinition('monolog.logger'));
$this->assertTrue($container->hasDefinition('monolog.handler.custom'));
$this->assertTrue($container->hasDefinition('monolog.handler.main'));
$this->assertTrue($container->hasDefinition('monolog.handler.nested'));
$this->assertTrue($container->hasDefinition('monolog.handler.first'));
$this->assertTrue($container->hasDefinition('monolog.handler.last'));
$logger = $container->getDefinition('monolog.logger');
$this->assertDICDefinitionMethodCallAt(2, $logger, 'pushHandler', array(new Reference('monolog.handler.last')));
$this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.custom')));
$this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.main')));
$this->assertDICDefinitionMethodCallAt(2, $logger, 'pushHandler', array(new Reference('monolog.handler.first')));
$handler = $container->getDefinition('monolog.handler.main');
$this->assertDICDefinitionClass($handler, '%monolog.handler.buffer.class%');
$this->assertDICConstructorArguments($handler, array(new Reference('monolog.handler.nested'), 0, \Monolog\Logger::INFO, true));
$handler = $container->getDefinition('monolog.handler.first');
$this->assertDICDefinitionClass($handler, '%monolog.handler.rotating_file.class%');
$this->assertDICConstructorArguments($handler, array('/tmp/monolog.log', 0, \Monolog\Logger::ERROR, true));
$handler = $container->getDefinition('monolog.handler.last');
$this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%');
$this->assertDICConstructorArguments($handler, array('/tmp/last.log', \Monolog\Logger::ERROR, true));
}
public function testHandlersWithChannels()
{
$container = $this->getContainer('handlers_with_channels');
$this->assertEquals(
array(
'monolog.handler.custom' => array('type' => 'inclusive', 'elements' => array('foo')),
'monolog.handler.main' => array('type' => 'exclusive', 'elements' => array('foo', 'bar')),
'monolog.handler.extra' => null,
'monolog.handler.more' => array('type' => 'inclusive', 'elements' => array('security', 'doctrine')),
),
$container->getParameter('monolog.handlers_to_channels')
);
}
/**
* @expectedException InvalidArgumentException
*/
public function testExceptionWhenInvalidHandler()
{
$container = new ContainerBuilder();
$loader = new MonologExtension();
$loader->load(array(array('handlers' => array('main' => array('type' => 'invalid_handler')))), $container);
}
/**
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testExceptionWhenUsingFingerscrossedWithoutHandler()
{
$container = new ContainerBuilder();
$loader = new MonologExtension();
$loader->load(array(array('handlers' => array('main' => array('type' => 'fingers_crossed')))), $container);
}
/**
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testExceptionWhenUsingBufferWithoutHandler()
{
$container = new ContainerBuilder();
$loader = new MonologExtension();
$loader->load(array(array('handlers' => array('main' => array('type' => 'buffer')))), $container);
}
/**
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testExceptionWhenUsingGelfWithoutPublisher()
{
$container = new ContainerBuilder();
$loader = new MonologExtension();
$loader->load(array(array('handlers' => array('gelf' => array('type' => 'gelf')))), $container);
}
/**
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testExceptionWhenUsingGelfWithoutPublisherHostname()
{
$container = new ContainerBuilder();
$loader = new MonologExtension();
$loader->load(array(array('handlers' => array('gelf' => array('type' => 'gelf', 'publisher' => array())))), $container);
}
/**
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testExceptionWhenUsingServiceWithoutId()
{
$container = new ContainerBuilder();
$loader = new MonologExtension();
$loader->load(array(array('handlers' => array('main' => array('type' => 'service')))), $container);
}
/**
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
*/
public function testExceptionWhenUsingDebugName()
{
// logger
$container = new ContainerBuilder();
$loader = new MonologExtension();
$loader->load(array(array('handlers' => array('debug' => array('type' => 'stream')))), $container);
}
protected function getContainer($fixture)
{
$container = new ContainerBuilder();
$container->registerExtension(new MonologExtension());
$this->loadFixture($container, $fixture);
$container->getCompilerPassConfig()->setOptimizationPasses(array());
$container->getCompilerPassConfig()->setRemovingPasses(array());
$container->compile();
return $container;
}
abstract protected function loadFixture(ContainerBuilder $container, $fixture);
/**
* 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.");
}
}
}
}

View File

@@ -0,0 +1,25 @@
<?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\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
class XmlMonologExtensionTest extends MonologExtensionTest
{
protected function loadFixture(ContainerBuilder $container, $fixture)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/Fixtures/xml'));
$loader->load($fixture.'.xml');
}
}

View File

@@ -0,0 +1,25 @@
<?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\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
class YamlMonologExtensionTest extends MonologExtensionTest
{
protected function loadFixture(ContainerBuilder $container, $fixture)
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/Fixtures/yml'));
$loader->load($fixture.'.yml');
}
}