SymfonyContainerAdapterTest   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 5

Importance

Changes 0
Metric Value
wmc 1
dl 0
loc 47
c 0
b 0
f 0
cbo 5
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testContainer() 0 12 1
A testContainerNotFound() 0 10 1
A testContainerWithException() 0 13 1
A exceptionFactory() 0 4 1
1
<?php
2
3
4
namespace TheCodingMachine\Interop\ServiceProviderBridgeBundle;
5
6
7
use PHPUnit\Framework\TestCase;
8
use Psr\Container\ContainerExceptionInterface;
9
use Psr\Container\NotFoundExceptionInterface;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Definition;
12
13
class SymfonyContainerAdapterTest extends TestCase
14
{
15
16
    public function testContainer()
17
    {
18
        $container = new ContainerBuilder();
19
        $container->setParameter('database_host', 'localhost');
20
21
        $container->compile();
22
23
        $containerAdapter = new SymfonyContainerAdapter($container);
24
25
        $this->assertTrue($containerAdapter->has('database_host'));
26
        $this->assertFalse($containerAdapter->has('not_exists'));
27
    }
28
29
    public function testContainerNotFound()
30
    {
31
        $container = new ContainerBuilder();
32
        $container->compile();
33
        $containerAdapter = new SymfonyContainerAdapter($container);
34
35
        $this->expectException(NotFoundExceptionInterface::class);
36
37
        $containerAdapter->get('not_found');
38
    }
39
40
    public function testContainerWithException()
41
    {
42
        $container = new ContainerBuilder();
43
        $definition = new Definition(\stdClass::class);
44
        $definition->setFactory([self::class, 'exceptionFactory']);
45
        $container->setDefinition('mydef', $definition);
46
        $container->compile();
47
        $containerAdapter = new SymfonyContainerAdapter($container);
48
49
        $this->expectException(ContainerExceptionInterface::class);
50
51
        $containerAdapter->get('mydef');
52
    }
53
54
    public static function exceptionFactory()
55
    {
56
        throw new class('Boom!') extends \Exception implements ContainerExceptionInterface {};
57
    }
58
59
}
60