Completed
Pull Request — master (#18)
by Eric
33:48
created

LazyRegistryTest::testDefaultState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
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\Bundle\RegistryBundle\Tests\Model;
13
14
use Lug\Bundle\RegistryBundle\Model\LazyRegistry;
15
use Lug\Bundle\RegistryBundle\Model\LazyRegistryInterface;
16
use Lug\Component\Registry\Model\Registry;
17
use Lug\Component\Registry\Model\RegistryInterface;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
20
/**
21
 * @author GeLo <[email protected]>
22
 */
23
class LazyRegistryTest extends \PHPUnit_Framework_TestCase
24
{
25
    /**
26
     * @var LazyRegistry
27
     */
28
    private $lazyRegistry;
29
30
    /**
31
     * @var Registry|\PHPUnit_Framework_MockObject_MockObject
32
     */
33
    private $registry;
34
35
    /**
36
     * @var ContainerInterface|\PHPUnit_Framework_MockObject_MockObject
37
     */
38
    private $container;
39
40
    /**
41
     * @var string[]
42
     */
43
    private $lazyServices;
44
45
    /**
46
     * @var ServiceInterface[]
47
     */
48
    private $containerServices;
49
50
    /**
51
     * @var ServiceInterface[]
52
     */
53
    private $services;
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    protected function setUp()
59
    {
60
        $this->services = ['foo' => $this->createServiceMock(), 'bar' => $this->createServiceMock()];
0 ignored issues
show
Documentation Bug introduced by
It seems like array('foo' => $this->cr...s->createServiceMock()) of type array<string,object<Lug\...ckObject_MockObject>"}> is incompatible with the declared type array<integer,object<Lug...odel\ServiceInterface>> of property $services.

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

Loading history...
61
        $this->containerServices = ['my.baz' => $this->createServiceMock(), 'my.bat' => $this->createServiceMock()];
0 ignored issues
show
Documentation Bug introduced by
It seems like array('my.baz' => $this-...s->createServiceMock()) of type array<string,object<Lug\...ckObject_MockObject>"}> is incompatible with the declared type array<integer,object<Lug...odel\ServiceInterface>> of property $containerServices.

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

Loading history...
62
        $this->lazyServices = ['baz' => 'my.baz', 'bat' => 'my.bat'];
0 ignored issues
show
Documentation Bug introduced by
It seems like array('baz' => 'my.baz', 'bat' => 'my.bat') of type array<string,string,{"ba...tring","bat":"string"}> is incompatible with the declared type array<integer,string> of property $lazyServices.

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

Loading history...
63
        $this->container = $this->createContainerMock();
64
        $this->registry = new Registry(ServiceInterface::class, $this->services);
65
66
        $this->lazyRegistry = new LazyRegistry(
67
            $this->container,
68
            $this->registry,
69
            $this->lazyServices
70
        );
71
    }
72
73
    public function testInheritance()
74
    {
75
        $this->assertInstanceOf(RegistryInterface::class, $this->lazyRegistry);
76
        $this->assertInstanceOf(LazyRegistryInterface::class, $this->lazyRegistry);
77
    }
78
79
    public function testDefaultState()
80
    {
81
        $this->lazyRegistry = new LazyRegistry(
82
            $this->container,
83
            new Registry(ServiceInterface::class)
84
        );
85
86
        $this->assertEmpty(iterator_to_array($this->lazyRegistry));
87
        $this->assertCount(0, $this->lazyRegistry);
88
    }
89
90
    public function testInitialState()
91
    {
92
        $services = array_merge(
93
            $this->services,
94
            array_combine(array_flip($this->lazyServices), $this->containerServices)
95
        );
96
97
        $this->assertSame($services, iterator_to_array($this->lazyRegistry));
98
        $this->assertCount(count($services), $this->lazyRegistry);
99
    }
100
101
    public function testHasLazy()
102
    {
103
        foreach (array_keys($this->lazyServices) as $type) {
104
            $this->assertTrue($this->lazyRegistry->hasLazy($type));
105
        }
106
107
        foreach (array_keys($this->services) as $type) {
108
            $this->assertFalse($this->lazyRegistry->hasLazy($type));
109
        }
110
    }
111
112
    public function testGetLazy()
113
    {
114
        foreach ($this->lazyServices as $type => $service) {
115
            $this->assertSame($service, $this->lazyRegistry->getLazy($type));
116
        }
117
    }
118
119
    /**
120
     * @expectedException \Lug\Bundle\RegistryBundle\Exception\LazyServiceNotFoundException
121
     * @expectedExceptionMessage The lazy service "foo" could not be found.
122
     */
123
    public function testGetLazyWithNonExistingLazy()
124
    {
125
        $this->lazyRegistry->getLazy('foo');
126
    }
127
128
    public function testSetLazy()
129
    {
130
        $this->containerServices[$containerService = 'my.ban'] = $service = $this->createServiceMock();
0 ignored issues
show
Unused Code introduced by
$service is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
131
132
        $this->container = $this->createContainerMock();
133
        $this->registry = new Registry(ServiceInterface::class, $this->services);
134
135
        $this->lazyRegistry = new LazyRegistry(
136
            $this->container,
137
            $this->registry,
138
            $this->lazyServices
139
        );
140
141
        $this->lazyServices[$type = 'ban'] = $containerService;
142
        $this->lazyRegistry->setLazy($type, $containerService);
143
144
        $this->assertTrue($this->lazyRegistry->hasLazy($type));
145
        $this->assertSame($containerService, $this->lazyRegistry->getLazy($type));
146
147
        $services = array_merge(
148
            $this->services,
149
            array_combine(array_flip($this->lazyServices), $this->containerServices)
150
        );
151
152
        $this->assertSame($services, iterator_to_array($this->lazyRegistry));
153
        $this->assertCount(count($services), $this->lazyRegistry);
154
    }
155
156
    /**
157
     * @expectedException \Lug\Bundle\RegistryBundle\Exception\LazyServiceAlreadyExistsException
158
     * @expectedExceptionMessage The lazy service "baz" already exists.
159
     */
160
    public function testSetLazyWithExistingLazy()
161
    {
162
        $this->lazyRegistry->setLazy('baz', 'my.baz');
163
    }
164
165
    public function testRemoveLazy()
166
    {
167
        $this->testInitialState();
168
169
        foreach (array_keys($this->lazyServices) as $type) {
170
            $this->lazyRegistry->removeLazy($type);
171
        }
172
173
        $this->assertSame($this->services, iterator_to_array($this->lazyRegistry));
174
        $this->assertCount(count($this->services), $this->lazyRegistry);
175
    }
176
177
    /**
178
     * @expectedException \Lug\Bundle\RegistryBundle\Exception\LazyServiceNotFoundException
179
     * @expectedExceptionMessage The lazy service "ban" could not be found.
180
     */
181
    public function testRemoveLazyWithNonExistingLazy()
182
    {
183
        $this->lazyRegistry->removeLazy('ban');
184
    }
185
186
    public function testOffsetExists()
187
    {
188
        foreach (array_keys(array_merge($this->services, $this->lazyServices)) as $type) {
189
            $this->assertTrue(isset($this->lazyRegistry[$type]));
190
        }
191
192
        $this->assertFalse(isset($this->lazyRegistry['ban']));
193
    }
194
195
    public function testOffsetGet()
196
    {
197
        $services = array_merge(
198
            $this->services,
199
            array_combine(array_flip($this->lazyServices), $this->containerServices)
200
        );
201
202
        foreach ($services as $type => $service) {
203
            $this->assertSame($service, $this->lazyRegistry[$type]);
204
        }
205
    }
206
207
    /**
208
     * @expectedException \Lug\Bundle\RegistryBundle\Exception\LazyServiceNotFoundException
209
     * @expectedExceptionMessage The lazy service "ban" could not be found.
210
     */
211
    public function testOffsetGetWithNonExistingService()
212
    {
213
        $this->lazyRegistry['ban'];
214
    }
215
216
    public function testOffsetSet()
217
    {
218
        $this->lazyRegistry[$type = 'ban'] = $service = $this->createServiceMock();
219
220
        $this->assertSame($service, $this->lazyRegistry[$type]);
221
222
        $services = array_merge(
223
            $this->services,
224
            [$type => $service],
225
            array_combine(array_flip($this->lazyServices), $this->containerServices)
226
        );
227
228
        $this->assertSame($services, iterator_to_array($this->lazyRegistry));
229
        $this->assertCount(count($services), $this->lazyRegistry);
230
    }
231
232
    /**
233
     * @expectedException \Lug\Component\Registry\Exception\ServiceAlreadyExistsException
234
     * @expectedExceptionMessage The service "baz" already exists.
235
     */
236
    public function testOffsetSetWithExistingService()
237
    {
238
        $this->lazyRegistry['baz'] = $this->createServiceMock();
239
    }
240
241 View Code Duplication
    public function testOffsetUnset()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
242
    {
243
        foreach (array_keys(array_merge($this->services, $this->lazyServices)) as $type) {
244
            unset($this->lazyRegistry[$type]);
245
246
            $this->assertFalse(isset($this->lazyRegistry[$type]));
247
        }
248
249
        $this->assertEmpty(iterator_to_array($this->lazyRegistry));
250
        $this->assertCount(0, $this->lazyRegistry);
251
    }
252
253
    /**
254
     * @return ContainerInterface|\PHPUnit_Framework_MockObject_MockObject
255
     */
256
    private function createContainerMock()
257
    {
258
        $hasMap = [];
259
        $getMap = [];
260
261
        foreach ($this->containerServices as $id => $service) {
262
            $hasMap[] = [$id, true];
263
            $getMap[] = [$id, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $service];
264
        }
265
266
        $container = $this->getMock(ContainerInterface::class);
267
        $container
268
            ->expects($this->any())
269
            ->method('has')
270
            ->will($this->returnValueMap($hasMap));
271
272
        $container
273
            ->expects($this->any())
274
            ->method('get')
275
            ->will($this->returnValueMap($getMap));
276
277
        return $container;
278
    }
279
280
    /**
281
     * @return ServiceInterface|\PHPUnit_Framework_MockObject_MockObject
282
     */
283
    private function createServiceMock()
284
    {
285
        return $this->getMock(ServiceInterface::class);
286
    }
287
}
288
289
/**
290
 * @author GeLo <[email protected]>
291
 */
292
interface ServiceInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
293
{
294
}
295