1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ArpTest\LaminasFactory; |
6
|
|
|
|
7
|
|
|
use Arp\LaminasFactory\ApplicationOptionsProviderTrait; |
8
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotCreatedException; |
9
|
|
|
use Laminas\ServiceManager\Exception\ServiceNotFoundException; |
10
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
11
|
|
|
use PHPUnit\Framework\TestCase; |
12
|
|
|
use Psr\Container\ContainerInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @covers \Arp\LaminasFactory\ApplicationOptionsProviderTrait |
16
|
|
|
* |
17
|
|
|
* @author Alex Patterson <[email protected]> |
18
|
|
|
* @package ArpTest\LaminasFactory |
19
|
|
|
*/ |
20
|
|
|
final class ApplicationOptionsProviderTraitTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var ContainerInterface&MockObject |
24
|
|
|
*/ |
25
|
|
|
private $container; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private string $optionsKey = 'arp'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private string $optionsService = 'config'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Setup the test dependencies. |
39
|
|
|
*/ |
40
|
|
|
public function setUp(): void |
41
|
|
|
{ |
42
|
|
|
$this->container = $this->createMock(ContainerInterface::class); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Assert that the getApplicationOptions() method will throw a ServiceNotFoundException if the configured |
47
|
|
|
* application service cannot be found within the container. |
48
|
|
|
* |
49
|
|
|
* @throws ServiceNotCreatedException |
50
|
|
|
* @throws ServiceNotFoundException |
51
|
|
|
*/ |
52
|
|
|
public function testWillThrowServiceNotFoundExceptionIfTheOptionsServiceCannotBeFound(): void |
53
|
|
|
{ |
54
|
|
|
$subject = new class () { |
55
|
|
|
use ApplicationOptionsProviderTrait; |
56
|
|
|
}; |
57
|
|
|
|
58
|
|
|
$this->container->expects($this->once()) |
59
|
|
|
->method('has') |
60
|
|
|
->with($this->optionsService) |
61
|
|
|
->willReturn(false); |
62
|
|
|
|
63
|
|
|
$this->expectException(ServiceNotFoundException::class); |
64
|
|
|
$this->expectExceptionMessage( |
65
|
|
|
sprintf( |
66
|
|
|
'The required application options service \'%s\' could not be found in \'%s::%s\'.', |
67
|
|
|
$this->optionsService, |
68
|
|
|
ApplicationOptionsProviderTrait::class, |
69
|
|
|
'getApplicationOptions' |
70
|
|
|
) |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
$subject->getApplicationOptions($this->container); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Assert that a ServiceNotCreatedException is thrown when the returned ApplicationOptionsService is not of |
78
|
|
|
* type array. |
79
|
|
|
* |
80
|
|
|
* @throws ServiceNotCreatedException |
81
|
|
|
* @throws ServiceNotFoundException |
82
|
|
|
*/ |
83
|
|
|
public function testWillThrowServiceNotCreatedExceptionIfTheReturnedOptionsServiceIsNotAnArray(): void |
84
|
|
|
{ |
85
|
|
|
$subject = new class () { |
86
|
|
|
use ApplicationOptionsProviderTrait; |
87
|
|
|
}; |
88
|
|
|
|
89
|
|
|
$this->container->expects($this->once()) |
90
|
|
|
->method('has') |
91
|
|
|
->with($this->optionsService) |
92
|
|
|
->willReturn(true); |
93
|
|
|
|
94
|
|
|
$this->container->expects($this->once()) |
95
|
|
|
->method('get') |
96
|
|
|
->with($this->optionsService) |
97
|
|
|
->willReturn(false); |
98
|
|
|
|
99
|
|
|
$this->expectException(ServiceNotCreatedException::class); |
100
|
|
|
$this->expectExceptionMessage( |
101
|
|
|
sprintf( |
102
|
|
|
'The application key \'%s\' could not be found within the application options service \'%s\'.', |
103
|
|
|
$this->optionsKey, |
104
|
|
|
$this->optionsService |
105
|
|
|
) |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
$subject->getApplicationOptions($this->container); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Assert that a ServiceNotCreatedException is thrown when the returned ApplicationOptionsService does not |
113
|
|
|
* container a array key matching the options key. |
114
|
|
|
* |
115
|
|
|
* @throws ServiceNotCreatedException |
116
|
|
|
* @throws ServiceNotFoundException |
117
|
|
|
*/ |
118
|
|
|
public function testWillThrowServiceNotCreatedExceptionIfTheReturnedOptionsServiceIsMissingOptionsKey(): void |
119
|
|
|
{ |
120
|
|
|
$subject = new class () { |
121
|
|
|
use ApplicationOptionsProviderTrait; |
122
|
|
|
}; |
123
|
|
|
|
124
|
|
|
$this->container->expects($this->once()) |
125
|
|
|
->method('has') |
126
|
|
|
->with($this->optionsService) |
127
|
|
|
->willReturn(true); |
128
|
|
|
|
129
|
|
|
$invalid = [ |
130
|
|
|
'foo' => 123, |
131
|
|
|
]; |
132
|
|
|
|
133
|
|
|
$this->container->expects($this->once()) |
134
|
|
|
->method('get') |
135
|
|
|
->with($this->optionsService) |
136
|
|
|
->willReturn($invalid); |
137
|
|
|
|
138
|
|
|
$this->expectException(ServiceNotCreatedException::class); |
139
|
|
|
$this->expectExceptionMessage( |
140
|
|
|
sprintf( |
141
|
|
|
'The application key \'%s\' could not be found within the application options service \'%s\'.', |
142
|
|
|
$this->optionsKey, |
143
|
|
|
$this->optionsService |
144
|
|
|
) |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
$subject->getApplicationOptions($this->container); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Assert that a ServiceNotCreatedException is thrown when the resolved service options are not of type array. |
152
|
|
|
* |
153
|
|
|
* @throws ServiceNotCreatedException |
154
|
|
|
* @throws ServiceNotFoundException |
155
|
|
|
*/ |
156
|
|
|
public function testWillThrowServiceNotCreatedExceptionIfTheServiceOptionsAreNotOfTypeArray(): void |
157
|
|
|
{ |
158
|
|
|
$subject = new class () { |
159
|
|
|
use ApplicationOptionsProviderTrait; |
160
|
|
|
}; |
161
|
|
|
|
162
|
|
|
$this->container->expects($this->once()) |
163
|
|
|
->method('has') |
164
|
|
|
->with($this->optionsService) |
165
|
|
|
->willReturn(true); |
166
|
|
|
|
167
|
|
|
$invalid = [ |
168
|
|
|
$this->optionsKey => false, // invalid options type! |
169
|
|
|
]; |
170
|
|
|
|
171
|
|
|
$this->container->expects($this->once()) |
172
|
|
|
->method('get') |
173
|
|
|
->with($this->optionsService) |
174
|
|
|
->willReturn($invalid); |
175
|
|
|
|
176
|
|
|
$this->expectException(ServiceNotCreatedException::class); |
177
|
|
|
$this->expectExceptionMessage( |
178
|
|
|
sprintf( |
179
|
|
|
'The application options must be an \'array\' or object of type \'%s\'; \'%s\' provided in \'%s::%s\'.', |
180
|
|
|
\Traversable::class, |
181
|
|
|
gettype($invalid[$this->optionsKey]), |
182
|
|
|
ApplicationOptionsProviderTrait::class, |
183
|
|
|
'getApplicationOptions' |
184
|
|
|
) |
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
$subject->getApplicationOptions($this->container); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|