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
|
|
|
|