Completed
Push — 1.0 ( d216e1...58fc60 )
by David
02:54
created

testContainerWithException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
4
namespace TheCodingMachine\Interop\ServiceProviderBridgeBundle;
5
6
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Definition;
9
10
class SymfonyContainerAdapterTest extends \PHPUnit_Framework_TestCase
11
{
12
13
    public function testContainer()
14
    {
15
        $container = new ContainerBuilder();
16
        $container->setParameter('database_host', 'localhost');
17
18
        $container->compile();
19
20
        $containerAdapter = new SymfonyContainerAdapter($container);
21
22
        $this->assertTrue($containerAdapter->has('database_host'));
23
        $this->assertFalse($containerAdapter->has('not_exists'));
24
    }
25
26
    /**
27
     * @expectedException \TheCodingMachine\Interop\ServiceProviderBridgeBundle\Exception\NotFoundException
28
     */
29
    public function testContainerNotFound()
30
    {
31
        $container = new ContainerBuilder();
32
        $container->compile();
33
        $containerAdapter = new SymfonyContainerAdapter($container);
34
35
        $containerAdapter->get('not_found');
36
    }
37
38
    /**
39
     * @expectedException \TheCodingMachine\Interop\ServiceProviderBridgeBundle\Exception\ContainerException
40
     */
41
    public function testContainerWithException()
42
    {
43
        $container = new ContainerBuilder();
44
        $definition = new Definition(\stdClass::class);
45
        $definition->setFactory(self::class, "exceptionFactory");
0 ignored issues
show
Unused Code introduced by
The call to Definition::setFactory() has too many arguments starting with 'exceptionFactory'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
46
        $container->setDefinition('mydef', $definition);
47
        $container->compile();
48
        $containerAdapter = new SymfonyContainerAdapter($container);
49
50
        $containerAdapter->get('mydef');
51
    }
52
53
    public static function exceptionFactory()
54
    {
55
        throw new \Exception('Boom!');
56
    }
57
58
}
59