|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Lug package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Lug\Component\Registry\Tests\Model; |
|
13
|
|
|
|
|
14
|
|
|
use Lug\Component\Registry\Model\Registry; |
|
15
|
|
|
use Lug\Component\Registry\Model\RegistryInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @author GeLo <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class RegistryTest extends \PHPUnit_Framework_TestCase |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var Registry |
|
24
|
|
|
*/ |
|
25
|
|
|
private $registry; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var ServiceInterface[] |
|
29
|
|
|
*/ |
|
30
|
|
|
private $services; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritdoc} |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function setUp() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->services = ['foo' => $this->createServiceMock(), 'bar' => $this->createServiceMock()]; |
|
|
|
|
|
|
38
|
|
|
$this->registry = new Registry(ServiceInterface::class, $this->services); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function testInheritance() |
|
42
|
|
|
{ |
|
43
|
|
|
$this->assertInstanceOf(RegistryInterface::class, $this->registry); |
|
44
|
|
|
$this->assertInstanceOf(\ArrayAccess::class, $this->registry); |
|
45
|
|
|
$this->assertInstanceOf(\Countable::class, $this->registry); |
|
46
|
|
|
$this->assertInstanceOf(\IteratorAggregate::class, $this->registry); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testDefaultState() |
|
50
|
|
|
{ |
|
51
|
|
|
$this->registry = new Registry(ServiceInterface::class); |
|
52
|
|
|
|
|
53
|
|
|
$this->assertEmpty(iterator_to_array($this->registry)); |
|
54
|
|
|
$this->assertCount(0, $this->registry); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testInitialState() |
|
58
|
|
|
{ |
|
59
|
|
|
$this->assertSame($this->services, iterator_to_array($this->registry)); |
|
60
|
|
|
$this->assertCount(count($this->services), $this->registry); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function testOffsetExists() |
|
64
|
|
|
{ |
|
65
|
|
|
foreach (array_keys($this->services) as $type) { |
|
66
|
|
|
$this->assertTrue(isset($this->registry[$type])); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$this->assertFalse(isset($this->registry['baz'])); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function testOffsetGet() |
|
73
|
|
|
{ |
|
74
|
|
|
foreach ($this->services as $type => $service) { |
|
75
|
|
|
$this->assertSame($service, $this->registry[$type]); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @expectedException \Lug\Component\Registry\Exception\ServiceNotFoundException |
|
81
|
|
|
* @expectedExceptionMessage The service "baz" could not be found. |
|
82
|
|
|
*/ |
|
83
|
|
|
public function testOffsetGetWithNonExistingService() |
|
84
|
|
|
{ |
|
85
|
|
|
$this->registry['baz']; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testOffsetSet() |
|
89
|
|
|
{ |
|
90
|
|
|
$this->registry[$type = 'baz'] = $service = $this->createServiceMock(); |
|
91
|
|
|
|
|
92
|
|
|
$this->assertSame($service, $this->registry[$type]); |
|
93
|
|
|
$this->assertSame(array_merge($this->services, [$type => $service]), iterator_to_array($this->registry)); |
|
94
|
|
|
$this->assertCount(count($this->services) + 1, $this->registry); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @expectedException \Lug\Component\Registry\Exception\ServiceAlreadyExistsException |
|
99
|
|
|
* @expectedExceptionMessage The service "foo" already exists. |
|
100
|
|
|
*/ |
|
101
|
|
|
public function testOffsetSetWithExistingService() |
|
102
|
|
|
{ |
|
103
|
|
|
$this->registry['foo'] = $this->createServiceMock(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @expectedException \Lug\Component\Registry\Exception\InvalidServiceException |
|
108
|
|
|
* @expectedExceptionMessage The service for the registry "Lug\Component\Registry\Model\Registry" must be an instance of "Lug\Component\Registry\Tests\Model\ServiceInterface", got "string". |
|
109
|
|
|
*/ |
|
110
|
|
|
public function testOffsetSetWithScalarService() |
|
111
|
|
|
{ |
|
112
|
|
|
$this->registry['baz'] = 'baz'; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @expectedException \Lug\Component\Registry\Exception\InvalidServiceException |
|
117
|
|
|
* @expectedExceptionMessage The service for the registry "Lug\Component\Registry\Model\Registry" must be an instance of "Lug\Component\Registry\Tests\Model\ServiceInterface", got "stdClass". |
|
118
|
|
|
*/ |
|
119
|
|
|
public function testOffsetSetWithInvalidService() |
|
120
|
|
|
{ |
|
121
|
|
|
$this->registry['baz'] = new \stdClass(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
View Code Duplication |
public function testOffsetUnset() |
|
|
|
|
|
|
125
|
|
|
{ |
|
126
|
|
|
foreach (array_keys($this->services) as $type) { |
|
127
|
|
|
unset($this->registry[$type]); |
|
128
|
|
|
|
|
129
|
|
|
$this->assertFalse(isset($this->registry[$type])); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$this->assertEmpty(iterator_to_array($this->registry)); |
|
133
|
|
|
$this->assertCount(0, $this->registry); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @expectedException \Lug\Component\Registry\Exception\ServiceNotFoundException |
|
138
|
|
|
* @expectedExceptionMessage The service "baz" could not be found. |
|
139
|
|
|
*/ |
|
140
|
|
|
public function testOffsetUnsetWithNonExistingService() |
|
141
|
|
|
{ |
|
142
|
|
|
unset($this->registry['baz']); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject|ServiceInterface |
|
147
|
|
|
*/ |
|
148
|
|
|
private function createServiceMock() |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->getMock(ServiceInterface::class); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @author GeLo <[email protected]> |
|
156
|
|
|
*/ |
|
157
|
|
|
interface ServiceInterface |
|
|
|
|
|
|
158
|
|
|
{ |
|
159
|
|
|
} |
|
160
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..