Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix for broken handling of Zend\ServiceManager\ServiceManager::shareByDefault = false (Issue #3408) #3409

Closed
wants to merge 3 commits into from

3 participants

@tux-rampage

Changed check for $this->shareByDefault() in
Zend\ServiceManager\ServiceManager::get() to allow shared
instances that are explicitly defined as shared when shareByDefault is
set to false.

This resolves issue #3408

@tux-rampage tux-rampage [+BUGFIX] Fixed shareByDefault check
Changed check for $this->shareByDefault() in
Zend\ServiceManager\ServiceManager::get() to allow shared
instances that are explicitly defined as shared when shareByDefault is
set to false.

Resolves #3408
8c7fab9
@weierophinney

Please add a unit test to verify the change (and prevent future regressions). Thanks!

@tux-rampage tux-rampage [~TASK] Unit test for shareByDefault = false
Implemented Unit test for testing correct ServiceManager behavior with 
shareByDefault set to false.

Resolves: #3408, #3409
44af562
@tux-rampage

Hi Matthew,

I added the unit test as requested. Please check if this is ok for a pull now.

Thanks

@Ocramius
Collaborator

@netiul is this what you were referring to when you were talking about the shared flag on a service?

@tux-rampage

@Ocramius Yes, this seems to address the same issue as #3439

@weierophinney weierophinney closed this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/3439'
Close #3439
Fixes #3409
b756cd7
@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney Merge branch 'hotfix/3439'
Close #3439
Fixes #3409
325909c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 11, 2013
  1. @tux-rampage

    [+BUGFIX] Fixed shareByDefault check

    tux-rampage authored
    Changed check for $this->shareByDefault() in
    Zend\ServiceManager\ServiceManager::get() to allow shared
    instances that are explicitly defined as shared when shareByDefault is
    set to false.
    
    Resolves #3408
Commits on Jan 15, 2013
  1. @tux-rampage

    [~TASK] Unit test for shareByDefault = false

    tux-rampage authored
    Implemented Unit test for testing correct ServiceManager behavior with 
    shareByDefault set to false.
    
    Resolves: #3408, #3409
  2. @tux-rampage
This page is out of date. Refresh to see the latest.
View
3  library/Zend/ServiceManager/ServiceManager.php
@@ -453,7 +453,8 @@ public function get($name, $usePeeringServiceManagers = true)
));
}
- if ($this->shareByDefault() && (!isset($this->shared[$cName]) || $this->shared[$cName] === true)) {
+ if ((isset($this->shared[$cName]) && $this->shared[$cName] === true)
+ || (!isset($this->shared[$cName]) && $this->shareByDefault())) {
$this->instances[$cName] = $instance;
}
View
130 tests/ZendTest/ServiceManager/NoShareByDefaultTest.php
@@ -0,0 +1,130 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_ServiceManager
+ */
+
+namespace ZendTest\ServiceManager;
+
+use Zend\ServiceManager\ServiceManager;
+use PHPUnit_Framework_TestCase as TestCase;
+use stdClass;
+use ReflectionObject;
+
+/**
+ * Non-ShareByDefault service manager test
+ *
+ * This will test the service manager behavior when shareByDefault is set to
+ * FALSE
+ */
+class NoShareByDefaultTest extends TestCase
+{
+ /**
+ * Service manager test instance
+ *
+ * @var \Zend\ServiceManager\ServiceManager
+ */
+ private $serviceManager = null;
+
+ /**
+ * (non-PHPdoc)
+ * @see PHPUnit_Framework_TestCase::setUp()
+ */
+ protected function setUp()
+ {
+ $this->serviceManager = $this->createServiceManager();
+ }
+
+ /**
+ * (non-PHPdoc)
+ * @see PHPUnit_Framework_TestCase::tearDown()
+ */
+ protected function tearDown()
+ {
+ $this->serviceManager = null;
+ }
+
+ /**
+ * Create a service manager instance with shareByDefault set to FALSE
+ *
+ * @return \Zend\ServiceManager\ServiceManager
+ */
+ private function createServiceManager()
+ {
+ $instance = new ServiceManager();
+ $reflection = new ReflectionObject($instance);
+ $property = $reflection->getProperty('shareByDefault');
+ $property->setAccessible(true);
+ $property->setValue($instance, false);
+
+ return $instance;
+ }
+
+ /**
+ * Test creating non-shared services by abstract factory when shareByDefault is FALSE
+ *
+ * shareByDefault = false
+ * Service definition share = undefined -> not shared
+ *
+ * @covers Zend\ServiceManager\ServiceManager::get
+ */
+ public function testAbstractFactoryServiceIsNotSharedWithNoShareByDefault()
+ {
+ $factory = $this->getMockForAbstractClass('Zend\ServiceManager\AbstractFactoryInterface');
+ $factory->expects($this->any())
+ ->method('canCreateServiceWithName')
+ ->will($this->returnValue(true));
+
+ $factory->expects($this->any())
+ ->method('createServiceWithName')
+ ->will($this->returnCallback(function() {
+ return new stdClass();
+ }));
+
+ $this->serviceManager->addAbstractFactory($factory);
+ $a = $this->serviceManager->get('testservice');
+
+ $this->assertNotSame($a, $this->serviceManager->get('testservice'), 'Retrived service must not be shared');
+ }
+
+ /**
+ * Test non-shared declared service works
+ *
+ * Services declared as not shared explicitly should still not be shared.
+ *
+ * shareByDefault = false
+ * Service definition share = false -> not shared
+ *
+ * @covers Zend\ServiceManager\ServiceManager::get
+ */
+ public function testServiceDeclaredNotSharedWorksWithNoShareByDefault()
+ {
+ $this->serviceManager->setInvokableClass('notsharedtest', 'stdClass', false);
+ $a = $this->serviceManager->get('notsharedtest');
+
+ $this->assertNotSame($a, $this->serviceManager->get('notsharedtest'), 'Registered service is not expected to be shared');
+ }
+
+ /**
+ * Test shared service works
+ *
+ * Even though shareByDefault is set to false, services declared as shared
+ * explicitly should still be shared.
+ *
+ * shareByDefault = false
+ * Service definition share = true -> shared
+ *
+ * @covers Zend\ServiceManager\ServiceManager::get
+ */
+ public function testServiceDeclaredSharedWorksWithNoShareByDefault()
+ {
+ $this->serviceManager->setInvokableClass('sharedtest', 'stdClass', true);
+ $a = $this->serviceManager->get('sharedtest');
+
+ $this->assertSame($a, $this->serviceManager->get('sharedtest'), 'Registered service is expected to be shared');
+ }
+}
Something went wrong with that request. Please try again.