This repository has been archived by the owner on May 27, 2023. It is now read-only.
/
AbstractEav.php
497 lines (433 loc) · 15.1 KB
/
AbstractEav.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<?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>
*/
/**
* Base implementation of EAV fixtures loader
*
*/
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
*
* @var array
*/
protected $_requiredIndexers = array();
/**
* Original list of indexers required to build
*
* @var array
*/
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
*
* @return array
*/
public function getRequiredIndexers()
{
return $this->_requiredIndexers;
}
/**
* Run required indexers and reset to original required indexers
*
* @return EcomDev_PHPUnit_Model_Mysql4_Fixture_AbstractEav
*/
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'])) {
$process = $indexer->getProcessByCode($indexerCode);
if ($process) {
$process->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_AbstractEav
*/
public function addRequiredIndexer($code)
{
if (!in_array($code, $this->_requiredIndexers)) {
$this->_requiredIndexers[] = $code;
}
return $this;
}
/**
* Clean entity data table
*
* @param string $entityType
* @return EcomDev_PHPUnit_Model_Mysql4_Fixture_AbstractEav
*/
public function cleanEntity($entityType)
{
/** @var $entityTypeModel Mage_Eav_Model_Entity_Type */
$entityTypeModel = Mage::getSingleton('eav/config')->getEntityType($entityType);
$this->cleanTable($entityTypeModel->getEntityTable());
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
*
* @param string $entityType
* @param array $values
* @return EcomDev_PHPUnit_Model_Mysql4_Fixture_AbstractEav
* @throws RuntimeException
*/
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)
&& $match[1] == $entityType) {
$this->_requiredIndexers[] = $match[2];
}
}
}
/** @var $entityTypeModel Mage_Eav_Model_Entity_Type */
$entityTypeModel = Mage::getSingleton('eav/config')->getEntityType($entityType);
$entityTableColumns = $this->_getWriteAdapter()->describeTable(
$this->getTable($entityTypeModel->getEntityTable())
);
$attributeTableColumns = $this->_getAttributeTablesColumnList($entityTypeModel);
$entities = array();
$entityValues = array();
// Custom values array is used for
// inserting custom entity data in custom tables.
// It is an associative array with table name as key,
// 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) {
if (!isset($row[$this->_getEntityIdField($entityTypeModel)])) {
throw new RuntimeException('Entity Id should be specified in EAV fixture');
}
// Fulfill necessary information
$values[$index]['entity_type_id'] = $entityTypeModel->getEntityTypeId();
$row = $values[$index];
if (!isset($row['attribute_set_id'])) {
$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
$entity = $this->_getTableRecord($row, $entityTableColumns);
$entities[] = $entity;
// Preparing simple attributes records
foreach ($entityTypeModel->getAttributeCollection() as $attribute) {
/** @var $attribute Mage_Eav_Model_Entity_Attribute */
$attributeBackendTable = $attribute->getBackendTable();
if (!$attribute->isStatic()
&& $attributeBackendTable
&& isset($attributeTableColumns[$attributeBackendTable])) {
// Preparing data for insert per attribute table
$attributeRecords = $this->_getAttributeRecords(
$row,
$attribute,
$attributeTableColumns[$attributeBackendTable]
);
if ($attributeRecords) {
if (!isset($entityValues[$attributeBackendTable])) {
$entityValues[$attributeBackendTable] = array();
}
$entityValues[$attributeBackendTable] = array_merge(
$entityValues[$attributeBackendTable],
$attributeRecords
);
}
}
}
// Processing custom entity values
$customValues = array_merge_recursive(
$customValues,
$this->_getCustomTableRecords($row, $entityTypeModel)
);
}
$this->_getWriteAdapter()->insertOnDuplicate(
$this->getTable($entityTypeModel->getEntityTable()),
$entities
);
foreach ($entityValues as $tableName => $records) {
$this->_getWriteAdapter()->insertOnDuplicate(
$tableName,
$records
);
}
foreach ($customValues as $tableName => $records) {
$this->_getWriteAdapter()->insertOnDuplicate(
(strpos($tableName, '/') !== false ? $this->getTable($tableName) : $tableName),
$records
);
}
foreach ($entities as $entity) {
$this->_customEntityAction($entity, $entityTypeModel);
}
return $this;
}
/**
* Performs custom action on entity
*
* @param array $entity
* @param Mage_Eav_Model_Entity_Type $entityTypeModel
* @return EcomDev_PHPUnit_Model_Mysql4_Fixture_AbstractEav
*/
protected function _customEntityAction($entity, $entityTypeModel)
{
return $this;
}
/**
* If you have some custom EAV tables,
* this method will help you to insert
* them on fixture processing step
* It should return an associative array, where an entry key
* is the table name and its value is a list of table rows
*
* @example
* return array(
* 'some/table' => array(
* array(
* 'field' => 'value'
* )
* )
* )
*
* @param array $row
* @param Mage_Eav_Model_Entity_Type $entityTypeModel
* @return array
*/
protected function _getCustomTableRecords($row, $entityTypeModel)
{
return array();
}
/**
* Retrieves associative list of attribute tables and their columns
*
* @param Mage_Eav_Model_Entity_Type $entityTypeModel
* @return array
*/
protected function _getAttributeTablesColumnList($entityTypeModel)
{
$tableNames = array_unique(
$entityTypeModel->getAttributeCollection()
->walk('getBackendTable')
);
$columnsByTable = array();
foreach ($tableNames as $table) {
if ($table) {
$columnsByTable[$table] = $this->_getWriteAdapter()
->describeTable(
$table
);
}
}
return $columnsByTable;
}
/**
* Retrieves attribute records for single entity
*
* @param array $row
* @param array $attribute
* @param $tableColumns
* @return array
* @internal param \Mage_Eav_Model_Entity_Type $entityTypeModel
*/
protected function _getAttributeRecords($row, $attribute, $tableColumns)
{
$records = array();
$value = $this->_getAttributeValue($row, $attribute);
if ($value !== null) {
$valueInfo = $this->_getAttributeValueInfo($row, $attribute);
$valueInfo['value'] = $value;
$records[] = $this->_getTableRecord($valueInfo, $tableColumns);
}
return $records;
}
/**
* Returns attribute meta info for record,
* e.g. entity_type_id, attribute_id, etc
*
* @param array $row
* @param Mage_Eav_Model_Entity_Attribute $attribute
* @return array
*/
protected function _getAttributeValueInfo($row, $attribute)
{
return array(
'attribute_id' => $attribute->getId(),
'entity_type_id' => $attribute->getEntityTypeId(),
$this->_getEntityIdField($attribute) => $row[$this->_getEntityIdField($attribute)]
);
}
/**
* Retrieves attribute value
*
* @param array $row
* @param Mage_Eav_Model_Entity_Attribute $attribute
* @return mixed|null
*/
protected function _getAttributeValue($row, $attribute)
{
if (isset($row[$attribute->getAttributeCode()]) && !is_array($row[$attribute->getAttributeCode()])) {
$value = $row[$attribute->getAttributeCode()];
} elseif ($attribute->getIsRequired()
&& $attribute->getDefaultValue() !== null
&& $attribute->getDefaultValue() !== ''
&& !is_array($attribute->getDefaultValue())) {
$value = $attribute->getDefaultValue();
} else {
$value = null;
}
if ($attribute->usesSource() && $value !== null) {
if ($attribute->getSource() instanceof Mage_Eav_Model_Entity_Attribute_Source_Abstract) {
$value = $attribute->getSource()->getOptionId($value);
} else {
$value = $this->_getOptionIdNonAttributeSource($attribute->getSource()->getAllOptions(), $value);
}
}
return $value;
}
/**
* Get option id if attribute source model does not support eav attribute interface
*
*
* @param array $options
* @param mixed $value
* @return null|string
*/
protected function _getOptionIdNonAttributeSource($options, $value)
{
foreach ($options as $option) {
if (strcasecmp($option['label'], $value)==0 || $option['value'] == $value) {
return $option['value'];
}
}
return null;
}
/**
* Retrieves entity id field, based on entity configuration
*
* @param Mage_Eav_Model_Entity_Type|Mage_Eav_Model_Entity_Attribute $entityTypeModel
* @return string
*/
protected function _getEntityIdField($entityTypeModel)
{
if ($entityTypeModel->getEntityIdField()) {
return $entityTypeModel->getEntityIdField();
}
return Mage_Eav_Model_Entity::DEFAULT_ENTITY_ID_FIELD;
}
}