Skip to content
This repository was archived by the owner on May 27, 2023. It is now read-only.

Commit 460f4a4

Browse files
committed
Improvements in the fixtures models
1 parent decb1d4 commit 460f4a4

5 files changed

Lines changed: 226 additions & 9 deletions

File tree

app/code/community/EcomDev/PHPUnit/Model/Fixture/Processor/Eav.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,15 @@ public function apply(array $data, $key, EcomDev_PHPUnit_Model_FixtureInterface
8989
$this->getResource()->beginTransaction();
9090

9191
foreach ($data as $entityType => $values) {
92-
$eavLoaders[] = $this->_getEavLoader($entityType)
92+
$eavLoaders[$entityType] = $this->_getEavLoader($entityType)
9393
->setFixture($fixture)
94-
->setOptions($fixture->getOptions())
95-
->loadEntity($entityType, $values);
94+
->setOptions($fixture->getOptions());
95+
96+
if ($eavLoaders[$entityType] instanceof EcomDev_PHPUnit_Model_Mysql4_Fixture_RestoreAwareInterface) {
97+
$eavLoaders[$entityType]->saveData($entityType);
98+
}
99+
100+
$eavLoaders[$entityType]->loadEntity($entityType, $values);
96101
}
97102

98103
$this->getResource()->commit();
@@ -126,16 +131,35 @@ public function discard(array $data, $key, EcomDev_PHPUnit_Model_FixtureInterfac
126131
EcomDev_PHPUnit_Model_FixtureInterface::SCOPE_SHARED);
127132
}
128133

134+
$typesToRestore = array();
129135
$this->getResource()->beginTransaction();
130136
foreach (array_keys($data) as $entityType) {
137+
$eavLoader = $this->_getEavLoader($entityType);
138+
131139
if (in_array($entityType, $ignoreCleanUp)) {
140+
if ($eavLoader instanceof EcomDev_PHPUnit_Model_Mysql4_Fixture_RestoreAwareInterface) {
141+
$eavLoader->clearData($entityType);
142+
}
132143
continue;
133144
}
134-
$this->_getEavLoader($entityType)
135-
->cleanEntity($entityType);
136-
}
145+
146+
$eavLoader->cleanEntity($entityType);
137147

148+
if ($eavLoader instanceof EcomDev_PHPUnit_Model_Mysql4_Fixture_RestoreAwareInterface) {
149+
$typesToRestore[$entityType] = $eavLoader;
150+
}
151+
}
138152
$this->getResource()->commit();
153+
154+
if ($typesToRestore) {
155+
$this->getResource()->beginTransaction();
156+
foreach ($typesToRestore as $entityType => $eavLoader) {
157+
$eavLoader->restoreData($entityType)
158+
->clearData($entityType);
159+
}
160+
$this->getResource()->commit();
161+
}
162+
139163
return $this;
140164
}
141165
}

app/code/community/EcomDev/PHPUnit/Model/Mysql4/Fixture/AbstractEav.php

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
*/
2323
abstract class EcomDev_PHPUnit_Model_Mysql4_Fixture_AbstractEav
2424
extends EcomDev_PHPUnit_Model_Mysql4_Fixture_AbstractComplex
25+
implements EcomDev_PHPUnit_Model_Mysql4_Fixture_RestoreAwareInterface
2526
{
27+
const RESTORE_KEY = 'restore_%s_data';
28+
2629
/**
2730
* List of indexers required to build
2831
*
@@ -37,6 +40,20 @@ abstract class EcomDev_PHPUnit_Model_Mysql4_Fixture_AbstractEav
3740
*/
3841
protected $_originalIndexers = array();
3942

43+
/**
44+
* List of tables that should be restored after run
45+
*
46+
* @var string[]
47+
*/
48+
protected $_restoreTables = array();
49+
50+
/**
51+
* Default data for eav entity
52+
*
53+
* @var array
54+
*/
55+
protected $_defaultData = array();
56+
4057
/**
4158
* Retrieve required indexers for re-building
4259
*
@@ -99,6 +116,69 @@ public function cleanEntity($entityType)
99116
return $this;
100117
}
101118

119+
120+
/**
121+
* Saves data for restoring it after fixture has been cleaned up
122+
*
123+
* @param string $code storage code
124+
* @return $this
125+
*/
126+
public function saveData($code)
127+
{
128+
if ($this->_restoreTables) {
129+
$storageKey = sprintf(self::RESTORE_KEY, $code);
130+
$data = array();
131+
foreach ($this->_restoreTables as $table) {
132+
$select = $this->_getReadAdapter()->select();
133+
$select->from($table);
134+
$data[$table] = $this->_getReadAdapter()->fetchAll($select);
135+
}
136+
$this->_fixture->setStorageData($storageKey, $data);
137+
}
138+
139+
return $this;
140+
}
141+
142+
/**
143+
* Restored saved data
144+
*
145+
* @param string $code storage code
146+
* @return $this
147+
*/
148+
public function restoreData($code)
149+
{
150+
if ($this->_restoreTables) {
151+
$storageKey = sprintf(self::RESTORE_KEY, $code);
152+
$data = $this->_fixture->getStorageData($storageKey);
153+
foreach ($this->_restoreTables as $table) {
154+
if (!empty($data[$table])) {
155+
$this->_getWriteAdapter()->insertOnDuplicate(
156+
$table,
157+
$data[$table]
158+
);
159+
}
160+
}
161+
}
162+
163+
return $this;
164+
}
165+
166+
/**
167+
* Clears storage from stored backup data
168+
*
169+
* @param $code
170+
* @return $this
171+
*/
172+
public function clearData($code)
173+
{
174+
if ($this->_restoreTables) {
175+
$storageKey = sprintf(self::RESTORE_KEY, $code);
176+
$this->_fixture->setStorageData($storageKey, array());
177+
}
178+
179+
return $this;
180+
}
181+
102182
/**
103183
* Loads EAV data into DB tables
104184
*
@@ -139,16 +219,43 @@ public function loadEntity($entityType, $values)
139219
// and rows list as value
140220
// See getCustomTableRecords
141221
$customValues = array();
222+
223+
if ($this->_defaultData) {
224+
$dataToInsert = $this->_defaultData;
225+
// Prevent insertion of default data,
226+
// if there is already data available
227+
foreach ($values as $index => $row) {
228+
if (isset($row[$this->_getEntityIdField($entityTypeModel)])
229+
&& isset($dataToInsert[$this->_getEntityIdField($entityTypeModel)])) {
230+
$dataToInsert = array();
231+
break;
232+
}
233+
}
234+
235+
foreach ($dataToInsert as $row) {
236+
array_unshift($values, $row);
237+
}
238+
}
239+
142240

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

148246
// Fulfill necessary information
149-
$row['entity_type_id'] = $entityTypeModel->getEntityTypeId();
247+
$values[$index]['entity_type_id'] = $entityTypeModel->getEntityTypeId();
248+
$row = $values[$index];
249+
150250
if (!isset($row['attribute_set_id'])) {
151-
$row['attribute_set_id'] = $entityTypeModel->getDefaultAttributeSetId();
251+
$defaultAttributeSet = $entityTypeModel->getDefaultAttributeSetId();
252+
253+
// Fix Magento core issue with attribute set information for customer and its address
254+
if (in_array($entityType, array('customer', 'customer_address'))) {
255+
$defaultAttributeSet = 0;
256+
}
257+
258+
$values[$index]['attribute_set_id'] = $defaultAttributeSet;
152259
}
153260

154261
// Preparing entity table record

app/code/community/EcomDev/PHPUnit/Model/Mysql4/Fixture/Eav/Catalog/Category.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,36 @@
2323
*/
2424
class EcomDev_PHPUnit_Model_Mysql4_Fixture_Eav_Catalog_Category extends EcomDev_PHPUnit_Model_Mysql4_Fixture_Eav_Catalog_Abstract
2525
{
26+
const XML_PATH_DEFAULT_DATA = 'phpunit/suite/fixture/default_data/category';
27+
2628
protected $_requiredIndexers = array(
2729
'catalog_category_flat'
2830
);
31+
32+
protected function _construct()
33+
{
34+
parent::_construct();
35+
$defaultData = Mage::getConfig()->getNode(self::XML_PATH_DEFAULT_DATA);
36+
37+
if ($defaultData) {
38+
foreach ($defaultData->children() as $item) {
39+
if (!isset($item->entity_id)) {
40+
continue;
41+
}
42+
43+
$entityId = (string)$item->entity_id;
44+
$this->_defaultData[$entityId] = array();
45+
foreach ($item->children() as $value) {
46+
$this->_defaultData[$entityId][$value->getName()] = (string)$value;
47+
}
48+
}
49+
}
50+
51+
$this->_restoreTables[] = $this->getTable('catalog/category');
52+
foreach (array('datetime', 'decimal', 'int', 'text', 'varchar') as $suffix) {
53+
$this->_restoreTables[] = $this->getTable(array('catalog/category', $suffix));
54+
}
55+
}
2956

3057
/**
3158
* Overridden to add easy fixture loading for product associations
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
interface EcomDev_PHPUnit_Model_Mysql4_Fixture_RestoreAwareInterface
4+
{
5+
/**
6+
* Saves data for restoring it after fixture has been cleaned up
7+
*
8+
* @param string $code storage code
9+
* @return $this
10+
*/
11+
public function saveData($code);
12+
13+
/**
14+
* Restored saved data
15+
*
16+
* @param string $code storage code
17+
* @return $this
18+
*/
19+
public function restoreData($code);
20+
21+
/**
22+
* Clears storage from stored backup data
23+
*
24+
* @param $code
25+
* @return $this
26+
*/
27+
public function clearData($code);
28+
}

app/code/community/EcomDev/PHPUnit/etc/config.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,37 @@
114114
<catalog_product>ecomdev_phpunit/fixture_eav_catalog_product</catalog_product>
115115
<catalog_category>ecomdev_phpunit/fixture_eav_catalog_category</catalog_category>
116116
</eav>
117+
<default_data>
118+
<category>
119+
<root>
120+
<entity_id>1</entity_id>
121+
<parent_id>0</parent_id>
122+
<path>1</path>
123+
<position>0</position>
124+
<level>0</level>
125+
<children_count>1</children_count>
126+
<name>Root Catalog</name>
127+
<url_key>root-catalog</url_key>
128+
<is_active>1</is_active>
129+
<is_anchor>0</is_anchor>
130+
<attribute_set_id>0</attribute_set_id>
131+
</root>
132+
<default_category>
133+
<entity_id>2</entity_id>
134+
<parent_id>1</parent_id>
135+
<path>1/2</path>
136+
<position>1</position>
137+
<level>1</level>
138+
<children_count>0</children_count>
139+
<name>Default Category</name>
140+
<url_key>default-category</url_key>
141+
<is_active>1</is_active>
142+
<is_anchor>0</is_anchor>
143+
<display_mode>PRODUCTS</display_mode>
144+
<include_in_menu>1</include_in_menu>
145+
</default_category>
146+
</category>
147+
</default_data>
117148
</fixture>
118149
<app>
119150
<!-- Application class name for running tests -->

0 commit comments

Comments
 (0)