Skip to content

Commit e09c32b

Browse files
committed
Rewrite tests
1 parent 7fe7f71 commit e09c32b

File tree

5 files changed

+194
-157
lines changed

5 files changed

+194
-157
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\Tests\TestCase;
15+
16+
abstract class DependencyInjectionTest extends TestCase
17+
{
18+
/**
19+
* Assertion on the Class of a DIC Service Definition.
20+
*
21+
* @param \Symfony\Component\DependencyInjection\Definition $definition
22+
* @param string $expectedClass
23+
*/
24+
protected function assertDICDefinitionClass($definition, $expectedClass)
25+
{
26+
$this->assertEquals($expectedClass, $definition->getClass(), "Expected Class of the DIC Container Service Definition is wrong.");
27+
}
28+
29+
protected function assertDICConstructorArguments($definition, $args)
30+
{
31+
$this->assertEquals($args, $definition->getArguments(), "Expected and actual DIC Service constructor arguments of definition '".$definition->getClass()."' don't match.");
32+
}
33+
34+
protected function assertDICDefinitionMethodCallAt($pos, $definition, $methodName, array $params = null)
35+
{
36+
$calls = $definition->getMethodCalls();
37+
if (isset($calls[$pos][0])) {
38+
$this->assertEquals($methodName, $calls[$pos][0], "Method '".$methodName."' is expected to be called at position $pos.");
39+
40+
if ($params !== null) {
41+
$this->assertEquals($params, $calls[$pos][1], "Expected parameters to methods '".$methodName."' do not match the actual parameters.");
42+
}
43+
} else {
44+
$this->fail("Method '".$methodName."' is expected to be called at position $pos.");
45+
}
46+
}
47+
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Reference;
17+
18+
abstract class FixtureMonologExtensionTest extends DependencyInjectionTest
19+
{
20+
public function testLoadWithSeveralHandlers()
21+
{
22+
$container = $this->getContainer('multiple_handlers');
23+
24+
$this->assertTrue($container->hasDefinition('monolog.logger'));
25+
$this->assertTrue($container->hasDefinition('monolog.handler.custom'));
26+
$this->assertTrue($container->hasDefinition('monolog.handler.main'));
27+
$this->assertTrue($container->hasDefinition('monolog.handler.nested'));
28+
29+
$logger = $container->getDefinition('monolog.logger');
30+
$this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.custom')));
31+
$this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.main')));
32+
33+
$handler = $container->getDefinition('monolog.handler.custom');
34+
$this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%');
35+
$this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::ERROR, false));
36+
37+
$handler = $container->getDefinition('monolog.handler.main');
38+
$this->assertDICDefinitionClass($handler, '%monolog.handler.fingers_crossed.class%');
39+
$this->assertDICConstructorArguments($handler, array(new Reference('monolog.handler.nested'), \Monolog\Logger::ERROR, 0, true, true));
40+
}
41+
42+
public function testLoadWithOverwriting()
43+
{
44+
$container = $this->getContainer('overwriting');
45+
46+
$this->assertTrue($container->hasDefinition('monolog.logger'));
47+
$this->assertTrue($container->hasDefinition('monolog.handler.custom'));
48+
$this->assertTrue($container->hasDefinition('monolog.handler.main'));
49+
$this->assertTrue($container->hasDefinition('monolog.handler.nested'));
50+
51+
$logger = $container->getDefinition('monolog.logger');
52+
$this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.custom')));
53+
$this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.main')));
54+
55+
$handler = $container->getDefinition('monolog.handler.custom');
56+
$this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%');
57+
$this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::WARNING, true));
58+
59+
$handler = $container->getDefinition('monolog.handler.main');
60+
$this->assertDICDefinitionClass($handler, '%monolog.handler.fingers_crossed.class%');
61+
$this->assertDICConstructorArguments($handler, array(new Reference('monolog.handler.nested'), \Monolog\Logger::ERROR, 0, true, true));
62+
}
63+
64+
public function testLoadWithNewAtEnd()
65+
{
66+
$container = $this->getContainer('new_at_end');
67+
68+
$this->assertTrue($container->hasDefinition('monolog.logger'));
69+
$this->assertTrue($container->hasDefinition('monolog.handler.custom'));
70+
$this->assertTrue($container->hasDefinition('monolog.handler.main'));
71+
$this->assertTrue($container->hasDefinition('monolog.handler.nested'));
72+
$this->assertTrue($container->hasDefinition('monolog.handler.new'));
73+
74+
$logger = $container->getDefinition('monolog.logger');
75+
$this->assertDICDefinitionMethodCallAt(2, $logger, 'pushHandler', array(new Reference('monolog.handler.new')));
76+
$this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.custom')));
77+
$this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.main')));
78+
79+
$handler = $container->getDefinition('monolog.handler.new');
80+
$this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%');
81+
$this->assertDICConstructorArguments($handler, array('/tmp/monolog.log', \Monolog\Logger::ERROR, true));
82+
}
83+
84+
public function testLoadWithNewAndPriority()
85+
{
86+
$container = $this->getContainer('new_and_priority');
87+
88+
$this->assertTrue($container->hasDefinition('monolog.logger'));
89+
$this->assertTrue($container->hasDefinition('monolog.handler.custom'));
90+
$this->assertTrue($container->hasDefinition('monolog.handler.main'));
91+
$this->assertTrue($container->hasDefinition('monolog.handler.nested'));
92+
$this->assertTrue($container->hasDefinition('monolog.handler.first'));
93+
$this->assertTrue($container->hasDefinition('monolog.handler.last'));
94+
95+
$logger = $container->getDefinition('monolog.logger');
96+
$this->assertDICDefinitionMethodCallAt(2, $logger, 'pushHandler', array(new Reference('monolog.handler.last')));
97+
$this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.custom')));
98+
$this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.main')));
99+
$this->assertDICDefinitionMethodCallAt(2, $logger, 'pushHandler', array(new Reference('monolog.handler.first')));
100+
101+
$handler = $container->getDefinition('monolog.handler.main');
102+
$this->assertDICDefinitionClass($handler, '%monolog.handler.buffer.class%');
103+
$this->assertDICConstructorArguments($handler, array(new Reference('monolog.handler.nested'), 0, \Monolog\Logger::INFO, true));
104+
105+
$handler = $container->getDefinition('monolog.handler.first');
106+
$this->assertDICDefinitionClass($handler, '%monolog.handler.rotating_file.class%');
107+
$this->assertDICConstructorArguments($handler, array('/tmp/monolog.log', 0, \Monolog\Logger::ERROR, true));
108+
109+
$handler = $container->getDefinition('monolog.handler.last');
110+
$this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%');
111+
$this->assertDICConstructorArguments($handler, array('/tmp/last.log', \Monolog\Logger::ERROR, true));
112+
}
113+
114+
public function testHandlersWithChannels()
115+
{
116+
$container = $this->getContainer('handlers_with_channels');
117+
118+
$this->assertEquals(
119+
array(
120+
'monolog.handler.custom' => array('type' => 'inclusive', 'elements' => array('foo')),
121+
'monolog.handler.main' => array('type' => 'exclusive', 'elements' => array('foo', 'bar')),
122+
'monolog.handler.extra' => null,
123+
'monolog.handler.more' => array('type' => 'inclusive', 'elements' => array('security', 'doctrine')),
124+
),
125+
$container->getParameter('monolog.handlers_to_channels')
126+
);
127+
}
128+
129+
protected function getContainer($fixture)
130+
{
131+
$container = new ContainerBuilder();
132+
$container->registerExtension(new MonologExtension());
133+
134+
$this->loadFixture($container, $fixture);
135+
136+
$container->getCompilerPassConfig()->setOptimizationPasses(array());
137+
$container->getCompilerPassConfig()->setRemovingPasses(array());
138+
$container->compile();
139+
140+
return $container;
141+
}
142+
143+
abstract protected function loadFixture(ContainerBuilder $container, $fixture);
144+
}

Tests/DependencyInjection/MonologExtensionTest.php

Lines changed: 1 addition & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@
1111

1212
namespace Symfony\Bundle\MonologBundle\Tests\DependencyInjection;
1313

14-
use Symfony\Bundle\MonologBundle\Tests\TestCase;
1514
use Symfony\Bundle\MonologBundle\DependencyInjection\MonologExtension;
1615
use Symfony\Component\DependencyInjection\ContainerBuilder;
1716
use Symfony\Component\DependencyInjection\Reference;
1817

19-
abstract class MonologExtensionTest extends TestCase
18+
class MonologExtensionTest extends DependencyInjectionTest
2019
{
2120
public function testLoadWithDefault()
2221
{
@@ -52,115 +51,6 @@ public function testLoadWithCustomValues()
5251
$this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::ERROR, false));
5352
}
5453

55-
public function testLoadWithSeveralHandlers()
56-
{
57-
$container = $this->getContainer('multiple_handlers');
58-
59-
$this->assertTrue($container->hasDefinition('monolog.logger'));
60-
$this->assertTrue($container->hasDefinition('monolog.handler.custom'));
61-
$this->assertTrue($container->hasDefinition('monolog.handler.main'));
62-
$this->assertTrue($container->hasDefinition('monolog.handler.nested'));
63-
64-
$logger = $container->getDefinition('monolog.logger');
65-
$this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.custom')));
66-
$this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.main')));
67-
68-
$handler = $container->getDefinition('monolog.handler.custom');
69-
$this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%');
70-
$this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::ERROR, false));
71-
72-
$handler = $container->getDefinition('monolog.handler.main');
73-
$this->assertDICDefinitionClass($handler, '%monolog.handler.fingers_crossed.class%');
74-
$this->assertDICConstructorArguments($handler, array(new Reference('monolog.handler.nested'), \Monolog\Logger::ERROR, 0, true, true));
75-
}
76-
77-
public function testLoadWithOverwriting()
78-
{
79-
$container = $this->getContainer('overwriting');
80-
81-
$this->assertTrue($container->hasDefinition('monolog.logger'));
82-
$this->assertTrue($container->hasDefinition('monolog.handler.custom'));
83-
$this->assertTrue($container->hasDefinition('monolog.handler.main'));
84-
$this->assertTrue($container->hasDefinition('monolog.handler.nested'));
85-
86-
$logger = $container->getDefinition('monolog.logger');
87-
$this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.custom')));
88-
$this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.main')));
89-
90-
$handler = $container->getDefinition('monolog.handler.custom');
91-
$this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%');
92-
$this->assertDICConstructorArguments($handler, array('/tmp/symfony.log', \Monolog\Logger::WARNING, true));
93-
94-
$handler = $container->getDefinition('monolog.handler.main');
95-
$this->assertDICDefinitionClass($handler, '%monolog.handler.fingers_crossed.class%');
96-
$this->assertDICConstructorArguments($handler, array(new Reference('monolog.handler.nested'), \Monolog\Logger::ERROR, 0, true, true));
97-
}
98-
99-
public function testLoadWithNewAtEnd()
100-
{
101-
$container = $this->getContainer('new_at_end');
102-
103-
$this->assertTrue($container->hasDefinition('monolog.logger'));
104-
$this->assertTrue($container->hasDefinition('monolog.handler.custom'));
105-
$this->assertTrue($container->hasDefinition('monolog.handler.main'));
106-
$this->assertTrue($container->hasDefinition('monolog.handler.nested'));
107-
$this->assertTrue($container->hasDefinition('monolog.handler.new'));
108-
109-
$logger = $container->getDefinition('monolog.logger');
110-
$this->assertDICDefinitionMethodCallAt(2, $logger, 'pushHandler', array(new Reference('monolog.handler.new')));
111-
$this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.custom')));
112-
$this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.main')));
113-
114-
$handler = $container->getDefinition('monolog.handler.new');
115-
$this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%');
116-
$this->assertDICConstructorArguments($handler, array('/tmp/monolog.log', \Monolog\Logger::ERROR, true));
117-
}
118-
119-
public function testLoadWithNewAndPriority()
120-
{
121-
$container = $this->getContainer('new_and_priority');
122-
123-
$this->assertTrue($container->hasDefinition('monolog.logger'));
124-
$this->assertTrue($container->hasDefinition('monolog.handler.custom'));
125-
$this->assertTrue($container->hasDefinition('monolog.handler.main'));
126-
$this->assertTrue($container->hasDefinition('monolog.handler.nested'));
127-
$this->assertTrue($container->hasDefinition('monolog.handler.first'));
128-
$this->assertTrue($container->hasDefinition('monolog.handler.last'));
129-
130-
$logger = $container->getDefinition('monolog.logger');
131-
$this->assertDICDefinitionMethodCallAt(2, $logger, 'pushHandler', array(new Reference('monolog.handler.last')));
132-
$this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.custom')));
133-
$this->assertDICDefinitionMethodCallAt(0, $logger, 'pushHandler', array(new Reference('monolog.handler.main')));
134-
$this->assertDICDefinitionMethodCallAt(2, $logger, 'pushHandler', array(new Reference('monolog.handler.first')));
135-
136-
$handler = $container->getDefinition('monolog.handler.main');
137-
$this->assertDICDefinitionClass($handler, '%monolog.handler.buffer.class%');
138-
$this->assertDICConstructorArguments($handler, array(new Reference('monolog.handler.nested'), 0, \Monolog\Logger::INFO, true));
139-
140-
$handler = $container->getDefinition('monolog.handler.first');
141-
$this->assertDICDefinitionClass($handler, '%monolog.handler.rotating_file.class%');
142-
$this->assertDICConstructorArguments($handler, array('/tmp/monolog.log', 0, \Monolog\Logger::ERROR, true));
143-
144-
$handler = $container->getDefinition('monolog.handler.last');
145-
$this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%');
146-
$this->assertDICConstructorArguments($handler, array('/tmp/last.log', \Monolog\Logger::ERROR, true));
147-
}
148-
149-
public function testHandlersWithChannels()
150-
{
151-
$container = $this->getContainer('handlers_with_channels');
152-
153-
$this->assertEquals(
154-
array(
155-
'monolog.handler.custom' => array('type' => 'inclusive', 'elements' => array('foo')),
156-
'monolog.handler.main' => array('type' => 'exclusive', 'elements' => array('foo', 'bar')),
157-
'monolog.handler.extra' => null,
158-
'monolog.handler.more' => array('type' => 'inclusive', 'elements' => array('security', 'doctrine')),
159-
),
160-
$container->getParameter('monolog.handlers_to_channels')
161-
);
162-
}
163-
16454
/**
16555
* @expectedException InvalidArgumentException
16656
*/
@@ -238,48 +128,4 @@ public function testExceptionWhenUsingDebugName()
238128

239129
$loader->load(array(array('handlers' => array('debug' => array('type' => 'stream')))), $container);
240130
}
241-
242-
protected function getContainer($fixture)
243-
{
244-
$container = new ContainerBuilder();
245-
$container->registerExtension(new MonologExtension());
246-
247-
$this->loadFixture($container, $fixture);
248-
249-
$container->getCompilerPassConfig()->setOptimizationPasses(array());
250-
$container->getCompilerPassConfig()->setRemovingPasses(array());
251-
$container->compile();
252-
253-
return $container;
254-
}
255-
256-
abstract protected function loadFixture(ContainerBuilder $container, $fixture);
257-
258-
/**
259-
* Assertion on the Class of a DIC Service Definition.
260-
*
261-
* @param \Symfony\Component\DependencyInjection\Definition $definition
262-
* @param string $expectedClass
263-
*/
264-
protected function assertDICDefinitionClass($definition, $expectedClass)
265-
{
266-
$this->assertEquals($expectedClass, $definition->getClass(), "Expected Class of the DIC Container Service Definition is wrong.");
267-
}
268-
269-
protected function assertDICConstructorArguments($definition, $args)
270-
{
271-
$this->assertEquals($args, $definition->getArguments(), "Expected and actual DIC Service constructor arguments of definition '".$definition->getClass()."' don't match.");
272-
}
273-
274-
protected function assertDICDefinitionMethodCallAt($pos, $definition, $methodName, array $params = null)
275-
{
276-
$calls = $definition->getMethodCalls();
277-
if (isset($calls[$pos][0])) {
278-
$this->assertEquals($methodName, $calls[$pos][0], "Method '".$methodName."' is expected to be called at position $pos.");
279-
280-
if ($params !== null) {
281-
$this->assertEquals($params, $calls[$pos][1], "Expected parameters to methods '".$methodName."' do not match the actual parameters.");
282-
}
283-
}
284-
}
285131
}

Tests/DependencyInjection/XmlMonologExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
1717

18-
class XmlMonologExtensionTest extends MonologExtensionTest
18+
class XmlMonologExtensionTest extends FixtureMonologExtensionTest
1919
{
2020
protected function loadFixture(ContainerBuilder $container, $fixture)
2121
{

Tests/DependencyInjection/YamlMonologExtensionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
1717

18-
class YamlMonologExtensionTest extends MonologExtensionTest
18+
class YamlMonologExtensionTest extends FixtureMonologExtensionTest
1919
{
2020
protected function loadFixture(ContainerBuilder $container, $fixture)
2121
{

0 commit comments

Comments
 (0)