10
10
namespace ZendTest \ServiceManager ;
11
11
12
12
use PHPUnit_Framework_TestCase as TestCase ;
13
+ use ProxyManager \Autoloader \AutoloaderInterface ;
13
14
use RecursiveDirectoryIterator ;
14
15
use RecursiveIteratorIterator ;
15
16
use RecursiveRegexIterator ;
22
23
use ZendTest \ServiceManager \TestAsset \InvokableObject ;
23
24
24
25
/**
25
- * @covers \Zend\ServiceManager\Proxy\LazyServiceFactory
26
+ * @covers \Zend\ServiceManager\ServiceManager
26
27
*/
27
28
class LazyServiceIntegrationTest extends TestCase
28
29
{
@@ -43,6 +44,9 @@ public function tearDown()
43
44
}
44
45
45
46
$ this ->removeDir ($ this ->proxyDir );
47
+ foreach ($ this ->getRegisteredProxyAutoloadFunctions () as $ autoloader ) {
48
+ spl_autoload_unregister ($ autoloader );
49
+ }
46
50
}
47
51
48
52
public function removeDir ($ directory )
@@ -77,22 +81,15 @@ public function listProxyFiles()
77
81
public function assertProxyDirEmpty ($ message = '' )
78
82
{
79
83
$ message = $ message ?: 'Expected empty proxy directory; found files ' ;
80
- $ count = 0 ;
81
- foreach ($ this ->listProxyFiles () as $ file ) {
82
- $ this ->fail ($ message );
83
- }
84
- $ this ->assertEquals (0 , $ count );
84
+ // AssertEquals instead AssertEmpty because the first one prints the list of files.
85
+ $ this ->assertEquals ([], iterator_to_array ($ this ->listProxyFiles ()), $ message );
85
86
}
86
87
87
88
public function assertProxyFileWritten ($ message = '' )
88
89
{
89
90
$ message = $ message ?: 'Expected ProxyManager to write at least one class file; none found ' ;
90
- $ count = 0 ;
91
- foreach ($ this ->listProxyFiles () as $ file ) {
92
- $ count += 1 ;
93
- break ;
94
- }
95
- $ this ->assertNotEquals (0 , $ count , $ message );
91
+ // AssertNotEquals instead AssertNotEmpty because the first one prints the list of files.
92
+ $ this ->assertNotEquals ([], iterator_to_array ($ this ->listProxyFiles ()), $ message );
96
93
}
97
94
98
95
/**
@@ -141,12 +138,12 @@ public function testCanUseLazyServiceFactoryFactoryToCreateLazyServiceFactoryToA
141
138
$ this ->assertInternalType (
142
139
'array ' ,
143
140
$ options ,
144
- sprintf (
145
- 'Expected an array of options; %s received ' ,
146
- (is_object ($ options ) ? get_class ($ options ) : gettype ($ options ))
147
- )
141
+ 'Expected an array of options '
148
142
);
149
143
$ this ->assertEquals (['foo ' => 'bar ' ], $ options , 'Options returned do not match configuration ' );
144
+
145
+ $ proxyAutoloadFunctions = $ this ->getRegisteredProxyAutoloadFunctions ();
146
+ $ this ->assertCount (1 , $ proxyAutoloadFunctions , 'Only 1 proxy autoloader must be registered ' );
150
147
}
151
148
152
149
/**
@@ -215,12 +212,49 @@ public function testWillNotGenerateProxyClassFilesByDefault()
215
212
$ this ->assertInternalType (
216
213
'array ' ,
217
214
$ options ,
218
- sprintf (
219
- 'Expected an array of options; %s received ' ,
220
- (is_object ($ options ) ? get_class ($ options ) : gettype ($ options ))
221
- )
215
+ 'Expected an array of options '
222
216
);
223
217
$ this ->assertEquals (['foo ' => 'bar ' ], $ options , 'Options returned do not match configuration ' );
218
+
219
+ $ proxyAutoloadFunctions = $ this ->getRegisteredProxyAutoloadFunctions ();
220
+ $ this ->assertCount (1 , $ proxyAutoloadFunctions , 'Only 1 proxy autoloader must be registered ' );
221
+ }
222
+
223
+ public function testOnlyOneProxyAutoloaderItsRegisteredOnSubsequentCalls ()
224
+ {
225
+ $ config = [
226
+ 'lazy_services ' => [
227
+ 'class_map ' => [
228
+ InvokableObject::class => InvokableObject::class,
229
+ \stdClass::class => \stdClass::class,
230
+ ],
231
+ 'proxies_namespace ' => 'TestAssetProxy ' ,
232
+ ],
233
+ 'factories ' => [
234
+ InvokableObject::class => InvokableFactory::class,
235
+ ],
236
+ 'delegators ' => [
237
+ InvokableObject::class => [LazyServiceFactory::class],
238
+ \stdClass::class => [LazyServiceFactory::class],
239
+ ],
240
+ ];
241
+
242
+ $ container = new ServiceManager ($ config );
243
+ $ instance = $ container ->build (InvokableObject::class, ['foo ' => 'bar ' ]);
244
+ $ this ->assertInstanceOf (
245
+ InvokableObject::class,
246
+ $ instance ,
247
+ 'Service returned does not extend ' . InvokableObject::class
248
+ );
249
+ $ instance = $ container ->build (\stdClass::class, ['foo ' => 'bar ' ]);
250
+ $ this ->assertInstanceOf (
251
+ \stdClass::class,
252
+ $ instance ,
253
+ 'Service returned does not extend ' . \stdClass::class
254
+ );
255
+
256
+ $ proxyAutoloadFunctions = $ this ->getRegisteredProxyAutoloadFunctions ();
257
+ $ this ->assertCount (1 , $ proxyAutoloadFunctions , 'Only 1 proxy autoloader must be registered ' );
224
258
}
225
259
226
260
public function testRaisesServiceNotFoundExceptionIfRequestedLazyServiceIsNotInClassMap ()
@@ -243,7 +277,20 @@ public function testRaisesServiceNotFoundExceptionIfRequestedLazyServiceIsNotInC
243
277
$ this ->assertProxyDirEmpty ();
244
278
245
279
$ container = new ServiceManager ($ config );
280
+
246
281
$ this ->setExpectedException (ServiceNotFoundException::class, 'not found in the provided services map ' );
247
- $ instance = $ container ->build (InvokableObject::class, ['foo ' => 'bar ' ]);
282
+ $ container ->build (InvokableObject::class, ['foo ' => 'bar ' ]);
283
+ }
284
+
285
+ /**
286
+ * @return AutoloaderInterface[]
287
+ */
288
+ protected function getRegisteredProxyAutoloadFunctions ()
289
+ {
290
+ $ filter = function ($ autoload ) {
291
+ return ($ autoload instanceof AutoloaderInterface);
292
+ };
293
+
294
+ return array_filter (spl_autoload_functions (), $ filter );
248
295
}
249
296
}
0 commit comments