Commit ccf93cc0 authored by Ivan Chepurnyi's avatar Ivan Chepurnyi

Merge pull request #120 from aligent/feature/attribute_fixture_loader

Re-enable Attribute fixture loader
parents 66923042 40a66db2
......@@ -36,6 +36,8 @@ class EcomDev_PHPUnit_Model_Fixture
// Configuration path for attribute loaders
const XML_PATH_FIXTURE_ATTRIBUTE_LOADERS = 'phpunit/suite/fixture/attribute';
// Default attribute loader class alias
const DEFAULT_ATTRIBUTE_LOADER_CLASS = 'ecomdev_phpunit/fixture_attribute_default';
// Default eav loader class node in loaders configuration
/* @deprecated since 0.3.0 */
......
<?php
/**
* PHP Unit test suite for Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category EcomDev
* @package EcomDev_PHPUnit
* @copyright Copyright (c) 2013 EcomDev BV (http://www.ecomdev.org)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
* @author Steve Rice <srice@endertech.com>
* @author Jonathan Day <jonathan@aligent.com.au>
*/
class EcomDev_PHPUnit_Model_Fixture_Processor_Attributes
extends Mage_Core_Model_Abstract
implements EcomDev_PHPUnit_Model_Fixture_Processor_Interface
{
const STORAGE_KEY = 'entities';
// Configuration path for attribute loaders
const XML_PATH_FIXTURE_ATTRIBUTE_LOADERS = 'phpunit/suite/fixture/attribute';
// Default attribute loader class node in loaders configuration
const DEFAULT_ATTRIBUTE_LOADER_NODE = 'default';
// Default attribute loader class alias
const DEFAULT_ATTRIBUTE_LOADER_CLASS = 'ecomdev_phpunit/fixture_attribute_default';
/**
* Initialize fixture resource model
*/
protected function _construct()
{
$this->_init('ecomdev_phpunit/fixture');
}
/**
* Does nothing
*
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Attributes
*/
public function initialize(EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
return $this;
}
/**
* Retrieves attribute loader for a particular attribute type
*
* @param string $entityType
* @return EcomDev_PHPUnit_Model_Mysql4_Fixture_Attribute_Abstract
*/
protected function _getAttributeLoader($entityType)
{
$loaders = Mage::getConfig()->getNode(self::XML_PATH_FIXTURE_ATTRIBUTE_LOADERS);
if (isset($loaders->$entityType)) {
$classAlias = (string)$loaders->$entityType;
} elseif (isset($loaders->{self::DEFAULT_ATTRIBUTE_LOADER_NODE})) {
$classAlias = (string)$loaders->{self::DEFAULT_ATTRIBUTE_LOADER_NODE};
} else {
$classAlias = self::DEFAULT_ATTRIBUTE_LOADER_CLASS;
}
return Mage::getResourceSingleton($classAlias);
}
/**
* Apply attribute records from fixture file
*
* @param array $data
* @param string $key
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
*
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Attributes
*/
public function apply(array $data, $key, EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
$attributeLoaders = array();
$this->getResource()->beginTransaction();
foreach ($data as $entityType => $values) {
$attributeLoaders[] = $this->_getAttributeLoader($entityType)
->setFixture($fixture)
->setOptions($fixture->getOptions())
->loadAttribute($entityType, $values);
}
$this->getResource()->commit();
foreach ($attributeLoaders as $attributeLoader){
$attributeLoader->runRequiredIndexers();
}
$fixture->setStorageData(self::STORAGE_KEY, array_keys($data));
return $this;
}
/**
* Discard applied attribute records
*
* @param array[] $data
* @param string $key
* @param EcomDev_PHPUnit_Model_Fixture_Interface $fixture
*
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Attributes
*/
public function discard(array $data, $key, EcomDev_PHPUnit_Model_Fixture_Interface $fixture)
{
$ignoreCleanUp = array();
// Ignore cleaning of entities if shared fixture loaded something for them
if ($fixture->isScopeLocal()
&& $fixture->getStorageData(self::STORAGE_KEY,
EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_SHARED)) {
$ignoreCleanUp = $fixture->getStorageData(self::STORAGE_KEY,
EcomDev_PHPUnit_Model_Fixture_Interface::SCOPE_SHARED);
}
$this->getResource()->beginTransaction();
foreach (array_keys($data) as $entityType) {
if (in_array($entityType, $ignoreCleanUp)) {
continue;
}
$this->_getAttributeLoader($entityType)
->cleanAttributes($entityType,$data);
}
$this->getResource()->commit();
EcomDev_PHPUnit_Test_Case_Util::replaceRegistry('_singleton/eav/config', null); //clean out the EAV cache
return $this;
}
}
\ No newline at end of file
......@@ -15,6 +15,7 @@
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @author Ivan Chepurnyi <ivan.chepurnyi@ecomdev.org>
* @author Steve Rice <srice@endertech.com>
* @author Jonathan Day <jonathan@aligent.com.au>
*/
abstract class EcomDev_PHPUnit_Model_Mysql4_Fixture_Attribute_Abstract
......@@ -22,6 +23,69 @@ abstract class EcomDev_PHPUnit_Model_Mysql4_Fixture_Attribute_Abstract
{
protected $_setupModel = 'Mage_Eav_Model_Entity_Setup';
/**
* List of indexers required to build
*
* @var array
*/
protected $_requiredIndexers = array(
'catalog_product_attribute',
);
/**
* Original list of indexers required to build
*
* @var array
*/
protected $_originalIndexers = array();
/**
* Retrieve required indexers for re-building
*
* @return array
*/
public function getRequiredIndexers()
{
return $this->_requiredIndexers;
}
/**
* Run required indexers and reset to original required indexers
*
* @return EcomDev_PHPUnit_Model_Mysql4_Fixture_Eav_Abstract
*/
public function runRequiredIndexers()
{
if (empty($this->_options['doNotIndexAll'])) {
$indexer = Mage::getSingleton('index/indexer');
foreach ($this->getRequiredIndexers() as $indexerCode) {
if (empty($this->_options['doNotIndex'])
|| !in_array($indexerCode, $this->_options['doNotIndex'])) {
$indexer->getProcessByCode($indexerCode)
->reindexAll();
}
}
}
// Restoring original required indexers for making tests isolated
$this->_requiredIndexers = $this->_originalIndexers;
return $this;
}
/**
* Add indexer by specific code to required indexers list
*
* @param string $code
* @return EcomDev_PHPUnit_Model_Mysql4_Fixture_Eav_Abstract
*/
public function addRequiredIndexer($code)
{
if (!in_array($code, $this->_requiredIndexers)) {
$this->_requiredIndexers[] = $code;
}
return $this;
}
/**
* @param string $entityType
* @return array
......@@ -69,6 +133,8 @@ abstract class EcomDev_PHPUnit_Model_Mysql4_Fixture_Attribute_Abstract
/** @var $eavSetupModel Mage_Eav_Model_Entity_Setup */
$eavSetupModel->addAttribute($entityTypeModel->getEntityTypeCode(), $value['attribute_code'], $value);
}
return $this;
}
/**
......@@ -79,12 +145,22 @@ abstract class EcomDev_PHPUnit_Model_Mysql4_Fixture_Attribute_Abstract
* @return EcomDev_PHPUnit_Model_Mysql4_Fixture_Attribute_Abstract
* @throws EcomDev_PHPUnit_Model_Mysql4_Fixture_Exception
*/
public function cleanAttributes($entityType, array $attributeCodes)
public function cleanAttributes($entityType, array $attributes)
{
$eavSetup = new Mage_Eav_Model_Entity_Setup('core_setup');
try {
if(empty($attributes)) {
throw new Exception('Attribute array cannot be empty');
}
else {
$attributeCodes = array();
foreach($attributes[$entityType] as $attribute){
$attributeCodes[] = $attribute['attribute_code'];
}
}
//delete entry from eav/attribute and allow FK cascade to delete all related values
//TODO: check if the attribute != is_user_defined (ie system), then *only* delete the attribute option values, not attribute definition
$this->_getWriteAdapter()
->delete(
$this->getTable('eav/attribute'),
......@@ -101,6 +177,8 @@ abstract class EcomDev_PHPUnit_Model_Mysql4_Fixture_Attribute_Abstract
);
}
$this->resetAttributesAutoIncrement();
return $this;
}
......
......@@ -95,7 +95,11 @@ class EcomDev_PHPUnit_Model_Mysql4_Fixture_Eav_Catalog_Product extends EcomDev_P
$records = array();
$attributeCodes = $entityTypeModel->getAttributeCollection();
foreach ($row['super_attributes'] as $attributeCode) {
$attributeId = $attributeCodes->getItemByColumnValue('attribute_code',$attributeCode)->getId();
$attribute = $attributeCodes->getItemByColumnValue('attribute_code', $attributeCode);
if(!$attribute){
throw new Exception('Super attribute not found with code: ' . $attributeCode);
}
$attributeId = $attribute->getId();
$records[] = array(
'product_id' => $row[$this->_getEntityIdField($entityTypeModel)],
'attribute_id' => $attributeId
......
......@@ -1808,7 +1808,7 @@ abstract class EcomDev_PHPUnit_Test_Case_Controller extends EcomDev_PHPUnit_Test
array($this, 'deleteCookieCallback')
));
$this->replaceByMock('singleton', 'core/cookie', $cookie);
$this->replaceByMock('model', 'core/cookie', $cookie);
return $this;
}
......
......@@ -94,6 +94,7 @@
<config>ecomdev_phpunit/fixture_processor_config</config>
<config_xml>ecomdev_phpunit/fixture_processor_config</config_xml>
<cache_options>ecomdev_phpunit/fixture_processor_cache</cache_options>
<attributes>ecomdev_phpunit/fixture_processor_attributes</attributes>
<eav>ecomdev_phpunit/fixture_processor_eav</eav>
<tables>ecomdev_phpunit/fixture_processor_tables</tables>
<vfs>ecomdev_phpunit/fixture_processor_vfs</vfs>
......@@ -102,10 +103,9 @@
<!-- Fixture loaders for EAV attributes
Default can be used but does not support extra attribute table configuration -->
<default>ecomdev_phpunit/fixture_attribute_default</default>
<!--<catalog_product>ecomdev_phpunit/fixture_attribute_catalog_product</catalog_product>-->
</attribute>
<eav>
<!-- Here goes the list of fixture loaders for EAV
<!-- Here goes the list of fixture loaders for EAV entities
If no fixture loader is specified for entity, then default will be used
-->
<default>ecomdev_phpunit/fixture_eav_default</default>
......
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