Completed
Pull Request — master (#46)
by
unknown
05:21
created

ServiceContainerSpec::it_can_store_parameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace spec\Coduo\TuTu;
4
5
use PhpSpec\ObjectBehavior;
6
use Prophecy\Argument;
7
8
class ServiceContainerSpec extends ObjectBehavior
9
{
10
    function it_can_store_parameters()
11
    {
12
        $this->hasParameter('key')->shouldreturn(false);
13
        $this->setParameter('key', 'value');
14
        $this->hasParameter('key')->shouldReturn(true);
15
        $this->getParameter('key')->shouldReturn('value');
16
    }
17
18
    function it_throws_exception_when_parameter_does_not_exist()
19
    {
20
        $this->shouldThrow(new \RuntimeException("Service container does not have parameter with id \"key\""))
21
            ->during('getParameter', ['key']);
22
    }
23
24
    function it_create_service_from_definition()
25
    {
26
        $this->hasService('service')->shouldReturn(false);
27
        $this->setDefinition('service', function($container) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
            return new \stdClass();
29
        });
30
        $this->hasService('service')->shouldReturn(true);
31
        $this->getService('service')->shouldReturnAnInstanceOf('stdClass');
32
    }
33
34
    function it_throws_exception_when_service_does_not_exist()
35
    {
36
        $this->shouldThrow(new \RuntimeException("Service container does not have service with id \"key\""))
37
            ->during('getService', ['key']);
38
    }
39
40
    function it_create_new_service_each_time()
41
    {
42
        $this->setDefinition('service', function($container) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
            return new \stdClass();
44
        });
45
46
        $service1 = $this->getService('service');
47
        $service2 = $this->getService('service');
48
49
        $service1->shouldNotBe($service2);
50
    }
51
52
    function it_can_return_same_object_each_time_when_service_definition_is_static()
53
    {
54
        $this->setStaticDefinition('service', function($container) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
            return new \stdClass();
56
        });
57
58
        $service1 = $this->getService('service');
59
        $service2 = $this->getService('service');
60
61
        $service1->shouldBe($service2);
62
    }
63
64
    function it_can_return_services_by_tags()
65
    {
66
        $this->setDefinition(
67
            'service_0',
68
            function($container) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69
                return new \stdClass();
70
            },
71
            ['foo']
72
        );
73
        $this->setDefinition(
74
            'service_1',
75
            function($container) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
                return new \DateTime();
77
            },
78
            ['foo']
79
        );
80
81
        $services = $this->getServicesByTag('foo');
82
        $services[0]->shouldReturnAnInstanceOf('stdClass');
83
        $services[1]->shouldReturnAnInstanceOf('DateTime');
84
    }
85
86
    function it_return_empty_array_when_cant_find_services_by_tag()
87
    {
88
        $this->getServicesByTag('foo')->shouldHaveCount(0);
89
    }
90
91
    function it_can_remove_service()
92
    {
93
        $this->setDefinition(
94
            'service',
95
            function($container) {
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
96
                return new \stdClass();
97
            },
98
            ['foo']
99
        );
100
        $this->removeService('service');
101
        $this->hasService('service')->shouldReturn(false);
102
    }
103
}
104