Skip to content

Commit b18329d

Browse files
committed
Convert to using a stack for test scope storage (see issue EcomDev#178)
The original code would only store the most recent version of the scop data when calling applyTestScope. If you called the method twice then you lost the original scope. Parts of the EcomDev_PHPUnit code are using the registry to store information. Currently the test scope is only setup at the suite level but if tests in the suite expect a clean registry then there are problems. This change converts to using a stack to store the scope. Each time you call applyTestScope the current scope data is added to the stack. Each time you call discardTestScope the previous scope data is restored. If you call discardTestScope and there is no stored scope data, a RuntimeException is thrown.
1 parent b937a83 commit b18329d

1 file changed

Lines changed: 18 additions & 15 deletions

File tree

  • app/code/community/EcomDev/PHPUnit/Model

app/code/community/EcomDev/PHPUnit/Model/App.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,11 @@ class EcomDev_PHPUnit_Model_App extends Mage_Core_Model_App
5959
const XML_PATH_CONTROLLER_RESPONSE = 'phpunit/suite/controller/response/class';
6060

6161
/**
62-
* Old test scope data to be returned back after unit tests are finished
62+
* Test scope data to be returned back after unit tests are finished
6363
*
6464
* @var array
6565
*/
66-
protected static $_oldTestScope = array(
67-
'app' => null,
68-
'config' => null,
69-
'events' => null,
70-
'registry' => null,
71-
);
66+
protected static $_testScopeStack = array();
7267

7368
/**
7469
* Configuration model class name for unit tests
@@ -124,10 +119,12 @@ class EcomDev_PHPUnit_Model_App extends Mage_Core_Model_App
124119
public static function applyTestScope()
125120
{
126121
// Save old environment variables
127-
self::$_oldTestScope['app'] = EcomDev_Utils_Reflection::getRestrictedPropertyValue('Mage', '_app');
128-
self::$_oldTestScope['config'] = EcomDev_Utils_Reflection::getRestrictedPropertyValue('Mage', '_config');
129-
self::$_oldTestScope['events'] = EcomDev_Utils_Reflection::getRestrictedPropertyValue('Mage', '_events');
130-
self::$_oldTestScope['registry'] = EcomDev_Utils_Reflection::getRestrictedPropertyValue('Mage', '_registry');
122+
self::$_testScopeStack[] = array(
123+
'app' => EcomDev_Utils_Reflection::getRestrictedPropertyValue('Mage', '_app'),
124+
'config' => EcomDev_Utils_Reflection::getRestrictedPropertyValue('Mage', '_config'),
125+
'events' => EcomDev_Utils_Reflection::getRestrictedPropertyValue('Mage', '_events'),
126+
'registry' => EcomDev_Utils_Reflection::getRestrictedPropertyValue('Mage', '_registry'),
127+
);
131128

132129

133130
// Setting environment variables for unit tests
@@ -415,11 +412,17 @@ protected function _initStores()
415412
*/
416413
public static function discardTestScope()
417414
{
415+
if(empty(self::$_testScopeStack)) {
416+
throw new RuntimeException('No test scope to discard');
417+
}
418+
419+
$previousScope = array_pop(self::$_testScopeStack);
420+
418421
// Setting environment variables for unit tests
419-
EcomDev_Utils_Reflection::setRestrictedPropertyValue('Mage', '_app', self::$_oldTestScope['app']);
420-
EcomDev_Utils_Reflection::setRestrictedPropertyValue('Mage', '_config', self::$_oldTestScope['config']);
421-
EcomDev_Utils_Reflection::setRestrictedPropertyValue('Mage', '_events', self::$_oldTestScope['events']);
422-
EcomDev_Utils_Reflection::setRestrictedPropertyValue('Mage', '_registry', self::$_oldTestScope['registry']);
422+
EcomDev_Utils_Reflection::setRestrictedPropertyValue('Mage', '_app', $previousScope['app']);
423+
EcomDev_Utils_Reflection::setRestrictedPropertyValue('Mage', '_config', $previousScope['config']);
424+
EcomDev_Utils_Reflection::setRestrictedPropertyValue('Mage', '_events', $previousScope['events']);
425+
EcomDev_Utils_Reflection::setRestrictedPropertyValue('Mage', '_registry', $previousScope['registry']);
423426
}
424427

425428
/**

0 commit comments

Comments
 (0)