1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the CrossContainerExtension package. |
5
|
|
|
* |
6
|
|
|
* (c) Kamil Kokot <[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 spec\FriendsOfBehat\CrossContainerExtension; |
13
|
|
|
|
14
|
|
|
use FriendsOfBehat\CrossContainerExtension\ContainerAccessor; |
15
|
|
|
use PhpSpec\ObjectBehavior; |
16
|
|
|
use Symfony\Component\DependencyInjection\Container; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
18
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; |
19
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
20
|
|
|
|
21
|
|
|
final class KernelBasedContainerAccessorSpec extends ObjectBehavior |
22
|
|
|
{ |
23
|
|
|
function let(KernelInterface $kernel) |
24
|
|
|
{ |
25
|
|
|
$this->beConstructedWith($kernel); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
function it_is_a_container_accessor() |
29
|
|
|
{ |
30
|
|
|
$this->shouldImplement(ContainerAccessor::class); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
function it_gets_a_service(KernelInterface $kernel, Container $container) |
34
|
|
|
{ |
35
|
|
|
$service = new \stdClass(); |
36
|
|
|
|
37
|
|
|
$kernel->getContainer()->willReturn($container); |
38
|
|
|
$container->get('acme')->willReturn($service); |
39
|
|
|
|
40
|
|
|
$this->getService('acme')->shouldReturn($service); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
function it_throws_an_exception_if_could_not_get_service(KernelInterface $kernel, ContainerInterface $container) |
44
|
|
|
{ |
45
|
|
|
$kernel->getContainer()->willReturn($container); |
46
|
|
|
|
47
|
|
|
$this->shouldThrow(\DomainException::class)->during('getService', ['acme']); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
function it_gets_parameters_from_frozen_container(KernelInterface $kernel, Container $container) |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$kernel->getContainer()->willReturn($container); |
53
|
|
|
|
54
|
|
|
$container->isFrozen()->willReturn(true); |
55
|
|
|
$container->getParameterBag()->willReturn(new ParameterBag(['name' => 'value'])); |
56
|
|
|
|
57
|
|
|
$this->getParameters()->shouldReturn(['name' => 'value']); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
function it_gets_parameters_from_not_frozen_container(KernelInterface $kernel, Container $container) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$kernel->getContainer()->willReturn($container); |
63
|
|
|
|
64
|
|
|
$container->isFrozen()->willReturn(false); |
65
|
|
|
$container->getParameterBag()->willReturn(new ParameterBag(['name' => 'value'])); |
66
|
|
|
|
67
|
|
|
$this->getParameters()->shouldReturn(['name' => 'value']); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
function it_throws_an_exception_if_could_not_get_parameters(KernelInterface $kernel, ContainerInterface $container) |
71
|
|
|
{ |
72
|
|
|
$kernel->getContainer()->willReturn($container); |
73
|
|
|
|
74
|
|
|
$this->shouldThrow(\DomainException::class)->during('getParameters'); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.