Completed
Push — master ( ea4e28...f4a6ba )
by Eric
08:48
created

RegistryTest::testOffsetExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
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\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()];
0 ignored issues
show
Documentation Bug introduced by
It seems like array('foo' => $this->cr...s->createServiceMock()) of type array<string,object<PHPU...l\\ServiceInterface>"}> 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...
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()
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...
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
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...
158
{
159
}
160