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