9
9
*/
10
10
namespace Magento \TestFramework \Workaround \Cleanup ;
11
11
12
+ use Magento \Framework \App \CacheInterface ;
12
13
use Magento \Framework \App \Utility \Files ;
13
14
use Magento \Framework \Component \ComponentRegistrar ;
15
+ use Magento \Framework \Serialize \SerializerInterface ;
16
+ use Magento \TestFramework \Helper \Bootstrap ;
14
17
18
+ /**
19
+ * Resets static properties of classes before each test run
20
+ */
15
21
class StaticProperties
16
22
{
17
23
/**
@@ -42,6 +48,8 @@ class StaticProperties
42
48
\Magento \Framework \Phrase::class,
43
49
];
44
50
51
+ private const CACHE_NAME = 'integration_test_static_properties ' ;
52
+
45
53
/**
46
54
* Constructor
47
55
*/
@@ -67,6 +75,7 @@ public function __construct()
67
75
*
68
76
* @param \ReflectionClass $reflectionClass
69
77
* @return bool
78
+ * phpcs:disable Magento2.Functions.StaticFunction
70
79
*/
71
80
protected static function _isClassCleanable (\ReflectionClass $ reflectionClass )
72
81
{
@@ -88,6 +97,7 @@ protected static function _isClassCleanable(\ReflectionClass $reflectionClass)
88
97
*
89
98
* @param string $classFile
90
99
* @return bool
100
+ * phpcs:disable Magento2.Functions.StaticFunction
91
101
*/
92
102
protected static function _isClassInCleanableFolders ($ classFile )
93
103
{
@@ -112,8 +122,11 @@ protected static function _isClassInCleanableFolders($classFile)
112
122
protected static $ classes = [];
113
123
114
124
/**
125
+ * Create a reflection class from the provided class
126
+ *
115
127
* @param string $class
116
128
* @return \ReflectionClass
129
+ * phpcs:disable Magento2.Functions.StaticFunction
117
130
*/
118
131
private static function getReflectionClass ($ class )
119
132
{
@@ -125,7 +138,9 @@ private static function getReflectionClass($class)
125
138
126
139
/**
127
140
* Restore static variables (after running controller test case)
141
+ *
128
142
* @TODO: refactor all code where objects are stored to static variables to use object manager instead
143
+ * phpcs:ignore Magento2.Functions.StaticFunction
129
144
*/
130
145
public static function restoreStaticVariables ()
131
146
{
@@ -142,12 +157,27 @@ public static function restoreStaticVariables()
142
157
/**
143
158
* Backup static variables
144
159
*
160
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
161
+ * phpcs:disable Magento2.Functions.StaticFunction
145
162
*/
146
163
public static function backupStaticVariables ()
147
164
{
148
165
if (count (self ::$ backupStaticVariables ) > 0 ) {
149
166
return ;
150
167
}
168
+
169
+ $ objectManager = Bootstrap::getInstance ()->getObjectManager ();
170
+ $ cache = $ objectManager ->get (CacheInterface::class);
171
+ $ serializer = $ objectManager ->get (SerializerInterface::class);
172
+ $ cachedProperties = $ cache ->load (self ::CACHE_NAME );
173
+
174
+ if ($ cachedProperties ) {
175
+ self ::$ backupStaticVariables = $ serializer ->unserialize ($ cachedProperties );
176
+ return ;
177
+ }
178
+
179
+ unset($ cachedProperties , $ objectManager );
180
+
151
181
$ classFiles = array_filter (
152
182
Files::init ()->getPhpFiles (
153
183
Files::INCLUDE_APP_CODE
@@ -156,12 +186,14 @@ public static function backupStaticVariables()
156
186
),
157
187
function ($ classFile ) {
158
188
return StaticProperties::_isClassInCleanableFolders ($ classFile )
189
+ // phpcs:ignore Magento2.Functions.DiscouragedFunction
159
190
&& strpos (file_get_contents ($ classFile ), ' static ' ) > 0 ;
160
191
}
161
192
);
162
193
$ namespacePattern = '/namespace [a-zA-Z0-9 \\\\]+;/ ' ;
163
194
$ classPattern = '/\nclass [a-zA-Z0-9_]+/ ' ;
164
195
foreach ($ classFiles as $ classFile ) {
196
+ // phpcs:ignore Magento2.Functions.DiscouragedFunction
165
197
$ code = file_get_contents ($ classFile );
166
198
preg_match ($ namespacePattern , $ code , $ namespace );
167
199
preg_match ($ classPattern , $ code , $ class );
@@ -187,17 +219,16 @@ function ($classFile) {
187
219
}
188
220
}
189
221
}
222
+
223
+ $ cache ->save ($ serializer ->serialize (self ::$ backupStaticVariables ), self ::CACHE_NAME );
190
224
}
191
225
192
226
/**
193
227
* Handler for 'startTestSuite' event
194
- *
195
228
*/
196
229
public function startTestSuite ()
197
230
{
198
- if (empty (self ::$ backupStaticVariables )) {
199
- self ::backupStaticVariables ();
200
- }
231
+ self ::backupStaticVariables ();
201
232
}
202
233
203
234
/**
0 commit comments