Skip to content
This repository was archived by the owner on Feb 6, 2020. It is now read-only.

Commit e932c8a

Browse files
committed
LazyService test improvements
1 parent a2a9413 commit e932c8a

File tree

3 files changed

+185
-22
lines changed

3 files changed

+185
-22
lines changed

src/Proxy/LazyServiceFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function __construct(LazyLoadingValueHolderFactory $proxyFactory, array $
4747
/**
4848
* {@inheritDoc}
4949
*
50-
* @return object|\ProxyManager\Proxy\LazyLoadingInterface|\ProxyManager\Proxy\ValueHolderInterface
50+
* @return object|\ProxyManager\Proxy\VirtualProxyInterface
5151
*/
5252
public function __invoke(ContainerInterface $container, $name, callable $callback, array $options = null)
5353
{

test/LazyServiceIntegrationTest.php

Lines changed: 68 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace ZendTest\ServiceManager;
1111

1212
use PHPUnit_Framework_TestCase as TestCase;
13+
use ProxyManager\Autoloader\AutoloaderInterface;
1314
use RecursiveDirectoryIterator;
1415
use RecursiveIteratorIterator;
1516
use RecursiveRegexIterator;
@@ -22,7 +23,7 @@
2223
use ZendTest\ServiceManager\TestAsset\InvokableObject;
2324

2425
/**
25-
* @covers \Zend\ServiceManager\Proxy\LazyServiceFactory
26+
* @covers \Zend\ServiceManager\ServiceManager
2627
*/
2728
class LazyServiceIntegrationTest extends TestCase
2829
{
@@ -43,6 +44,9 @@ public function tearDown()
4344
}
4445

4546
$this->removeDir($this->proxyDir);
47+
foreach ($this->getRegisteredProxyAutoloadFunctions() as $autoloader) {
48+
spl_autoload_unregister($autoloader);
49+
}
4650
}
4751

4852
public function removeDir($directory)
@@ -77,22 +81,15 @@ public function listProxyFiles()
7781
public function assertProxyDirEmpty($message = '')
7882
{
7983
$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);
8586
}
8687

8788
public function assertProxyFileWritten($message = '')
8889
{
8990
$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);
9693
}
9794

9895
/**
@@ -141,12 +138,12 @@ public function testCanUseLazyServiceFactoryFactoryToCreateLazyServiceFactoryToA
141138
$this->assertInternalType(
142139
'array',
143140
$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'
148142
);
149143
$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');
150147
}
151148

152149
/**
@@ -215,12 +212,49 @@ public function testWillNotGenerateProxyClassFilesByDefault()
215212
$this->assertInternalType(
216213
'array',
217214
$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'
222216
);
223217
$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');
224258
}
225259

226260
public function testRaisesServiceNotFoundExceptionIfRequestedLazyServiceIsNotInClassMap()
@@ -243,7 +277,20 @@ public function testRaisesServiceNotFoundExceptionIfRequestedLazyServiceIsNotInC
243277
$this->assertProxyDirEmpty();
244278

245279
$container = new ServiceManager($config);
280+
246281
$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);
248295
}
249296
}

test/Proxy/LazyServiceFactoryTest.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace ZendTest\ServiceManager\Proxy;
11+
12+
use Interop\Container\ContainerInterface;
13+
use PHPUnit_Framework_MockObject_MockObject as MockObject;
14+
use PHPUnit_Framework_TestCase as TestCase;
15+
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
16+
use ProxyManager\Proxy\LazyLoadingInterface;
17+
use ProxyManager\Proxy\VirtualProxyInterface;
18+
use Zend\ServiceManager\Exception\ServiceNotFoundException;
19+
use Zend\ServiceManager\Factory\DelegatorFactoryInterface;
20+
use Zend\ServiceManager\Proxy\LazyServiceFactory;
21+
22+
/**
23+
* @covers \Zend\ServiceManager\Proxy\LazyServiceFactory
24+
*/
25+
final class LazyServiceFactoryTest extends TestCase
26+
{
27+
/**
28+
* @var LazyServiceFactory
29+
*/
30+
private $factory;
31+
32+
/**
33+
* @var LazyLoadingValueHolderFactory|MockObject
34+
*/
35+
private $proxyFactory;
36+
37+
/**
38+
* {@inheritDoc}
39+
*/
40+
protected function setUp()
41+
{
42+
$this->proxyFactory = $this->getMock(LazyLoadingValueHolderFactory::class);
43+
$servicesMap = [
44+
'fooService' => 'FooClass',
45+
];
46+
47+
$this->factory = new LazyServiceFactory($this->proxyFactory, $servicesMap);
48+
}
49+
50+
public function testImplementsDelegatorFactoryInterface()
51+
{
52+
$this->assertInstanceOf(DelegatorFactoryInterface::class, $this->factory);
53+
}
54+
55+
public function testThrowExceptionWhenServiceNotExists()
56+
{
57+
$callback = $this->getMock('stdClass', ['callback']);
58+
$callback->expects($this->never())
59+
->method('callback')
60+
;
61+
$container = $this->createContainerMock();
62+
63+
$this->proxyFactory->expects($this->never())
64+
->method('createProxy')
65+
;
66+
$this->setExpectedException(
67+
ServiceNotFoundException::class,
68+
'The requested service "not_exists" was not found in the provided services map'
69+
);
70+
71+
$this->factory->__invoke($container, 'not_exists', [$callback, 'callback']);
72+
}
73+
74+
public function testCreates()
75+
{
76+
$callback = $this->getMock('stdClass', ['callback']);
77+
$callback->expects($this->once())
78+
->method('callback')
79+
->willReturn('fooValue')
80+
;
81+
$container = $this->createContainerMock();
82+
$expectedService = $this->getMock(VirtualProxyInterface::class);
83+
84+
$this->proxyFactory->expects($this->once())
85+
->method('createProxy')
86+
->willReturnCallback(
87+
function ($className, $initializer) use ($expectedService) {
88+
$this->assertEquals('FooClass', $className, 'class name not match');
89+
90+
$wrappedInstance = null;
91+
$result = $initializer($wrappedInstance, $this->getMock(LazyLoadingInterface::class));
92+
93+
$this->assertEquals('fooValue', $wrappedInstance, 'expected callback return value');
94+
$this->assertTrue($result, 'initializer must return true');
95+
96+
return $expectedService;
97+
}
98+
)
99+
;
100+
101+
$result = $this->factory->__invoke($container, 'fooService', [$callback, 'callback']);
102+
103+
$this->assertSame($expectedService, $result, 'service created not match the expected');
104+
}
105+
106+
/**
107+
* @return ContainerInterface|MockObject
108+
*/
109+
private function createContainerMock()
110+
{
111+
/** @var ContainerInterface|MockObject $container */
112+
$container = $this->getMock(ContainerInterface::class);
113+
114+
return $container;
115+
}
116+
}

0 commit comments

Comments
 (0)