This repository has been archived by the owner on May 27, 2023. It is now read-only.
/
Eav.php
165 lines (138 loc) · 5.4 KB
/
Eav.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
<?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>
*/
class EcomDev_PHPUnit_Model_Fixture_Processor_Eav
extends Mage_Core_Model_Abstract
implements EcomDev_PHPUnit_Model_Fixture_ProcessorInterface
{
const STORAGE_KEY = 'entities';
// Configuration path for eav loaders
const XML_PATH_FIXTURE_EAV_LOADERS = 'phpunit/suite/fixture/eav';
// Default eav loader class node in loaders configuration
const DEFAULT_EAV_LOADER_NODE = 'default';
// Default eav loader class alias
const DEFAULT_EAV_LOADER_CLASS = 'ecomdev_phpunit/fixture_eav_default';
/**
* Initialize fixture resource model
*/
protected function _construct()
{
$this->_init('ecomdev_phpunit/fixture');
}
/**
* Does nothing
*
* @param EcomDev_PHPUnit_Model_FixtureInterface $fixture
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Eav
*/
public function initialize(EcomDev_PHPUnit_Model_FixtureInterface $fixture)
{
return $this;
}
/**
* Retrieves eav loader for a particular entity type
*
* @param string $entityType
* @return EcomDev_PHPUnit_Model_Mysql4_Fixture_AbstractEav
*/
protected function _getEavLoader($entityType)
{
$loaders = Mage::getConfig()->getNode(self::XML_PATH_FIXTURE_EAV_LOADERS);
if (isset($loaders->$entityType)) {
$classAlias = (string)$loaders->$entityType;
} elseif (isset($loaders->{self::DEFAULT_EAV_LOADER_NODE})) {
$classAlias = (string)$loaders->{self::DEFAULT_EAV_LOADER_NODE};
} else {
$classAlias = self::DEFAULT_EAV_LOADER_CLASS;
}
return Mage::getResourceSingleton($classAlias);
}
/**
* Apply eav records from fixture file
*
* @param array $data
* @param string $key
* @param EcomDev_PHPUnit_Model_FixtureInterface $fixture
*
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Eav
*/
public function apply(array $data, $key, EcomDev_PHPUnit_Model_FixtureInterface $fixture)
{
$eavLoaders = array();
$this->getResource()->beginTransaction();
foreach ($data as $entityType => $values) {
$eavLoaders[$entityType] = $this->_getEavLoader($entityType)
->setFixture($fixture)
->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();
foreach ($eavLoaders as $eavLoader){
$eavLoader->runRequiredIndexers();
}
$fixture->setStorageData(self::STORAGE_KEY, array_keys($data));
return $this;
}
/**
* Discard applied eav records
*
* @param array[] $data
* @param string $key
* @param EcomDev_PHPUnit_Model_FixtureInterface $fixture
*
* @return EcomDev_PHPUnit_Model_Fixture_Processor_Eav
*/
public function discard(array $data, $key, EcomDev_PHPUnit_Model_FixtureInterface $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_FixtureInterface::SCOPE_SHARED)) {
$ignoreCleanUp = $fixture->getStorageData(self::STORAGE_KEY,
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;
}
$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;
}
}