Failed Conditions
Pull Request — develop (#4)
by Carlo
03:06
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 Magefix\Magento\Store\Scope as MagentoStoreScope;
7
use Magefix\Fixture\Factory\Builder as FixtureBuilder;
8
use Magefix\Exceptions\UnavailableHook;
9
use Magefix\Exceptions\NullFixtureId;
10
11
/**
12
 * Class Registry
13
 *
14
 * @package Magefix\Fixtures
15
 * @author  Carlo Tasca <[email protected]>
16
 */
17
trait Registry
18
{
19
    /**
20
     * Registry collection
21
     *
22
     * @var array
23
     */
24
    static private $_registry = [];
25
26
    /**
27
     * @var array
28
     */
29
    private $_contexts = [];
30
31
    /**
32
     * Register a new variable
33
     *
34
     * @param string $key
35
     * @param mixed  $value
36
     *
37
     * @throws Mage_Core_Exception
38
     */
39
    public static function register($key, $value)
40
    {
41
        self::$_registry[$key] = $value;
42
    }
43
44
    /**
45
     * Retrieve a value from registry by a key
46
     *
47
     * @param string $key
48
     *
49
     * @return mixed
50
     */
51
    public static function registry($key)
52
    {
53
        if (isset(self::$_registry[$key])) {
54
            return self::$_registry[$key];
55
        }
56
57
        return null;
58
    }
59
60
    /**
61
     * Unregister a variable from register by key
62
     *
63
     * @param string $key
64
     */
65
    public static function unregister($key)
66
    {
67
        if (isset(self::$_registry[$key])) {
68
            if (is_object(self::$_registry[$key]) && (method_exists(self::$_registry[$key], '__destruct'))) {
69
                self::$_registry[$key]->__destruct();
70
            }
71
            unset(self::$_registry[$key]);
72
        }
73
    }
74
75
    /**
76
     * @return array
77
     */
78
    public static function getRegistry()
79
    {
80
        return self::$_registry;
81
    }
82
83
    /**
84
     * Reset static registry
85
     */
86
    public static function reset()
87
    {
88
        self::$_registry = [];
89
    }
90
91
    /**
92
     * Register fixture reference
93
     *
94
     * @param $type
95
     * @param $fixtureId
96
     * @param $hook
97
     *
98
     * @throws UnavailableHook
99
     */
100
    public static function registerFixture($type, $fixtureId, $hook)
101
    {
102
        if ($hook) {
103
            $lowercaseHook = strtolower($hook);
104
            self::_registerFixture($type, $fixtureId, trim($lowercaseHook, '@'));
105
        }
106
    }
107
108
    /**
109
     * @AfterSuite
110
     */
111
    public static function afterSuiteFixturesCleanup()
112
    {
113
        self::_cleanupFixtureByHook('aftersuite');
114
    }
115
116
    /**
117
     * @AfterFeature
118
     */
119
    public static function afterFeatureFixturesCleanup()
120
    {
121
        self::_cleanupFixtureByHook('afterfeature');
122
    }
123
124
    /**
125
     * @AfterScenario
126
     */
127
    public function afterScenarioFixturesCleanup()
128
    {
129
        self::_cleanupFixtureByHook('afterscenario');
130
    }
131
132
    /**
133
     * @AfterStep
134
     */
135
    public function afterStepFixturesCleanup()
136
    {
137
        self::_cleanupFixtureByHook('afterstep');
138
    }
139
140
    /**
141
     * Register feature fixture reference
142
     *
143
     * @param $type
144
     * @param $fixtureId
145
     * @param $hook
146
     *
147
     * @throws NullFixtureId
148
     */
149
    protected static function _registerFixture($type, $fixtureId, $hook)
150
    {
151
        self::_throwNullFixtureIdException($fixtureId);
152
        self::register($type . "_{$hook}_" . $fixtureId, $fixtureId);
153
    }
154
155
    /**
156
     * @param string feature|scenario|step $hook
157
     */
158
    protected static function _cleanupFixtureByHook($hook)
159
    {
160
        $registry         = self::_getBuilderRegistry();
161
        $registryIterator = new RegistryIterator($registry);
162
        MagentoStoreScope::setAdminStoreScope();
163
        $registryIterator->iterateByHook($hook);
164
        MagentoStoreScope::setCurrentStoreScope();
165
    }
166
167
    /**
168
     * @return array
169
     */
170
    protected static function _getBuilderRegistry()
171
    {
172
        return FixtureBuilder::getRegistry();
173
    }
174
175
    /**
176
     * @param $fixtureId
177
     *
178
     * @throws NullFixtureId
179
     */
180
    protected static function _throwNullFixtureIdException($fixtureId)
181
    {
182
        if (!$fixtureId) {
183
            throw new NullFixtureId('Could not register fixture. Fixture id is null.');
184
        }
185
    }
186
187
    /**
188
     * Get environment context classes
189
     *
190
     * @BeforeScenario
191
     *
192
     * @param BeforeScenarioScope $scope
193
     */
194
    public function getContexts(BeforeScenarioScope $scope)
195
    {
196
        $environment = $scope->getEnvironment();
197
198
        foreach ($environment->getContextClasses() as $class) {
199
            $name                   = strtolower(preg_replace('~.*\\\\(\w+)Context~', '\1', $class));
200
            $this->_contexts[$name] = $environment->getContext($class);
201
        }
202
    }
203
}
204