From 54aef0d2a35d0e2b735192b6eb1e44823771ab95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Gallego?= Date: Tue, 30 Oct 2012 23:14:54 +0100 Subject: [PATCH 1/9] Clean history --- src/AbstractPluginManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AbstractPluginManager.php b/src/AbstractPluginManager.php index 72e0fe0c..7337d593 100644 --- a/src/AbstractPluginManager.php +++ b/src/AbstractPluginManager.php @@ -30,7 +30,7 @@ abstract class AbstractPluginManager extends ServiceManager implements ServiceLo * * @var bool */ - protected $allowOverride = true; + protected $allowOverride = true; /** * Whether or not to auto-add a class as an invokable class if it exists From b9344408a4d5300dad70ad3133114b321b90eedc Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sat, 17 Nov 2012 13:20:03 +0100 Subject: [PATCH 2/9] Applying fix for the failing tests --- src/ServiceManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ServiceManager.php b/src/ServiceManager.php index 6dfe797b..8d604fec 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -591,7 +591,7 @@ public function canCreateFromAbstractFactory($cName, $rName) foreach ($this->abstractFactories as $index => $abstractFactory) { // Support string abstract factory class names if (is_string($abstractFactory) && class_exists($abstractFactory, true)) { - $this->abstractFactory[$index] = $abstractFactory = new $abstractFactory(); + $this->abstractFactories[$index] = $abstractFactory = new $abstractFactory(); } if ( From 285d3a7c5468bb16fa948d17103a958df586ae94 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 18 Nov 2012 16:37:31 +0100 Subject: [PATCH 3/9] Adding failing test for zendframework/zf2zendframework/zf2#2497 --- test/ServiceManagerTest.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/test/ServiceManagerTest.php b/test/ServiceManagerTest.php index 92f7ff1d..cb04dbf0 100644 --- a/test/ServiceManagerTest.php +++ b/test/ServiceManagerTest.php @@ -578,13 +578,12 @@ public function testWithAllowOverrideOnRegisteringAServiceDuplicatingAnExistingA { $this->serviceManager->setAllowOverride(true); $sm = $this->serviceManager; - $this->serviceManager->setFactory('http.response', function ($services) use ($sm) { + $this->serviceManager->setFactory('http.response', function () use ($sm) { return $sm; }); $this->serviceManager->setAlias('response', 'http.response'); $this->assertSame($sm, $this->serviceManager->get('response')); - $self = $this; $this->serviceManager->{$method}('response', $service); $this->{$assertion}($expected, $this->serviceManager->get('response')); } @@ -615,4 +614,22 @@ public function testWanCreateFromAbstractFactoryWillNotInstantiateAbstractFactor $this->assertSame($count + 1, FooCounterAbstractFactory::$instantiationCount); } + + /** + * @covers Zend\ServiceManager\ServiceManager::setAlias + * @covers Zend\ServiceManager\ServiceManager::get + * @covers Zend\ServiceManager\ServiceManager::retrieveFromPeeringManager + */ + public function testCanGetAliasedServicesFromPeeringServiceManagers() + { + $service = new \stdClass(); + $peeringSm = new ServiceManager(); + + $peeringSm->setService('actual-service-name', $service); + $this->serviceManager->addPeeringServiceManager($peeringSm); + + $this->serviceManager->setAlias('alias-name', 'actual-service-name'); + + $this->assertSame($service, $this->serviceManager->get('alias-name')); + } } From be36b1f936cb204c9ccea97646ba02b085d89fb2 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 18 Nov 2012 16:38:51 +0100 Subject: [PATCH 4/9] Applying fix for failing test of zendframework/zf2zendframework/zf2#2497 --- src/ServiceManager.php | 48 ++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/src/ServiceManager.php b/src/ServiceManager.php index 8d604fec..7ce23028 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -411,20 +411,16 @@ public function setShared($name, $isShared) */ public function get($name, $usePeeringServiceManagers = true) { - $cName = $this->canonicalizeName($name); - $rName = $name; + $cName = $this->canonicalizeName($name); + $rName = $name; + $isAlias = false; if ($this->hasAlias($cName)) { + $isAlias = true; + do { $cName = $this->aliases[$cName]; } while ($this->hasAlias($cName)); - - if (!$this->has(array($cName, $rName))) { - throw new Exception\ServiceNotFoundException(sprintf( - 'An alias "%s" was requested but no service could be found.', - $name - )); - } } if (isset($this->instances[$cName])) { @@ -447,16 +443,21 @@ public function get($name, $usePeeringServiceManagers = true) // Still no instance? raise an exception if (!$instance && !is_array($instance)) { + if ($isAlias) { + throw new Exception\ServiceNotFoundException(sprintf( + 'An alias "%s" was requested but no service could be found.', + $name + )); + } + throw new Exception\ServiceNotFoundException(sprintf( '%s was unable to fetch or create an instance for %s', - __METHOD__, - $name - ) - ); + __METHOD__, + $name + )); } - if ($this->shareByDefault() && (!isset($this->shared[$cName]) || $this->shared[$cName] === true) - ) { + if ($this->shareByDefault() && (!isset($this->shared[$cName]) || $this->shared[$cName] === true)) { $this->instances[$cName] = $instance; } @@ -467,7 +468,7 @@ public function get($name, $usePeeringServiceManagers = true) * Create an instance * * @param string|array $name - * @return false|object + * @return bool|object * @throws Exception\ServiceNotFoundException * @throws Exception\ServiceNotCreatedException */ @@ -795,6 +796,21 @@ protected function retrieveFromPeeringManager($name) return $peeringServiceManager->get($name); } } + + $name = $this->canonicalizeName($name); + + if ($this->hasAlias($name)) { + do { + $name = $this->aliases[$name]; + } while ($this->hasAlias($name)); + } + + foreach ($this->peeringServiceManagers as $peeringServiceManager) { + if ($peeringServiceManager->has($name)) { + return $peeringServiceManager->get($name); + } + } + return null; } From 70012980451b73ef56b9a4234692f0f153ffc9d0 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 18 Nov 2012 14:30:41 +0100 Subject: [PATCH 5/9] Adding tests for zendframework/zf2zendframework/zf2#2593 --- test/ServiceManagerTest.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/ServiceManagerTest.php b/test/ServiceManagerTest.php index 92f7ff1d..682f5a82 100644 --- a/test/ServiceManagerTest.php +++ b/test/ServiceManagerTest.php @@ -615,4 +615,26 @@ public function testWanCreateFromAbstractFactoryWillNotInstantiateAbstractFactor $this->assertSame($count + 1, FooCounterAbstractFactory::$instantiationCount); } + + /** + * @covers Zend\ServiceManager\ServiceManager::canCreateFromAbstractFactory + * @covers Zend\ServiceManager\ServiceManager::create + */ + public function testAbstractFactoryNotUsedIfNotAbleToCreate() + { + $service = new \stdClass; + + $af1 = $this->getMock('Zend\ServiceManager\AbstractFactoryInterface'); + $af1->expects($this->any())->method('canCreateServiceWithName')->will($this->returnValue(true)); + $af1->expects($this->any())->method('createServiceWithName')->will($this->returnValue($service)); + + $af2 = $this->getMock('Zend\ServiceManager\AbstractFactoryInterface'); + $af2->expects($this->any())->method('canCreateServiceWithName')->will($this->returnValue(false)); + $af2->expects($this->never())->method('createServiceWithName'); + + $this->serviceManager->addAbstractFactory($af1); + $this->serviceManager->addAbstractFactory($af2); + + $this->assertSame($service, $this->serviceManager->create('test')); + } } From 95874b334c8cdfa169efeae6049bb09cdbaab3aa Mon Sep 17 00:00:00 2001 From: Martin Meredith Date: Fri, 9 Nov 2012 15:15:24 +0000 Subject: [PATCH 6/9] Add a bunch of traits to ZF2 As requested @ https://github.com/protecinnovations/zf2-traits/issues/1 This is the initial commit to start getting traits into ZF2. We need to move our tests over - which are currently using Mockery. --- src/ServiceLocatorAwareTrait.php | 48 ++++++++++++++++++++++++++++++++ src/ServiceManagerAwareTrait.php | 38 +++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 src/ServiceLocatorAwareTrait.php create mode 100644 src/ServiceManagerAwareTrait.php diff --git a/src/ServiceLocatorAwareTrait.php b/src/ServiceLocatorAwareTrait.php new file mode 100644 index 00000000..a6affab8 --- /dev/null +++ b/src/ServiceLocatorAwareTrait.php @@ -0,0 +1,48 @@ +service_locator = $serviceLocator; + + return $this; + } + + /** + * getServiceLocator + * + * @return \Zend\ServiceManager\ServiceLocator + */ + public function getServiceLocator() + { + return $this->service_locator; + } +} diff --git a/src/ServiceManagerAwareTrait.php b/src/ServiceManagerAwareTrait.php new file mode 100644 index 00000000..f02ee935 --- /dev/null +++ b/src/ServiceManagerAwareTrait.php @@ -0,0 +1,38 @@ +service_manager = $serviceManager; + + return $this; + } +} From 1a44d78a6f95be7fbccdb9854505db9f50368506 Mon Sep 17 00:00:00 2001 From: Alex Denvir Date: Fri, 9 Nov 2012 15:33:33 +0000 Subject: [PATCH 7/9] Improve coding standards on new traits, add tests --- src/ServiceLocatorAwareTrait.php | 14 +++--- src/ServiceManagerAwareTrait.php | 10 ++-- test/ServiceLocatorAwareTraitTest.php | 49 +++++++++++++++++++ test/ServiceManagerAwareTraitTest.php | 32 ++++++++++++ .../MockServiceLocatorAwareTrait.php | 19 +++++++ .../MockServiceManagerAwareTrait.php | 19 +++++++ 6 files changed, 131 insertions(+), 12 deletions(-) create mode 100644 test/ServiceLocatorAwareTraitTest.php create mode 100644 test/ServiceManagerAwareTraitTest.php create mode 100644 test/TestAsset/MockServiceLocatorAwareTrait.php create mode 100644 test/TestAsset/MockServiceManagerAwareTrait.php diff --git a/src/ServiceLocatorAwareTrait.php b/src/ServiceLocatorAwareTrait.php index a6affab8..658f0178 100644 --- a/src/ServiceLocatorAwareTrait.php +++ b/src/ServiceLocatorAwareTrait.php @@ -19,19 +19,19 @@ trait ServiceLocatorAwareTrait { /** - * @var \Zend\ServiceManager\ServiceLocator + * @var ServiceLocator */ - protected $service_locator = null; + protected $serviceLocator = null; /** * setServiceLocator * - * @param \Zend\ServiceManager\ServiceLocatorInterface $serviceLocator - * @return + * @param ServiceLocatorInterface $serviceLocator + * @return mixed */ public function setServiceLocator(ServiceLocatorInterface $serviceLocator) { - $this->service_locator = $serviceLocator; + $this->serviceLocator = $serviceLocator; return $this; } @@ -39,10 +39,10 @@ public function setServiceLocator(ServiceLocatorInterface $serviceLocator) /** * getServiceLocator * - * @return \Zend\ServiceManager\ServiceLocator + * @return ServiceLocator */ public function getServiceLocator() { - return $this->service_locator; + return $this->serviceLocator; } } diff --git a/src/ServiceManagerAwareTrait.php b/src/ServiceManagerAwareTrait.php index f02ee935..1a0364c8 100644 --- a/src/ServiceManagerAwareTrait.php +++ b/src/ServiceManagerAwareTrait.php @@ -19,19 +19,19 @@ trait ServiceManagerAwareTrait { /** - * @var \Zend\ServiceManager\ServiceManager + * @var ServiceManager */ - protected $service_manager = null; + protected $serviceManager = null; /** * setServiceManager * - * @param \Zend\ServiceManager\ServiceManager $serviceManager - * @return + * @param ServiceManager $serviceManager + * @return mixed */ public function setServiceManager(ServiceManager $serviceManager) { - $this->service_manager = $serviceManager; + $this->serviceManager = $serviceManager; return $this; } diff --git a/test/ServiceLocatorAwareTraitTest.php b/test/ServiceLocatorAwareTraitTest.php new file mode 100644 index 00000000..aeb31810 --- /dev/null +++ b/test/ServiceLocatorAwareTraitTest.php @@ -0,0 +1,49 @@ +assertAttributeEquals(null, 'serviceLocator', $object); + + $serviceLocator = new ServiceManager; + + $object->setServiceLocator($serviceLocator); + + $this->assertAttributeEquals($serviceLocator, 'serviceLocator', $object); + } + + /** + * @requires PHP 5.4 + */ + public function testGetServiceLocator() + { + $object = new TestAsset\MockServiceLocatorAwareTrait; + + $this->assertNull($object->getServiceLocator()); + + $serviceLocator = new ServiceManager; + + $object->setServiceLocator($serviceLocator); + + $this->assertEquals($serviceLocator, $object->getServiceLocator()); + } +} diff --git a/test/ServiceManagerAwareTraitTest.php b/test/ServiceManagerAwareTraitTest.php new file mode 100644 index 00000000..ca0c7df5 --- /dev/null +++ b/test/ServiceManagerAwareTraitTest.php @@ -0,0 +1,32 @@ +assertAttributeEquals(null, 'serviceManager', $object); + + $serviceManager = new \Zend\ServiceManager\ServiceManager; + + $object->setServiceManager($serviceManager); + + $this->assertAttributeEquals($serviceManager, 'serviceManager', $object); + } +} diff --git a/test/TestAsset/MockServiceLocatorAwareTrait.php b/test/TestAsset/MockServiceLocatorAwareTrait.php new file mode 100644 index 00000000..702114f1 --- /dev/null +++ b/test/TestAsset/MockServiceLocatorAwareTrait.php @@ -0,0 +1,19 @@ + Date: Mon, 19 Nov 2012 17:30:14 +0000 Subject: [PATCH 8/9] Improve function comment quality, clean up tests a little --- src/ServiceLocatorAwareTrait.php | 4 ++-- src/ServiceManagerAwareTrait.php | 2 +- test/ServiceLocatorAwareTraitTest.php | 13 +++++-------- test/ServiceManagerAwareTraitTest.php | 8 ++++---- .../MockServiceLocatorAwareTrait.php | 19 ------------------- .../MockServiceManagerAwareTrait.php | 19 ------------------- 6 files changed, 12 insertions(+), 53 deletions(-) delete mode 100644 test/TestAsset/MockServiceLocatorAwareTrait.php delete mode 100644 test/TestAsset/MockServiceManagerAwareTrait.php diff --git a/src/ServiceLocatorAwareTrait.php b/src/ServiceLocatorAwareTrait.php index 658f0178..ab81e565 100644 --- a/src/ServiceLocatorAwareTrait.php +++ b/src/ServiceLocatorAwareTrait.php @@ -24,7 +24,7 @@ trait ServiceLocatorAwareTrait protected $serviceLocator = null; /** - * setServiceLocator + * Set service locator * * @param ServiceLocatorInterface $serviceLocator * @return mixed @@ -37,7 +37,7 @@ public function setServiceLocator(ServiceLocatorInterface $serviceLocator) } /** - * getServiceLocator + * Get service locator * * @return ServiceLocator */ diff --git a/src/ServiceManagerAwareTrait.php b/src/ServiceManagerAwareTrait.php index 1a0364c8..e87af156 100644 --- a/src/ServiceManagerAwareTrait.php +++ b/src/ServiceManagerAwareTrait.php @@ -24,7 +24,7 @@ trait ServiceManagerAwareTrait protected $serviceManager = null; /** - * setServiceManager + * Set service manager * * @param ServiceManager $serviceManager * @return mixed diff --git a/test/ServiceLocatorAwareTraitTest.php b/test/ServiceLocatorAwareTraitTest.php index aeb31810..d33e136a 100644 --- a/test/ServiceLocatorAwareTraitTest.php +++ b/test/ServiceLocatorAwareTraitTest.php @@ -13,14 +13,14 @@ use \PHPUnit_Framework_TestCase as TestCase; use \Zend\ServiceManager\ServiceManager; +/** + * @requires PHP 5.4 + */ class ServiceLocatorAwareTraitTest extends TestCase { - /** - * @requires PHP 5.4 - */ public function testSetServiceLocator() { - $object = new TestAsset\MockServiceLocatorAwareTrait; + $object = $this->getObjectForTrait('\Zend\ServiceManager\ServiceLocatorAwareTrait'); $this->assertAttributeEquals(null, 'serviceLocator', $object); @@ -31,12 +31,9 @@ public function testSetServiceLocator() $this->assertAttributeEquals($serviceLocator, 'serviceLocator', $object); } - /** - * @requires PHP 5.4 - */ public function testGetServiceLocator() { - $object = new TestAsset\MockServiceLocatorAwareTrait; + $object = $this->getObjectForTrait('\Zend\ServiceManager\ServiceLocatorAwareTrait'); $this->assertNull($object->getServiceLocator()); diff --git a/test/ServiceManagerAwareTraitTest.php b/test/ServiceManagerAwareTraitTest.php index ca0c7df5..4a74bc83 100644 --- a/test/ServiceManagerAwareTraitTest.php +++ b/test/ServiceManagerAwareTraitTest.php @@ -12,14 +12,14 @@ use \PHPUnit_Framework_TestCase as TestCase; +/** + * @requires PHP 5.4 + */ class ServiceManagerAwareTraitTest extends TestCase { - /** - * @requires PHP 5.4 - */ public function testSetServiceManager() { - $object = new TestAsset\MockServiceManagerAwareTrait; + $object = $this->getObjectForTrait('\Zend\ServiceManager\ServiceManagerAwareTrait'); $this->assertAttributeEquals(null, 'serviceManager', $object); diff --git a/test/TestAsset/MockServiceLocatorAwareTrait.php b/test/TestAsset/MockServiceLocatorAwareTrait.php deleted file mode 100644 index 702114f1..00000000 --- a/test/TestAsset/MockServiceLocatorAwareTrait.php +++ /dev/null @@ -1,19 +0,0 @@ - Date: Tue, 20 Nov 2012 10:25:05 +0000 Subject: [PATCH 9/9] Remove SharedEventManagerAwareTrait and ServiceManagerAwareTrait, and associated tests --- src/ServiceManagerAwareTrait.php | 38 --------------------------- test/ServiceManagerAwareTraitTest.php | 32 ---------------------- 2 files changed, 70 deletions(-) delete mode 100644 src/ServiceManagerAwareTrait.php delete mode 100644 test/ServiceManagerAwareTraitTest.php diff --git a/src/ServiceManagerAwareTrait.php b/src/ServiceManagerAwareTrait.php deleted file mode 100644 index e87af156..00000000 --- a/src/ServiceManagerAwareTrait.php +++ /dev/null @@ -1,38 +0,0 @@ -serviceManager = $serviceManager; - - return $this; - } -} diff --git a/test/ServiceManagerAwareTraitTest.php b/test/ServiceManagerAwareTraitTest.php deleted file mode 100644 index 4a74bc83..00000000 --- a/test/ServiceManagerAwareTraitTest.php +++ /dev/null @@ -1,32 +0,0 @@ -getObjectForTrait('\Zend\ServiceManager\ServiceManagerAwareTrait'); - - $this->assertAttributeEquals(null, 'serviceManager', $object); - - $serviceManager = new \Zend\ServiceManager\ServiceManager; - - $object->setServiceManager($serviceManager); - - $this->assertAttributeEquals($serviceManager, 'serviceManager', $object); - } -}