/** * Clear InstanceManager cache * * @return Magento_Test_ObjectManager */ public function clearCache() { foreach ($this->_classesToDestruct as $className) { if ($this->hasSharedInstance($className)) { $object = $this->get($className); if ($object) { // force to cleanup circular references $object->__destruct(); } } } $instanceManagerNew = new Magento_Di_InstanceManager_Zend(); $instanceManagerNew->addSharedInstance($this, 'Magento_ObjectManager'); if ($this->_di->instanceManager()->hasSharedInstance('Mage_Core_Model_Resource')) { $resource = $this->_di->instanceManager()->getSharedInstance('Mage_Core_Model_Resource'); $instanceManagerNew->addSharedInstance($resource, 'Mage_Core_Model_Resource'); } $this->_di->setInstanceManager($instanceManagerNew); return $this; }
public function testClearCache() { $object = $this->getMock('stdClass', array('__destruct')); $object->expects($this->once())->method('__destruct'); $resource = $this->getMock('stdClass', array('__destruct')); $object->expects($this->once())->method('__destruct'); $instanceManager = new Magento_Di_InstanceManager_Zend(); $instanceManager->addSharedInstance($object, 'sharedInstance'); $instanceManager->addSharedInstance($resource, 'Mage_Core_Model_Resource'); $diInstance = new Magento_Di_Zend(); $model = new Magento_Test_ObjectManager(null, $diInstance); // Reflection is the only way to setup fixture input data in place of the hard-coded property value $reflectionClass = new ReflectionProperty(get_class($model), '_classesToDestruct'); $reflectionClass->setAccessible(true); $reflectionClass->setValue($model, array('sharedInstance', 'nonRegisteredInstance')); $diInstance->setInstanceManager($instanceManager); $this->assertSame($model, $model->clearCache()); $this->assertNotSame($instanceManager, $diInstance->instanceManager()); $this->assertSame($model, $diInstance->instanceManager()->getSharedInstance('Magento_ObjectManager')); $this->assertSame($resource, $diInstance->instanceManager()->getSharedInstance('Mage_Core_Model_Resource')); $this->assertFalse($diInstance->instanceManager()->hasSharedInstance('sharedInstance')); }