We will work on Apr 26th (Saturday) and will be off from Apr 30th (Wednesday) until May 2nd (Friday) for public holiday in our country

Commit 460f4a42 authored by Ivan Chepurnyi's avatar Ivan Chepurnyi

Improvements in the fixtures models

parent decb1d48
......@@ -89,10 +89,15 @@ class EcomDev_PHPUnit_Model_Fixture_Processor_Eav
$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();
......@@ -126,16 +131,35 @@ class EcomDev_PHPUnit_Model_Fixture_Processor_Eav
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;
}
}
\ No newline at end of file
......@@ -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
*
......@@ -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
*
......@@ -99,6 +116,69 @@ abstract class EcomDev_PHPUnit_Model_Mysql4_Fixture_AbstractEav
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
*
......@@ -139,16 +219,43 @@ abstract class EcomDev_PHPUnit_Model_Mysql4_Fixture_AbstractEav
// 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
......
......@@ -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
......
<?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);
}
\ No newline at end of file
......@@ -114,6 +114,37 @@
<catalog_product>ecomdev_phpunit/fixture_eav_catalog_product</catalog_product>
<catalog_category>ecomdev_phpunit/fixture_eav_catalog_category</catalog_category>
</eav>
<default_data>
<category>
<root>
<entity_id>1</entity_id>
<parent_id>0</parent_id>
<path>1</path>
<position>0</position>
<level>0</level>
<children_count>1</children_count>
<name>Root Catalog</name>
<url_key>root-catalog</url_key>
<is_active>1</is_active>
<is_anchor>0</is_anchor>
<attribute_set_id>0</attribute_set_id>
</root>
<default_category>
<entity_id>2</entity_id>
<parent_id>1</parent_id>
<path>1/2</path>
<position>1</position>
<level>1</level>
<children_count>0</children_count>
<name>Default Category</name>
<url_key>default-category</url_key>
<is_active>1</is_active>
<is_anchor>0</is_anchor>
<display_mode>PRODUCTS</display_mode>
<include_in_menu>1</include_in_menu>
</default_category>
</category>
</default_data>
</fixture>
<app>
<!-- Application class name for running tests -->
......
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