Commit d77b04cd authored by Ivan Chepurnyi's avatar Ivan Chepurnyi

+ New feature #12: Support annotations per test function/class to enable/disable cache

parent 1260662b
......@@ -179,15 +179,11 @@ class EcomDev_PHPUnit_Model_App extends Mage_Core_Model_App
// Set using cache
// for things that shouldn't be reloaded each time
EcomDev_Utils_Reflection::setRestrictedPropertyValue(
$this->_cache,
'_allowedCacheOptions',
array(
'eav' => 1,
'layout' => 1,
'translate' => 1
)
);
$this->setCacheOptions(array(
'eav' => 1,
'layout' => 1,
'translate' => 1
));
// Clean cache before the whole suite is running
$this->getCache()->clean();
......@@ -227,7 +223,35 @@ class EcomDev_PHPUnit_Model_App extends Mage_Core_Model_App
$this->replaceRegistry(self::REGISTRY_PATH_SHARED_STORAGE, new Varien_Object());
return $this;
}
/**
* Sets cache options for test case
*
* @param array $options
* @return EcomDev_PHPUnit_Model_App
*/
public function setCacheOptions(array $options)
{
EcomDev_Utils_Reflection::setRestrictedPropertyValue(
$this->_cache,
'_allowedCacheOptions',
$options
);
}
/**
* Retrieve cache options for test case
*
* @return array
*/
public function getCacheOptions()
{
return EcomDev_Utils_Reflection::getRestrictedPropertyValue(
$this->_cache,
'_allowedCacheOptions'
);
}
/**
* Retrieves a model from config and checks it on interface implementation
*
......
......@@ -51,6 +51,9 @@ class EcomDev_PHPUnit_Model_Fixture
// Key for loaded entities by EAV loaders
const STORAGE_KEY_ENTITIES = 'entities';
// Key for loaded cache options
const STORAGE_KEY_CACHE_OPTIONS = 'cache_options';
// Key for created scope models
const STORAGE_KEY_SCOPE = 'scope';
......@@ -275,6 +278,11 @@ class EcomDev_PHPUnit_Model_Fixture
'loadFixture',
array('class', 'method')
);
$cacheOptions = $testCase->getAnnotationByName('cache', 'method');
$this->_parseCacheOptions($cacheOptions);
$this->_loadFixtureFiles($fixtures, $testCase);
......@@ -301,9 +309,42 @@ class EcomDev_PHPUnit_Model_Fixture
null, array($className, 'loadSharedFixture', 'class')
);
$cacheOptions = $method->invokeArgs(
null, array($className, 'cache', 'class')
);
$this->_parseCacheOptions($cacheOptions);
$this->_loadFixtureFiles($fixtures, $className);
return $this;
}
/**
* Loads test case cache on off annotations
*
* @param array $annotations
* @return EcomDev_PHPUnit_Model_Fixture
*/
protected function _parseCacheOptions($annotations)
{
$cacheOptions = array();
foreach ($annotations as $annotation) {
list($action, $cacheType) = preg_split('/\s+/', trim($annotation));
$flag = ($action === 'off' ? 0 : 1);
if ($cacheType === 'all') {
foreach (Mage::app()->getCacheInstance()->getTypes() as $type) {
$cacheOptions[$type->getId()] = $flag;
}
} else {
$cacheOptions[$cacheType] = $flag;
}
}
if ($cacheOptions) {
$this->_fixture['cache_options'] = $cacheOptions;
}
}
/**
* Loads fixture files
......@@ -411,6 +452,36 @@ class EcomDev_PHPUnit_Model_Fixture
$this->_fixture = array();
}
/**
* Applies cache options for current test or test case
*
* @param array $options
* @return EcomDev_PHPUnit_Model_Fixture
*/
protected function _applyCacheOptions($options)
{
$originalOptions = Mage::app()->getCacheOptions();
$this->setStorageData(self::STORAGE_KEY_CACHE_OPTIONS, $originalOptions);
$options += $originalOptions;
Mage::app()->setCacheOptions($options);
return $this;
}
/**
* Discards changes that were made to Magento cache
*
* @return EcomDev_PHPUnit_Model_Fixture
*/
protected function _discardCacheOptions()
{
Mage::app()->setCacheOptions(
$this->getStorageData(self::STORAGE_KEY_CACHE_OPTIONS)
);
return $this;
}
/**
* Applies fixture configuration values into Mage_Core_Model_Config
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment