Completed
Pull Request — develop (#4)
by Carlo
03:23
created

Registry::afterSuiteFixturesCleanup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Magefix\Fixtures;
4
5
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
6
use Mage_Core_Model_Abstract;
7
use Magefix\Magento\Store\Scope as MagentoStoreScope;
8
use Magefix\Fixture\Factory\Builder as FixtureBuilder;
9
use Magefix\Exceptions\UnavailableHook;
10
use Magefix\Exceptions\NullFixtureId;
11
12
/**
13
 * Class Registry
14
 *
15
 * @package Magefix\Fixtures
16
 * @author  Carlo Tasca <[email protected]>
17
 */
18
trait Registry
19
{
20
    /**
21
     * Registry collection
22
     *
23
     * @var array
24
     */
25
    static private $_registry = [];
26
27
    /**
28
     * @var array
29
     */
30
    private $_contexts = [];
31
32
    /**
33
     * Register a new variable
34
     *
35
     * @param string $key
36
     * @param mixed  $value
37
     *
38
     * @throws Mage_Core_Exception
39
     */
40
    public static function register($key, $value)
41
    {
42
        self::$_registry[$key] = $value;
43
    }
44
45
    /**
46
     * Retrieve a value from registry by a key
47
     *
48
     * @param string $key
49
     *
50
     * @return mixed
51
     */
52
    public static function registry($key)
53
    {
54
        if (isset(self::$_registry[$key])) {
55
            return self::$_registry[$key];
56
        }
57
58
        return null;
59
    }
60
61
    /**
62
     * Unregister a variable from register by key
63
     *
64
     * @param string $key
65
     */
66
    public static function unregister($key)
67
    {
68
        if (isset(self::$_registry[$key])) {
69
            if (is_object(self::$_registry[$key]) && (method_exists(self::$_registry[$key], '__destruct'))) {
70
                self::$_registry[$key]->__destruct();
71
            }
72
            unset(self::$_registry[$key]);
73
        }
74
    }
75
76
    /**
77
     * @return array
78
     */
79
    public static function getRegistry()
80
    {
81
        return self::$_registry;
82
    }
83
84
    /**
85
     * Reset static registry
86
     */
87
    public static function reset()
88
    {
89
        self::$_registry = [];
90
    }
91
92
    /**
93
     * Register fixture reference
94
     *
95
     * @param $type
96
     * @param $fixtureId
97
     * @param $hook
98
     *
99
     * @throws UnavailableHook
100
     */
101
    public static function registerFixture($type, $fixtureId, $hook)
102
    {
103
        if ($hook) {
104
            $lowercaseHook = strtolower($hook);
105
            self::_registerFixture($type, $fixtureId, trim($lowercaseHook, '@'));
106
        }
107
    }
108
109
    /**
110
     * @AfterSuite
111
     */
112
    public static function afterSuiteFixturesCleanup()
113
    {
114
        self::_cleanupFixtureByHook('aftersuite');
115
    }
116
117
    /**
118
     * @AfterFeature
119
     */
120
    public static function afterFeatureFixturesCleanup()
121
    {
122
        self::_cleanupFixtureByHook('afterfeature');
123
    }
124
125
    /**
126
     * @AfterScenario
127
     */
128
    public function afterScenarioFixturesCleanup()
129
    {
130
        self::_cleanupFixtureByHook('afterscenario');
131
    }
132
133
    /**
134
     * @AfterStep
135
     */
136
    public function afterStepFixturesCleanup()
137
    {
138
        self::_cleanupFixtureByHook('afterstep');
139
    }
140
141
    /**
142
     * Register feature fixture reference
143
     *
144
     * @param $type
145
     * @param $fixtureId
146
     * @param $hook
147
     *
148
     * @throws NullFixtureId
149
     */
150
    protected static function _registerFixture($type, $fixtureId, $hook)
151
    {
152
        self::_throwNullFixtureIdException($fixtureId);
153
        self::register($type . "_{$hook}_" . $fixtureId, $fixtureId);
154
    }
155
156
    /**
157
     * @param string feature|scenario|step $hook
158
     */
159
    protected static function _cleanupFixtureByHook($hook)
160
    {
161
        $registry = self::_getBuilderRegistry();
162
163
        MagentoStoreScope::setAdminStoreScope();
164
        self::_iterateRegistry($registry, $hook);
165
        MagentoStoreScope::setCurrentStoreScope();
166
    }
167
168
    /**
169
     * @param $registry
170
     * @param $hook
171
     *
172
     * @throws \Exception
173
     */
174
    protected static function _iterateRegistry($registry, $hook)
175
    {
176
        $registryIterator         = new RegistryIterator($registry);
177
        $registryIteratorIterator = $registryIterator->getIterator();
178
179
        while ($registryIteratorIterator->valid()) {
180
            $key        = $registryIteratorIterator->key();
181
            $entry      = $registryIteratorIterator->current();
182
            $entryMatch = $registryIterator->isEntryMatch($hook, $key);
183
184
            if (!empty($entryMatch) && isset($entryMatch[1])) {
185
                self::_echoRegistryChangeMessage(
186
                    $registryIterator->getMageModelForMatch($entryMatch[1]), $entryMatch[1], $entry, $key
187
                );
188
            }
189
190
            $registryIteratorIterator->next();
191
        }
192
    }
193
194
195
    /**
196
     * @param Mage_Core_Model_Abstract $model
197
     * @param                          $fixtureType
198
     * @param                          $entry
199
     * @param                          $key
200
     */
201
    protected static function _echoRegistryChangeMessage(Mage_Core_Model_Abstract $model, $fixtureType, $entry, $key)
202
    {
203
        echo self::_deleteAndUnregisterFixture(
204
            $model, $fixtureType, $entry, $key
205
        );
206
    }
207
208
    /**
209
     * @return array
210
     */
211
    protected static function _getBuilderRegistry()
212
    {
213
        return FixtureBuilder::getRegistry();
214
    }
215
216
    /**
217
     * @param Mage_Core_Model_Abstract $model
218
     * @param                          $fixtureType
219
     * @param                          $entry
220
     * @param                          $key
221
     *
222
     * @return string
223
     * @throws \Exception
224
     */
225
    protected static function _deleteAndUnregisterFixture(Mage_Core_Model_Abstract $model, $fixtureType, $entry, $key)
226
    {
227
        $fixture = $model->load((int)$entry);
228
        $fixture->delete();
229
        FixtureBuilder::unregister($key);
230
231
        return self::_deleteFixtureMessage($fixtureType, $entry);
232
    }
233
234
    /**
235
     * @param string $fixtureType
236
     * @param string $entry
237
     *
238
     * @return string
239
     */
240
    protected static function _deleteFixtureMessage($fixtureType, $entry)
241
    {
242
        return "-- DELETED {$fixtureType} fixture with ID {$entry}\n";
243
    }
244
245
    /**
246
     * @param $fixtureId
247
     *
248
     * @throws NullFixtureId
249
     */
250
    protected static function _throwNullFixtureIdException($fixtureId)
251
    {
252
        if (!$fixtureId) {
253
            throw new NullFixtureId('Could not register fixture. Fixture id is null.');
254
        }
255
    }
256
257
    /**
258
     * Get environment context classes
259
     *
260
     * @BeforeScenario
261
     *
262
     * @param BeforeScenarioScope $scope
263
     */
264
    public function getContexts(BeforeScenarioScope $scope)
265
    {
266
        $environment = $scope->getEnvironment();
267
268
        foreach ($environment->getContextClasses() as $class) {
269
            $name                   = strtolower(preg_replace('~.*\\\\(\w+)Context~', '\1', $class));
270
            $this->_contexts[$name] = $environment->getContext($class);
271
        }
272
    }
273
}
274