This repository has been archived by the owner on Feb 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 89
/
LazyServiceIntegrationTest.php
297 lines (261 loc) · 9.91 KB
/
LazyServiceIntegrationTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
/**
* @link http://github.com/zendframework/zend-servicemanager for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace ZendTest\ServiceManager;
use PHPUnit\Framework\TestCase;
use ProxyManager\Autoloader\AutoloaderInterface;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RecursiveRegexIterator;
use RegexIterator;
use stdClass;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\Factory\InvokableFactory;
use Zend\ServiceManager\Proxy\LazyServiceFactory;
use Zend\ServiceManager\ServiceManager;
use ZendTest\ServiceManager\TestAsset\InvokableObject;
/**
* @covers \Zend\ServiceManager\ServiceManager
*/
class LazyServiceIntegrationTest extends TestCase
{
public $proxyDir;
public function setUp()
{
$this->proxyDir = sys_get_temp_dir() . '/zend-servicemanager-proxy';
if (! is_dir($this->proxyDir)) {
mkdir($this->proxyDir);
}
}
public function tearDown()
{
if (! is_dir($this->proxyDir)) {
return;
}
$this->removeDir($this->proxyDir);
foreach ($this->getRegisteredProxyAutoloadFunctions() as $autoloader) {
spl_autoload_unregister($autoloader);
}
}
public function removeDir($directory)
{
$handle = opendir($directory);
while (false !== ($item = readdir($handle))) {
if ('.' === $item || '..' === $item) {
continue;
}
$path = $directory . '/' . $item;
if (is_dir($path)) {
$this->removeDir($path);
continue;
}
if (is_file($path)) {
unlink($path);
continue;
}
}
closedir($handle);
rmdir($directory);
}
public function listProxyFiles()
{
$rdi = new RecursiveDirectoryIterator($this->proxyDir);
$rii = new RecursiveIteratorIterator($rdi);
return new RegexIterator($rii, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);
}
public function assertProxyDirEmpty($message = '')
{
$message = $message ?: 'Expected empty proxy directory; found files';
// AssertEquals instead AssertEmpty because the first one prints the list of files.
$this->assertEquals([], iterator_to_array($this->listProxyFiles()), $message);
}
public function assertProxyFileWritten($message = '')
{
$message = $message ?: 'Expected ProxyManager to write at least one class file; none found';
// AssertNotEquals instead AssertNotEmpty because the first one prints the list of files.
$this->assertNotEquals([], iterator_to_array($this->listProxyFiles()), $message);
}
/**
* @covers \Zend\ServiceManager\ServiceManager::createLazyServiceDelegatorFactory
*/
public function testCanUseLazyServiceFactoryFactoryToCreateLazyServiceFactoryToActAsDelegatorToCreateLazyService()
{
$config = [
'lazy_services' => [
'class_map' => [
InvokableObject::class => InvokableObject::class,
],
'proxies_namespace' => 'TestAssetProxy',
'proxies_target_dir' => $this->proxyDir,
'write_proxy_files' => true,
],
'factories' => [
InvokableObject::class => InvokableFactory::class,
],
'delegators' => [
InvokableObject::class => [LazyServiceFactory::class],
],
];
$this->assertProxyDirEmpty();
$container = new ServiceManager($config);
$instance = $container->build(InvokableObject::class, ['foo' => 'bar']);
$this->assertProxyFileWritten();
// Test we got a usable proxy
$this->assertInstanceOf(
InvokableObject::class,
$instance,
'Service returned does not extend ' . InvokableObject::class
);
$this->assertContains(
'TestAssetProxy',
get_class($instance),
'Service returned does not contain expected namespace'
);
// Test proxying works as expected
$options = $instance->getOptions();
$this->assertInternalType(
'array',
$options,
'Expected an array of options'
);
$this->assertEquals(['foo' => 'bar'], $options, 'Options returned do not match configuration');
$proxyAutoloadFunctions = $this->getRegisteredProxyAutoloadFunctions();
$this->assertCount(1, $proxyAutoloadFunctions, 'Only 1 proxy autoloader should be registered');
}
/**
* @covers \Zend\ServiceManager\ServiceManager::createLazyServiceDelegatorFactory
*/
public function testMissingClassMapRaisesExceptionOnAttemptToRetrieveLazyService()
{
$config = [
'lazy_services' => [
],
'factories' => [
InvokableObject::class => InvokableFactory::class,
],
'delegators' => [
InvokableObject::class => [LazyServiceFactory::class],
],
];
$container = new ServiceManager($config);
$this->expectException(ServiceNotCreatedException::class);
$this->expectExceptionMessage('class_map');
$container->get(InvokableObject::class);
}
/**
* @covers \Zend\ServiceManager\ServiceManager::createLazyServiceDelegatorFactory
*/
public function testWillNotGenerateProxyClassFilesByDefault()
{
$config = [
'lazy_services' => [
'class_map' => [
InvokableObject::class => InvokableObject::class,
],
'proxies_namespace' => 'TestAssetProxy',
],
'factories' => [
InvokableObject::class => InvokableFactory::class,
],
'delegators' => [
InvokableObject::class => [LazyServiceFactory::class],
],
];
$this->assertProxyDirEmpty();
$container = new ServiceManager($config);
$instance = $container->build(InvokableObject::class, ['foo' => 'bar']);
// This is the important test
$this->assertProxyDirEmpty('Expected proxy directory to remain empty when write_proxy_files disabled');
// Test we got a usable proxy
$this->assertInstanceOf(
InvokableObject::class,
$instance,
'Service returned does not extend ' . InvokableObject::class
);
$this->assertContains(
'TestAssetProxy',
get_class($instance),
'Service returned does not contain expected namespace'
);
// Test proxying works as expected
$options = $instance->getOptions();
$this->assertInternalType(
'array',
$options,
'Expected an array of options'
);
$this->assertEquals(['foo' => 'bar'], $options, 'Options returned do not match configuration');
$proxyAutoloadFunctions = $this->getRegisteredProxyAutoloadFunctions();
$this->assertCount(1, $proxyAutoloadFunctions, 'Only 1 proxy autoloader should be registered');
}
public function testOnlyOneProxyAutoloaderItsRegisteredOnSubsequentCalls()
{
$config = [
'lazy_services' => [
'class_map' => [
InvokableObject::class => InvokableObject::class,
stdClass::class => stdClass::class,
],
'proxies_namespace' => 'TestAssetProxy',
],
'factories' => [
InvokableObject::class => InvokableFactory::class,
],
'delegators' => [
InvokableObject::class => [LazyServiceFactory::class],
stdClass::class => [LazyServiceFactory::class],
],
];
$container = new ServiceManager($config);
$instance = $container->build(InvokableObject::class, ['foo' => 'bar']);
$this->assertInstanceOf(
InvokableObject::class,
$instance,
'Service returned does not extend ' . InvokableObject::class
);
$instance = $container->build(stdClass::class, ['foo' => 'bar']);
$this->assertInstanceOf(
stdClass::class,
$instance,
'Service returned does not extend ' . stdClass::class
);
$proxyAutoloadFunctions = $this->getRegisteredProxyAutoloadFunctions();
$this->assertCount(1, $proxyAutoloadFunctions, 'Only 1 proxy autoloader should be registered');
}
public function testRaisesServiceNotFoundExceptionIfRequestedLazyServiceIsNotInClassMap()
{
$config = [
'lazy_services' => [
'class_map' => [
stdClass::class => stdClass::class,
],
'proxies_namespace' => 'TestAssetProxy',
],
'factories' => [
InvokableObject::class => InvokableFactory::class,
],
'delegators' => [
InvokableObject::class => [LazyServiceFactory::class],
],
];
$this->assertProxyDirEmpty();
$container = new ServiceManager($config);
$this->expectException(ServiceNotFoundException::class);
$this->expectExceptionMessage('not found in the provided services map');
$container->build(InvokableObject::class, ['foo' => 'bar']);
}
/**
* @return AutoloaderInterface[]
*/
protected function getRegisteredProxyAutoloadFunctions()
{
$filter = function ($autoload) {
return ($autoload instanceof AutoloaderInterface);
};
return array_filter(spl_autoload_functions(), $filter);
}
}