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) { |
|
|
|
|
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) { |
|
|
|
|
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) { |
|
|
|
|
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) { |
|
|
|
|
69
|
|
|
return new \stdClass(); |
70
|
|
|
}, |
71
|
|
|
['foo'] |
72
|
|
|
); |
73
|
|
|
$this->setDefinition( |
74
|
|
|
'service_1', |
75
|
|
|
function($container) { |
|
|
|
|
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) { |
|
|
|
|
96
|
|
|
return new \stdClass(); |
97
|
|
|
}, |
98
|
|
|
['foo'] |
99
|
|
|
); |
100
|
|
|
$this->removeService('service'); |
101
|
|
|
$this->hasService('service')->shouldReturn(false); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.