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
|
|
|
protected function setUp() |
31
|
|
|
{ |
32
|
|
|
$this->container = $this->getMockBuilder(ServiceContainer::class)->getMock(); |
33
|
|
|
$this->realContainer = new ServiceContainer(); |
34
|
|
|
$this->realContainer->addService(new ServiceDefinition(self::DEFAULT_SERVICE_NANE, \DateTime::class)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testConstructor() |
38
|
|
|
{ |
39
|
|
|
$this->assertAttributeEquals([], 'serviceInstances', $this->container); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @covers \Wonderland\Container\ServiceContainer::addService |
44
|
|
|
*/ |
45
|
|
|
public function test_AddService() |
46
|
|
|
{ |
47
|
|
|
$definition = $this->getMockBuilder(ServiceDefinition::class) |
48
|
|
|
->setConstructorArgs([self::SERVICE_NAME, \DateTime::class]) |
49
|
|
|
->getMock(); |
50
|
|
|
; |
51
|
|
|
|
52
|
|
|
$definition->expects($this->once()) |
53
|
|
|
->method('getServiceName') |
54
|
|
|
->willReturn(self::SERVICE_NAME); |
55
|
|
|
|
56
|
|
|
$this->container->expects($this->once()) |
|
|
|
|
57
|
|
|
->method('addService') |
58
|
|
|
->willReturn($this->container); |
59
|
|
|
|
60
|
|
|
$this->assertSame($this->container, $this->container->addService($definition)); |
|
|
|
|
61
|
|
|
$this->assertSame($this->realContainer, $this->realContainer->addService($definition)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @throws DuplicatedServiceException |
66
|
|
|
*/ |
67
|
|
|
public function test_AddServiceInstance() |
68
|
|
|
{ |
69
|
|
|
$definition = $this->getMockBuilder(InstanceDefinition::class) |
70
|
|
|
->setConstructorArgs([self::SERVICE_NAME, new \DateTime()]) |
71
|
|
|
->getMock(); |
72
|
|
|
|
73
|
|
|
$definition->expects($this->atLeast(2)) |
74
|
|
|
->method('getServiceName') |
75
|
|
|
->willReturn(self::SERVICE_NAME); |
76
|
|
|
|
77
|
|
|
$this->container->expects($this->once()) |
78
|
|
|
->method('addServiceInstance') |
79
|
|
|
->willReturn($this->container); |
80
|
|
|
|
81
|
|
|
$this->assertSame($this->container, $this->container->addServiceInstance($definition)); |
|
|
|
|
82
|
|
|
$this->assertSame($this->realContainer, $this->realContainer->addServiceInstance($definition)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @throws DuplicatedServiceException |
87
|
|
|
*/ |
88
|
|
|
public function test_AddServiceInstanceException() |
89
|
|
|
{ |
90
|
|
|
$this->expectException(DuplicatedServiceException::class); |
91
|
|
|
|
92
|
|
|
$definition = $this->getMockBuilder(InstanceDefinition::class) |
93
|
|
|
->setConstructorArgs([self::SERVICE_NAME, new \DateTime()]) |
94
|
|
|
->getMock(); |
95
|
|
|
|
96
|
|
|
$definition->expects($this->atLeast(2)) |
97
|
|
|
->method('getServiceName') |
98
|
|
|
->willReturn(self::SERVICE_NAME); |
99
|
|
|
|
100
|
|
|
$this->realContainer->addServiceInstance($definition); |
101
|
|
|
$this->realContainer->addServiceInstance($definition); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function test_get() |
105
|
|
|
{ |
106
|
|
|
$this->assertSame(null, $this->realContainer->get('test')); |
107
|
|
|
$this->assertSame(\DateTime::class, get_class($this->realContainer->get(self::DEFAULT_SERVICE_NANE))); |
108
|
|
|
$instance = $this->realContainer->get(self::DEFAULT_SERVICE_NANE); |
109
|
|
|
$this->assertSame(\DateTime::class, get_class($instance)); |
110
|
|
|
$this->assertSame($instance, $this->realContainer->get(self::DEFAULT_SERVICE_NANE)); |
111
|
|
|
|
112
|
|
|
$this->realContainer->addServiceInstance( |
113
|
|
|
new InstanceDefinition('call.format', "d-m-Y") |
114
|
|
|
); |
115
|
|
|
|
116
|
|
|
$this->realContainer->addService( |
117
|
|
|
new ServiceDefinition( |
118
|
|
|
'service.call', |
119
|
|
|
\DateTime::class, |
120
|
|
|
['1970-01-01'], |
121
|
|
|
['format' => ['call.format']] |
122
|
|
|
) |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
$date = new \DateTime('1970-01-01'); |
126
|
|
|
$this->assertSame($date->format('m-d-Y'), $this->realContainer->get('service.call')->format('m-d-Y')); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function test_has() |
130
|
|
|
{ |
131
|
|
|
$this->assertSame(false, $this->realContainer->has('fake')); |
132
|
|
|
$this->assertSame(true, $this->realContainer->has(self::DEFAULT_SERVICE_NANE)); |
133
|
|
|
$this->assertSame(true, $this->realContainer->has(self::DEFAULT_SERVICE_NANE)); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
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.