PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/ZendTest/ServiceManager/AbstractPluginManagerTest.php

http://github.com/zendframework/zf2
PHP | 286 lines | 199 code | 57 blank | 30 comment | 1 complexity | 40fe2ec39c62477f21a3be49615224e7 MD5 | raw file
Possible License(s): BSD-3-Clause
  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. namespace ZendTest\ServiceManager;
  10. use ReflectionClass;
  11. use ReflectionObject;
  12. use Zend\ServiceManager\Config;
  13. use Zend\ServiceManager\Exception\RuntimeException;
  14. use Zend\ServiceManager\ServiceManager;
  15. use ZendTest\ServiceManager\TestAsset\FooPluginManager;
  16. use ZendTest\ServiceManager\TestAsset\MockSelfReturningDelegatorFactory;
  17. class AbstractPluginManagerTest extends \PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * @var ServiceManager
  21. */
  22. protected $serviceManager;
  23. /**
  24. * @var FooPluginManager
  25. */
  26. protected $pluginManager;
  27. public function setup()
  28. {
  29. $this->serviceManager = new ServiceManager();
  30. $this->pluginManager = new FooPluginManager(new Config(array(
  31. 'factories' => array(
  32. 'Foo' => 'ZendTest\ServiceManager\TestAsset\FooFactory',
  33. ),
  34. 'shared' => array(
  35. 'Foo' => false,
  36. ),
  37. )));
  38. }
  39. public function testSetMultipleCreationOptions()
  40. {
  41. $pluginManager = new FooPluginManager(new Config(array(
  42. 'factories' => array(
  43. 'Foo' => 'ZendTest\ServiceManager\TestAsset\FooFactory'
  44. ),
  45. 'shared' => array(
  46. 'Foo' => false
  47. )
  48. )));
  49. $refl = new ReflectionClass($pluginManager);
  50. $reflProperty = $refl->getProperty('factories');
  51. $reflProperty->setAccessible(true);
  52. $value = $reflProperty->getValue($pluginManager);
  53. $this->assertInternalType('string', $value['foo']);
  54. $pluginManager->get('Foo', array('key1' => 'value1'));
  55. $value = $reflProperty->getValue($pluginManager);
  56. $this->assertInstanceOf('ZendTest\ServiceManager\TestAsset\FooFactory', $value['foo']);
  57. $this->assertEquals(array('key1' => 'value1'), $value['foo']->getCreationOptions());
  58. $pluginManager->get('Foo', array('key2' => 'value2'));
  59. $value = $reflProperty->getValue($pluginManager);
  60. $this->assertInstanceOf('ZendTest\ServiceManager\TestAsset\FooFactory', $value['foo']);
  61. $this->assertEquals(array('key2' => 'value2'), $value['foo']->getCreationOptions());
  62. }
  63. /**
  64. * @group issue-4208
  65. */
  66. public function testGetFaultyRegisteredInvokableThrowsException()
  67. {
  68. $this->setExpectedException('Zend\ServiceManager\Exception\ServiceNotFoundException');
  69. $pluginManager = new FooPluginManager();
  70. $pluginManager->setInvokableClass('helloWorld', 'IDoNotExist');
  71. $pluginManager->get('helloWorld');
  72. }
  73. public function testAbstractFactoryWithMutableCreationOptions()
  74. {
  75. $creationOptions = array('key1' => 'value1');
  76. $mock = 'ZendTest\ServiceManager\TestAsset\AbstractFactoryWithMutableCreationOptions';
  77. $abstractFactory = $this->getMock($mock, array('setCreationOptions'));
  78. $abstractFactory->expects($this->once())
  79. ->method('setCreationOptions')
  80. ->with($creationOptions);
  81. $this->pluginManager->addAbstractFactory($abstractFactory);
  82. $instance = $this->pluginManager->get('classnoexists', $creationOptions);
  83. $this->assertInternalType('object', $instance);
  84. }
  85. public function testMutableMethodNeverCalledWithoutCreationOptions()
  86. {
  87. $mock = 'ZendTest\ServiceManager\TestAsset\CallableWithMutableCreationOptions';
  88. $callable = $this->getMock($mock, array('setCreationOptions'));
  89. $callable->expects($this->never())
  90. ->method('setCreationOptions');
  91. $ref = new ReflectionObject($this->pluginManager);
  92. $method = $ref->getMethod('createServiceViaCallback');
  93. $method->setAccessible(true);
  94. $method->invoke($this->pluginManager, $callable, 'foo', 'bar');
  95. }
  96. public function testCallableObjectWithMutableCreationOptions()
  97. {
  98. $creationOptions = array('key1' => 'value1');
  99. $mock = 'ZendTest\ServiceManager\TestAsset\CallableWithMutableCreationOptions';
  100. $callable = $this->getMock($mock, array('setCreationOptions'));
  101. $callable->expects($this->once())
  102. ->method('setCreationOptions')
  103. ->with($creationOptions);
  104. $ref = new ReflectionObject($this->pluginManager);
  105. $property = $ref->getProperty('creationOptions');
  106. $property->setAccessible(true);
  107. $property->setValue($this->pluginManager, $creationOptions);
  108. $method = $ref->getMethod('createServiceViaCallback');
  109. $method->setAccessible(true);
  110. $method->invoke($this->pluginManager, $callable, 'foo', 'bar');
  111. }
  112. public function testValidatePluginIsCalledWithDelegatorFactoryIfItsAService()
  113. {
  114. $pluginManager = $this->getMockForAbstractClass('Zend\ServiceManager\AbstractPluginManager');
  115. $delegatorFactory = $this->getMock('Zend\\ServiceManager\\DelegatorFactoryInterface');
  116. $pluginManager->setService('delegator-factory', $delegatorFactory);
  117. $pluginManager->addDelegator('foo-service', 'delegator-factory');
  118. $pluginManager->expects($this->once())
  119. ->method('validatePlugin')
  120. ->with($delegatorFactory);
  121. $pluginManager->create('foo-service');
  122. }
  123. public function testSingleDelegatorUsage()
  124. {
  125. $delegatorFactory = $this->getMock('Zend\\ServiceManager\\DelegatorFactoryInterface');
  126. /* @var $pluginManager \Zend\ServiceManager\AbstractPluginManager|\PHPUnit_Framework_MockObject_MockObject */
  127. $pluginManager = $this->getMockForAbstractClass('Zend\ServiceManager\AbstractPluginManager');
  128. $realService = $this->getMock('stdClass', array(), array(), 'RealService');
  129. $delegator = $this->getMock('stdClass', array(), array(), 'Delegator');
  130. $delegatorFactory
  131. ->expects($this->once())
  132. ->method('createDelegatorWithName')
  133. ->with(
  134. $pluginManager,
  135. 'fooservice',
  136. 'foo-service',
  137. $this->callback(function ($callback) use ($realService) {
  138. if (!is_callable($callback)) {
  139. return false;
  140. }
  141. return call_user_func($callback) === $realService;
  142. })
  143. )
  144. ->will($this->returnValue($delegator));
  145. $pluginManager->setFactory('foo-service', function () use ($realService) {
  146. return $realService;
  147. });
  148. $pluginManager->addDelegator('foo-service', $delegatorFactory);
  149. $pluginManager->expects($this->once())
  150. ->method('validatePlugin')
  151. ->with($delegator);
  152. $this->assertSame($delegator, $pluginManager->get('foo-service'));
  153. }
  154. public function testMultipleDelegatorsUsage()
  155. {
  156. /* @var $pluginManager \Zend\ServiceManager\AbstractPluginManager|\PHPUnit_Framework_MockObject_MockObject */
  157. $pluginManager = $this->getMockForAbstractClass('Zend\ServiceManager\AbstractPluginManager');
  158. $fooDelegator = new MockSelfReturningDelegatorFactory();
  159. $barDelegator = new MockSelfReturningDelegatorFactory();
  160. $pluginManager->addDelegator('foo-service', $fooDelegator);
  161. $pluginManager->addDelegator('foo-service', $barDelegator);
  162. $pluginManager->setInvokableClass('foo-service', 'stdClass');
  163. $pluginManager->expects($this->once())
  164. ->method('validatePlugin')
  165. ->with($barDelegator);
  166. $this->assertSame($barDelegator, $pluginManager->get('foo-service'));
  167. $this->assertCount(1, $barDelegator->instances);
  168. $this->assertCount(1, $fooDelegator->instances);
  169. $this->assertInstanceOf('stdClass', array_shift($fooDelegator->instances));
  170. $this->assertSame($fooDelegator, array_shift($barDelegator->instances));
  171. }
  172. /**
  173. * @group 6833
  174. */
  175. public function testCanCheckInvalidServiceManagerIsUsed()
  176. {
  177. $sm = new ServiceManager();
  178. $sm->setService('bar', new \stdClass());
  179. /** @var \Zend\ServiceManager\AbstractPluginManager $pluginManager */
  180. $pluginManager = new FooPluginManager();
  181. $pluginManager->setServiceLocator($sm);
  182. $this->setExpectedException('Zend\ServiceManager\Exception\ServiceLocatorUsageException');
  183. $pluginManager->get('bar');
  184. $this->fail('A Zend\ServiceManager\Exception\ServiceNotCreatedException is expected');
  185. }
  186. /**
  187. * @group 6833
  188. */
  189. public function testWillRethrowOnNonValidatedPlugin()
  190. {
  191. $sm = new ServiceManager();
  192. $sm->setInvokableClass('stdClass', 'stdClass');
  193. /** @var \Zend\ServiceManager\AbstractPluginManager|\PHPUnit_Framework_MockObject_MockObject $pluginManager */
  194. $pluginManager = $this->getMockForAbstractClass('Zend\ServiceManager\AbstractPluginManager');
  195. $pluginManager
  196. ->expects($this->once())
  197. ->method('validatePlugin')
  198. ->with($this->isInstanceOf('stdClass'))
  199. ->will($this->throwException(new RuntimeException()));
  200. $pluginManager->setServiceLocator($sm);
  201. $this->setExpectedException('Zend\ServiceManager\Exception\ServiceLocatorUsageException');
  202. $pluginManager->get('stdClass');
  203. }
  204. /**
  205. * @group 6833
  206. */
  207. public function testWillResetAutoInvokableServiceIfNotValid()
  208. {
  209. /** @var \Zend\ServiceManager\AbstractPluginManager|\PHPUnit_Framework_MockObject_MockObject $pluginManager */
  210. $pluginManager = $this->getMockForAbstractClass('Zend\ServiceManager\AbstractPluginManager');
  211. $pluginManager
  212. ->expects($this->any())
  213. ->method('validatePlugin')
  214. ->will($this->throwException(new RuntimeException()));
  215. $pluginManager->setInvokableClass(__CLASS__, __CLASS__);
  216. try {
  217. $pluginManager->get('stdClass');
  218. $this->fail('Expected the plugin manager to throw a RuntimeException, none thrown');
  219. } catch (RuntimeException $exception) {
  220. $this->assertFalse($pluginManager->has('stdClass'));
  221. }
  222. try {
  223. $pluginManager->get(__CLASS__);
  224. $this->fail('Expected the plugin manager to throw a RuntimeException, none thrown');
  225. } catch (RuntimeException $exception) {
  226. $this->assertTrue($pluginManager->has(__CLASS__));
  227. }
  228. }
  229. }