1 | <?php |
||||
2 | |||||
3 | namespace Wonderland\Container\Tests; |
||||
4 | |||||
5 | use PHPUnit\Framework\MockObject\MockObject; |
||||
6 | use PHPUnit\Framework\TestCase; |
||||
7 | use Wonderland\Container\Exception\DuplicatedServiceException; |
||||
8 | use Wonderland\Container\Service\InstanceDefinition; |
||||
9 | use Wonderland\Container\Service\ServiceDefinition; |
||||
10 | use Wonderland\Container\Service\ServiceInstanceInterface; |
||||
11 | use Wonderland\Container\ServiceContainer; |
||||
12 | use Wonderland\Container\Service\ServiceDefinitionInterface; |
||||
13 | |||||
14 | /** |
||||
15 | * Class ContainerTest |
||||
16 | * @package Wonderland\Container\Tests |
||||
17 | * @author Alice Praud <[email protected]> |
||||
18 | */ |
||||
19 | class ServiceContainerTest extends TestCase |
||||
20 | { |
||||
21 | private const SERVICE_NAME = 'service.name'; |
||||
22 | private const DEFAULT_SERVICE_NANE = 'default.service.name'; |
||||
23 | |||||
24 | /** @var ServiceContainer|MockObject */ |
||||
25 | private $container; |
||||
26 | |||||
27 | /** @var ServiceContainer */ |
||||
28 | private $realContainer; |
||||
29 | |||||
30 | /** |
||||
31 | * @throws DuplicatedServiceException |
||||
32 | */ |
||||
33 | protected function setUp() |
||||
34 | { |
||||
35 | $this->container = $this->getMockBuilder(ServiceContainer::class)->getMock(); |
||||
36 | $this->realContainer = new ServiceContainer(); |
||||
37 | $this->realContainer->addService(new ServiceDefinition(self::DEFAULT_SERVICE_NANE, \DateTime::class)); |
||||
38 | } |
||||
39 | |||||
40 | public function testConstructor() |
||||
41 | { |
||||
42 | $this->assertAttributeEquals([], 'serviceInstances', $this->container); |
||||
43 | } |
||||
44 | |||||
45 | /** |
||||
46 | * @covers \Wonderland\Container\ServiceContainer::addService |
||||
47 | * @throws DuplicatedServiceException |
||||
48 | */ |
||||
49 | public function test_AddService() |
||||
50 | { |
||||
51 | $definition = $this->getMockBuilder(ServiceDefinition::class) |
||||
52 | ->setConstructorArgs([self::SERVICE_NAME, \DateTime::class]) |
||||
53 | ->getMock(); |
||||
54 | ; |
||||
55 | |||||
56 | $definition->expects($this->exactly(2)) |
||||
57 | ->method('getServiceName') |
||||
58 | ->willReturn(self::SERVICE_NAME); |
||||
59 | |||||
60 | $this->assertSame($this->realContainer, $this->realContainer->addService($definition)); |
||||
61 | } |
||||
62 | |||||
63 | /** |
||||
64 | * @throws DuplicatedServiceException |
||||
65 | */ |
||||
66 | public function test_AddServiceException() |
||||
67 | { |
||||
68 | $this->expectException(DuplicatedServiceException::class); |
||||
69 | |||||
70 | $definition = $this->getMockBuilder(ServiceDefinition::class) |
||||
71 | ->setConstructorArgs([self::SERVICE_NAME, \DateTime::class]) |
||||
72 | ->getMock(); |
||||
73 | ; |
||||
74 | |||||
75 | $definition->expects($this->exactly(4)) |
||||
76 | ->method('getServiceName') |
||||
77 | ->willReturn(self::SERVICE_NAME); |
||||
78 | |||||
79 | $this->realContainer->addService($definition); |
||||
80 | $this->realContainer->addService($definition); |
||||
81 | } |
||||
82 | |||||
83 | public function test_loadServices() |
||||
84 | { |
||||
85 | $definition = $this->getMockBuilder(ServiceDefinition::class) |
||||
86 | ->setConstructorArgs([self::SERVICE_NAME . '10', \DateTime::class]) |
||||
87 | ->getMock(); |
||||
88 | ; |
||||
89 | $definition2 = $this->getMockBuilder(ServiceDefinition::class) |
||||
90 | ->setConstructorArgs([self::SERVICE_NAME . '20', \DateTime::class]) |
||||
91 | ->getMock(); |
||||
92 | ; |
||||
93 | |||||
94 | $list = [ |
||||
95 | $definition, |
||||
96 | $definition2 |
||||
97 | ]; |
||||
98 | |||||
99 | $definition->expects($this->exactly(2)) |
||||
100 | ->method('getServiceName') |
||||
101 | ->willReturn(self::SERVICE_NAME . '10'); |
||||
102 | $definition2->expects($this->exactly(2)) |
||||
103 | ->method('getServiceName') |
||||
104 | ->willReturn(self::SERVICE_NAME . '20'); |
||||
105 | |||||
106 | $this->realContainer->loadServices($list); |
||||
107 | } |
||||
108 | |||||
109 | /** |
||||
110 | * @throws DuplicatedServiceException |
||||
111 | */ |
||||
112 | public function test_AddServiceInstance() |
||||
113 | { |
||||
114 | $definition = $this->getMockBuilder(InstanceDefinition::class) |
||||
115 | ->setConstructorArgs([self::SERVICE_NAME, new \DateTime()]) |
||||
116 | ->getMock(); |
||||
117 | |||||
118 | $definition->expects($this->atLeast(2)) |
||||
119 | ->method('getServiceName') |
||||
120 | ->willReturn(self::SERVICE_NAME); |
||||
121 | |||||
122 | $this->container->expects($this->once()) |
||||
0 ignored issues
–
show
|
|||||
123 | ->method('addServiceInstance') |
||||
124 | ->willReturn($this->container); |
||||
125 | |||||
126 | $this->assertSame($this->container, $this->container->addServiceInstance($definition)); |
||||
0 ignored issues
–
show
The method
addServiceInstance() does not exist on PHPUnit\Framework\MockObject\MockObject .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||
127 | $this->assertSame($this->realContainer, $this->realContainer->addServiceInstance($definition)); |
||||
128 | } |
||||
129 | |||||
130 | /** |
||||
131 | * @throws DuplicatedServiceException |
||||
132 | */ |
||||
133 | public function test_AddServiceInstanceException() |
||||
134 | { |
||||
135 | $this->expectException(DuplicatedServiceException::class); |
||||
136 | |||||
137 | $definition = $this->getMockBuilder(InstanceDefinition::class) |
||||
138 | ->setConstructorArgs([self::SERVICE_NAME, new \DateTime()]) |
||||
139 | ->getMock(); |
||||
140 | |||||
141 | $definition->expects($this->exactly(2)) |
||||
142 | ->method('getServiceName') |
||||
143 | ->willReturn(self::DEFAULT_SERVICE_NANE); |
||||
144 | |||||
145 | $this->realContainer->addServiceInstance($definition); |
||||
146 | } |
||||
147 | |||||
148 | /** |
||||
149 | * @throws DuplicatedServiceException |
||||
150 | */ |
||||
151 | public function test_get() |
||||
152 | { |
||||
153 | $this->assertSame(null, $this->realContainer->get('test')); |
||||
154 | $this->assertSame(\DateTime::class, get_class($this->realContainer->get(self::DEFAULT_SERVICE_NANE))); |
||||
155 | $instance = $this->realContainer->get(self::DEFAULT_SERVICE_NANE); |
||||
156 | $this->assertSame(\DateTime::class, get_class($instance)); |
||||
157 | $this->assertSame($instance, $this->realContainer->get(self::DEFAULT_SERVICE_NANE)); |
||||
158 | |||||
159 | $this->realContainer->addServiceInstance( |
||||
160 | new InstanceDefinition('call.format', 'd-m-Y') |
||||
161 | ); |
||||
162 | $this->realContainer->addServiceInstance( |
||||
163 | new InstanceDefinition('call.date', '1970-01-01') |
||||
164 | ); |
||||
165 | |||||
166 | $this->realContainer->addService( |
||||
167 | new ServiceDefinition( |
||||
168 | 'service.call', |
||||
169 | \DateTime::class, |
||||
170 | ['@call.date'], |
||||
171 | ['format' => ['@call.format']] |
||||
172 | ) |
||||
173 | ); |
||||
174 | |||||
175 | $this->realContainer->addService( |
||||
176 | new ServiceDefinition( |
||||
177 | 'service.call2', |
||||
178 | \DateTime::class, |
||||
179 | ['1970-01-01'], |
||||
180 | ['format' => ['Y-m-d']] |
||||
181 | ) |
||||
182 | ); |
||||
183 | |||||
184 | $date = new \DateTime('1970-01-01'); |
||||
185 | $this->assertSame($date->format('m-d-Y'), $this->realContainer->get('service.call')->format('m-d-Y')); |
||||
186 | $this->assertSame($date->format('m-d-Y'), $this->realContainer->get('service.call2')->format('m-d-Y')); |
||||
187 | } |
||||
188 | |||||
189 | public function test_has() |
||||
190 | { |
||||
191 | $this->assertSame(false, $this->realContainer->has('fake')); |
||||
192 | $this->assertSame(true, $this->realContainer->has(self::DEFAULT_SERVICE_NANE)); |
||||
193 | $this->assertSame(true, $this->realContainer->has(self::DEFAULT_SERVICE_NANE)); |
||||
194 | |||||
195 | $this->realContainer->addServiceInstance(new InstanceDefinition('instance.name', \DateTime::class)); |
||||
196 | $this->assertSame(true, $this->realContainer->has('instance.name')); |
||||
197 | } |
||||
198 | |||||
199 | } |
||||
200 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.