Skip to content

Commit

Permalink
Merge branch 'release/0.3.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanChepurnyi committed Oct 24, 2014
2 parents 1cd444f + 460f4a4 commit e9104ee
Show file tree
Hide file tree
Showing 17 changed files with 368 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ public function getHttpHost($trimPort = false)
*
* @see Mage_Core_Controller_Request_Http::getBaseUrl()
*/
public function getBaseUrl()
public function getBaseUrl($raw = false)
{
return $this->_baseUrl;
}
Expand Down
6 changes: 3 additions & 3 deletions app/code/community/EcomDev/PHPUnit/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ public function flushReplaceInstanceCreation()
*/
public function getModelInstance($modelClass='', $constructArguments=array())
{
if (!isset($this->_replaceInstanceCreation['model'][$modelClass])) {
return parent::getModelInstance($modelClass, $constructArguments);
if (!isset($this->_replaceInstanceCreation['model'][(string)$modelClass])) {
return parent::getModelInstance((string)$modelClass, $constructArguments);
}

return $this->_replaceInstanceCreation['model'][$modelClass];
return $this->_replaceInstanceCreation['model'][(string)$modelClass];
}

/**
Expand Down
4 changes: 2 additions & 2 deletions app/code/community/EcomDev/PHPUnit/Model/Fixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public function isScopeDefault()
public function loadByTestCase(PHPUnit_Framework_TestCase $testCase)
{
$fixtures = EcomDev_PHPUnit_Test_Case_Util::getAnnotationByNameFromClass(
get_class($testCase), 'loadFixture', array('class', 'method'), $testCase->getName(false)
get_class($testCase), 'loadFixture', array('method', 'class'), $testCase->getName(false)
);

$this->_loadFixtureFiles($fixtures, $testCase);
Expand Down Expand Up @@ -412,7 +412,7 @@ public function loadYaml($filePath)
if (empty($this->_fixture)) {
$this->_fixture = $data;
} else {
$this->_fixture = array_merge_recursive($this->_fixture, $data);
$this->_fixture = array_replace_recursive($this->_fixture, $data);
}

return $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ protected function _restoreConfig()
{
Mage::getConfig()->loadScopeSnapshot();
Mage::getConfig()->loadDb();

// Flush website and store configuration caches
foreach (Mage::app()->getWebsites(true) as $website) {
EcomDev_Utils_Reflection::setRestrictedPropertyValue(
$website, '_configCache', array()
);
}
foreach (Mage::app()->getStores(true) as $store) {
EcomDev_Utils_Reflection::setRestrictedPropertyValue(
$store, '_configCache', array()
);
}

return $this;
}

Expand All @@ -160,9 +173,19 @@ protected function _setConfigNodeValue($path, $value)
$value = $backend->getValue();
}

if (is_array($value)) {
Mage::throwException(
sprintf(
'There is a collision in configuration value %s. Got: %s',
$path,
print_r($value, true)
)
);
}

Mage::getConfig()->setNode($path, $value);
return $this;
}


}
}
36 changes: 30 additions & 6 deletions app/code/community/EcomDev/PHPUnit/Model/Fixture/Processor/Eav.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,15 @@ public function apply(array $data, $key, EcomDev_PHPUnit_Model_FixtureInterface
$this->getResource()->beginTransaction();

foreach ($data as $entityType => $values) {
$eavLoaders[] = $this->_getEavLoader($entityType)
$eavLoaders[$entityType] = $this->_getEavLoader($entityType)
->setFixture($fixture)
->setOptions($fixture->getOptions())
->loadEntity($entityType, $values);
->setOptions($fixture->getOptions());

if ($eavLoaders[$entityType] instanceof EcomDev_PHPUnit_Model_Mysql4_Fixture_RestoreAwareInterface) {
$eavLoaders[$entityType]->saveData($entityType);
}

$eavLoaders[$entityType]->loadEntity($entityType, $values);
}

$this->getResource()->commit();
Expand Down Expand Up @@ -126,16 +131,35 @@ public function discard(array $data, $key, EcomDev_PHPUnit_Model_FixtureInterfac
EcomDev_PHPUnit_Model_FixtureInterface::SCOPE_SHARED);
}

$typesToRestore = array();
$this->getResource()->beginTransaction();
foreach (array_keys($data) as $entityType) {
$eavLoader = $this->_getEavLoader($entityType);

if (in_array($entityType, $ignoreCleanUp)) {
if ($eavLoader instanceof EcomDev_PHPUnit_Model_Mysql4_Fixture_RestoreAwareInterface) {
$eavLoader->clearData($entityType);
}
continue;
}
$this->_getEavLoader($entityType)
->cleanEntity($entityType);
}

$eavLoader->cleanEntity($entityType);

if ($eavLoader instanceof EcomDev_PHPUnit_Model_Mysql4_Fixture_RestoreAwareInterface) {
$typesToRestore[$entityType] = $eavLoader;
}
}
$this->getResource()->commit();

if ($typesToRestore) {
$this->getResource()->beginTransaction();
foreach ($typesToRestore as $entityType => $eavLoader) {
$eavLoader->restoreData($entityType)
->clearData($entityType);
}
$this->getResource()->commit();
}

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
*/
abstract class EcomDev_PHPUnit_Model_Mysql4_Fixture_AbstractEav
extends EcomDev_PHPUnit_Model_Mysql4_Fixture_AbstractComplex
implements EcomDev_PHPUnit_Model_Mysql4_Fixture_RestoreAwareInterface
{
const RESTORE_KEY = 'restore_%s_data';

/**
* List of indexers required to build
*
Expand All @@ -37,6 +40,20 @@ abstract class EcomDev_PHPUnit_Model_Mysql4_Fixture_AbstractEav
*/
protected $_originalIndexers = array();

/**
* List of tables that should be restored after run
*
* @var string[]
*/
protected $_restoreTables = array();

/**
* Default data for eav entity
*
* @var array
*/
protected $_defaultData = array();

/**
* Retrieve required indexers for re-building
*
Expand Down Expand Up @@ -99,6 +116,69 @@ public function cleanEntity($entityType)
return $this;
}


/**
* Saves data for restoring it after fixture has been cleaned up
*
* @param string $code storage code
* @return $this
*/
public function saveData($code)
{
if ($this->_restoreTables) {
$storageKey = sprintf(self::RESTORE_KEY, $code);
$data = array();
foreach ($this->_restoreTables as $table) {
$select = $this->_getReadAdapter()->select();
$select->from($table);
$data[$table] = $this->_getReadAdapter()->fetchAll($select);
}
$this->_fixture->setStorageData($storageKey, $data);
}

return $this;
}

/**
* Restored saved data
*
* @param string $code storage code
* @return $this
*/
public function restoreData($code)
{
if ($this->_restoreTables) {
$storageKey = sprintf(self::RESTORE_KEY, $code);
$data = $this->_fixture->getStorageData($storageKey);
foreach ($this->_restoreTables as $table) {
if (!empty($data[$table])) {
$this->_getWriteAdapter()->insertOnDuplicate(
$table,
$data[$table]
);
}
}
}

return $this;
}

/**
* Clears storage from stored backup data
*
* @param $code
* @return $this
*/
public function clearData($code)
{
if ($this->_restoreTables) {
$storageKey = sprintf(self::RESTORE_KEY, $code);
$this->_fixture->setStorageData($storageKey, array());
}

return $this;
}

/**
* Loads EAV data into DB tables
*
Expand All @@ -112,7 +192,7 @@ public function loadEntity($entityType, $values)
$this->_originalIndexers = $this->_requiredIndexers;
if (!empty($this->_options['addRequiredIndex'])) {
foreach ($this->_options['addRequiredIndex'] as $data) {
if (preg_match('/^([a-z0-9_\\-])+\\s+([a-z0-9_\\-])\s*$/i', $data, $match)
if (preg_match('/^([a-z0-9_\\-]+)\\s+([a-z0-9_\\-]+)\s*$/i', $data, $match)
&& $match[1] == $entityType) {
$this->_requiredIndexers[] = $match[2];
}
Expand All @@ -139,16 +219,43 @@ public function loadEntity($entityType, $values)
// and rows list as value
// See getCustomTableRecords
$customValues = array();

if ($this->_defaultData) {
$dataToInsert = $this->_defaultData;
// Prevent insertion of default data,
// if there is already data available
foreach ($values as $index => $row) {
if (isset($row[$this->_getEntityIdField($entityTypeModel)])
&& isset($dataToInsert[$this->_getEntityIdField($entityTypeModel)])) {
$dataToInsert = array();
break;
}
}

foreach ($dataToInsert as $row) {
array_unshift($values, $row);
}
}


foreach ($values as $index => &$row) {
foreach ($values as $index => $row) {
if (!isset($row[$this->_getEntityIdField($entityTypeModel)])) {
throw new RuntimeException('Entity Id should be specified in EAV fixture');
}

// Fulfill necessary information
$row['entity_type_id'] = $entityTypeModel->getEntityTypeId();
$values[$index]['entity_type_id'] = $entityTypeModel->getEntityTypeId();
$row = $values[$index];

if (!isset($row['attribute_set_id'])) {
$row['attribute_set_id'] = $entityTypeModel->getDefaultAttributeSetId();
$defaultAttributeSet = $entityTypeModel->getDefaultAttributeSetId();

// Fix Magento core issue with attribute set information for customer and its address
if (in_array($entityType, array('customer', 'customer_address'))) {
$defaultAttributeSet = 0;
}

$values[$index]['attribute_set_id'] = $defaultAttributeSet;
}

// Preparing entity table record
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,36 @@
*/
class EcomDev_PHPUnit_Model_Mysql4_Fixture_Eav_Catalog_Category extends EcomDev_PHPUnit_Model_Mysql4_Fixture_Eav_Catalog_Abstract
{
const XML_PATH_DEFAULT_DATA = 'phpunit/suite/fixture/default_data/category';

protected $_requiredIndexers = array(
'catalog_category_flat'
);

protected function _construct()
{
parent::_construct();
$defaultData = Mage::getConfig()->getNode(self::XML_PATH_DEFAULT_DATA);

if ($defaultData) {
foreach ($defaultData->children() as $item) {
if (!isset($item->entity_id)) {
continue;
}

$entityId = (string)$item->entity_id;
$this->_defaultData[$entityId] = array();
foreach ($item->children() as $value) {
$this->_defaultData[$entityId][$value->getName()] = (string)$value;
}
}
}

$this->_restoreTables[] = $this->getTable('catalog/category');
foreach (array('datetime', 'decimal', 'int', 'text', 'varchar') as $suffix) {
$this->_restoreTables[] = $this->getTable(array('catalog/category', $suffix));
}
}

/**
* Overridden to add easy fixture loading for product associations
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

interface EcomDev_PHPUnit_Model_Mysql4_Fixture_RestoreAwareInterface
{
/**
* Saves data for restoring it after fixture has been cleaned up
*
* @param string $code storage code
* @return $this
*/
public function saveData($code);

/**
* Restored saved data
*
* @param string $code storage code
* @return $this
*/
public function restoreData($code);

/**
* Clears storage from stored backup data
*
* @param $code
* @return $this
*/
public function clearData($code);
}
11 changes: 11 additions & 0 deletions app/code/community/EcomDev/PHPUnit/Test/Case/Helper/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,19 @@ class EcomDev_PHPUnit_Test_Case_Helper_Session
*/
public function helperMockSession($classAlias, array $methods = array())
{
if (!empty($methods) && !in_array('start', $methods, true)) {
$methods[] = 'start';
}

$sessionMock = EcomDev_PHPUnit_Helper::invoke('mockModel', $classAlias, $methods)
->disableOriginalConstructor();

TestUtil::replaceByMock('singleton', $classAlias, $sessionMock);

$sessionMock->expects($this->testCase->any())
->method('start')
->willReturnSelf();

return $sessionMock;
}

Expand All @@ -58,6 +67,8 @@ public function helperMockSession($classAlias, array $methods = array())
*/
public function helperAdminSession(array $resources = array())
{
$this->helperMockSession('core/session', array('renew'));
$this->helperMockSession('adminhtml/session', array('renew'));
$session = $this->helperMockSession('admin/session', array('refreshAcl'));
$user = $this->createUser();
$this->loadRules($user, $this->getAcl(), $resources);
Expand Down
Loading

0 comments on commit e9104ee

Please sign in to comment.