99 */
1010namespace Magento \TestFramework \Workaround \Cleanup ;
1111
12+ use Magento \Framework \App \CacheInterface ;
1213use Magento \Framework \App \Utility \Files ;
1314use Magento \Framework \Component \ComponentRegistrar ;
15+ use Magento \Framework \Serialize \SerializerInterface ;
16+ use Magento \TestFramework \Helper \Bootstrap ;
1417
18+ /**
19+ * Resets static properties of classes before each test run
20+ */
1521class StaticProperties
1622{
1723 /**
@@ -42,6 +48,8 @@ class StaticProperties
4248 \Magento \Framework \Phrase::class,
4349 ];
4450
51+ private const CACHE_NAME = 'integration_test_static_properties ' ;
52+
4553 /**
4654 * Constructor
4755 */
@@ -67,6 +75,7 @@ public function __construct()
6775 *
6876 * @param \ReflectionClass $reflectionClass
6977 * @return bool
78+ * phpcs:disable Magento2.Functions.StaticFunction
7079 */
7180 protected static function _isClassCleanable (\ReflectionClass $ reflectionClass )
7281 {
@@ -88,6 +97,7 @@ protected static function _isClassCleanable(\ReflectionClass $reflectionClass)
8897 *
8998 * @param string $classFile
9099 * @return bool
100+ * phpcs:disable Magento2.Functions.StaticFunction
91101 */
92102 protected static function _isClassInCleanableFolders ($ classFile )
93103 {
@@ -112,8 +122,11 @@ protected static function _isClassInCleanableFolders($classFile)
112122 protected static $ classes = [];
113123
114124 /**
125+ * Create a reflection class from the provided class
126+ *
115127 * @param string $class
116128 * @return \ReflectionClass
129+ * phpcs:disable Magento2.Functions.StaticFunction
117130 */
118131 private static function getReflectionClass ($ class )
119132 {
@@ -125,7 +138,9 @@ private static function getReflectionClass($class)
125138
126139 /**
127140 * Restore static variables (after running controller test case)
141+ *
128142 * @TODO: refactor all code where objects are stored to static variables to use object manager instead
143+ * phpcs:ignore Magento2.Functions.StaticFunction
129144 */
130145 public static function restoreStaticVariables ()
131146 {
@@ -142,12 +157,27 @@ public static function restoreStaticVariables()
142157 /**
143158 * Backup static variables
144159 *
160+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
161+ * phpcs:disable Magento2.Functions.StaticFunction
145162 */
146163 public static function backupStaticVariables ()
147164 {
148165 if (count (self ::$ backupStaticVariables ) > 0 ) {
149166 return ;
150167 }
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+
151181 $ classFiles = array_filter (
152182 Files::init ()->getPhpFiles (
153183 Files::INCLUDE_APP_CODE
@@ -156,12 +186,14 @@ public static function backupStaticVariables()
156186 ),
157187 function ($ classFile ) {
158188 return StaticProperties::_isClassInCleanableFolders ($ classFile )
189+ // phpcs:ignore Magento2.Functions.DiscouragedFunction
159190 && strpos (file_get_contents ($ classFile ), ' static ' ) > 0 ;
160191 }
161192 );
162193 $ namespacePattern = '/namespace [a-zA-Z0-9 \\\\]+;/ ' ;
163194 $ classPattern = '/\nclass [a-zA-Z0-9_]+/ ' ;
164195 foreach ($ classFiles as $ classFile ) {
196+ // phpcs:ignore Magento2.Functions.DiscouragedFunction
165197 $ code = file_get_contents ($ classFile );
166198 preg_match ($ namespacePattern , $ code , $ namespace );
167199 preg_match ($ classPattern , $ code , $ class );
@@ -187,17 +219,16 @@ function ($classFile) {
187219 }
188220 }
189221 }
222+
223+ $ cache ->save ($ serializer ->serialize (self ::$ backupStaticVariables ), self ::CACHE_NAME );
190224 }
191225
192226 /**
193227 * Handler for 'startTestSuite' event
194- *
195228 */
196229 public function startTestSuite ()
197230 {
198- if (empty (self ::$ backupStaticVariables )) {
199- self ::backupStaticVariables ();
200- }
231+ self ::backupStaticVariables ();
201232 }
202233
203234 /**
0 commit comments