Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Static Public Member Functions | Static Protected Member Functions | Static Protected Attributes
StaticProperties Class Reference

Public Member Functions

 startTestSuite ()
 
 endTestSuite (\PHPUnit\Framework\TestSuite $suite)
 

Static Public Member Functions

static restoreStaticVariables ()
 
static backupStaticVariables ()
 

Static Protected Member Functions

static _isClassCleanable (\ReflectionClass $reflectionClass)
 
static _isClassInCleanableFolders ($classFile)
 

Static Protected Attributes

static $_cleanableFolders
 
static $backupStaticVariables = []
 
static $_classesToSkip
 
static $classes = []
 

Detailed Description

Definition at line 15 of file StaticProperties.php.

Member Function Documentation

◆ _isClassCleanable()

static _isClassCleanable ( \ReflectionClass  $reflectionClass)
staticprotected

Check whether it is allowed to clean given class static variables

Parameters
\ReflectionClass$reflectionClass
Returns
bool

Definition at line 71 of file StaticProperties.php.

72  {
73  // do not process blacklisted classes from integration framework
74  foreach (self::$_classesToSkip as $notCleanableClass) {
75  if ($reflectionClass->getName() == $notCleanableClass || is_subclass_of(
76  $reflectionClass->getName(),
77  $notCleanableClass
78  )
79  ) {
80  return false;
81  }
82  }
83  return true;
84  }
is_subclass_of($obj, $className)
$reflectionClass
Definition: categories.php:25

◆ _isClassInCleanableFolders()

static _isClassInCleanableFolders (   $classFile)
staticprotected

Check if class has to be backed up

Parameters
string$classFile
Returns
bool

Definition at line 92 of file StaticProperties.php.

93  {
94  $classFile = str_replace('\\', '/', $classFile);
95  foreach (self::$_cleanableFolders as $include => $excludeSet) {
96  if (stripos($classFile, $include) !== false) {
97  foreach ($excludeSet as $exclude) {
98  $excludeExp = '#' . str_replace('*', '[\w]+', $exclude) . '#';
99  if (preg_match($excludeExp, $classFile)) {
100  return false; // File is in an "include" directory, but also an "exclude" subdirectory of it
101  }
102  }
103  return true; // File is in an "include" directory, and not in an "exclude" subdirectory of it
104  }
105  }
106  return false; // File is not in an "include" directory
107  }

◆ backupStaticVariables()

static backupStaticVariables ( )
static

Backup static variables

Definition at line 146 of file StaticProperties.php.

147  {
148  if (count(self::$backupStaticVariables) > 0) {
149  return;
150  }
151  $classFiles = array_filter(
152  Files::init()->getPhpFiles(
156  ),
157  function ($classFile) {
159  && strpos(file_get_contents($classFile), ' static ') > 0;
160  }
161  );
162  $namespacePattern = '/namespace [a-zA-Z0-9\\\\]+;/';
163  $classPattern = '/\nclass [a-zA-Z0-9_]+/';
164  foreach ($classFiles as $classFile) {
165  $code = file_get_contents($classFile);
166  preg_match($namespacePattern, $code, $namespace);
167  preg_match($classPattern, $code, $class);
168  if (!isset($namespace[0]) || !isset($class[0])) {
169  continue;
170  }
171  // trim namespace and class name
172  $namespace = substr($namespace[0], 10, strlen($namespace[0]) - 11);
173  $class = substr($class[0], 7, strlen($class[0]) - 7);
174  $className = $namespace . '\\' . $class;
175 
176  try {
177  $reflectionClass = self::getReflectionClass($className);
178  } catch (\Exception $e) {
179  continue;
180  }
181  if (self::_isClassCleanable($reflectionClass)) {
182  $staticProperties = $reflectionClass->getProperties(\ReflectionProperty::IS_STATIC);
183  foreach ($staticProperties as $staticProperty) {
184  $staticProperty->setAccessible(true);
185  $value = $staticProperty->getValue();
186  self::$backupStaticVariables[$className][$staticProperty->getName()] = $value;
187  }
188  }
189  }
190  }
$_option $_optionId $class
Definition: date.phtml:13
$value
Definition: gender.phtml:16
$reflectionClass
Definition: categories.php:25
$code
Definition: info.phtml:12
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31

◆ endTestSuite()

endTestSuite ( \PHPUnit\Framework\TestSuite  $suite)

Handler for 'endTestSuite' event

Parameters
\PHPUnit\Framework\TestSuite$suite

Definition at line 208 of file StaticProperties.php.

209  {
210  $clearStatics = false;
211  foreach ($suite->tests() as $test) {
212  if ($test instanceof \Magento\TestFramework\TestCase\AbstractController) {
213  $clearStatics = true;
214  break;
215  }
216  }
217  if ($clearStatics) {
219  }
220  }

◆ restoreStaticVariables()

static restoreStaticVariables ( )
static

Restore static variables (after running controller test case) @TODO: refactor all code where objects are stored to static variables to use object manager instead

Definition at line 130 of file StaticProperties.php.

131  {
132  foreach (array_keys(self::$backupStaticVariables) as $class) {
133  $reflectionClass = self::getReflectionClass($class);
134  $staticProperties = $reflectionClass->getProperties(\ReflectionProperty::IS_STATIC);
135  foreach ($staticProperties as $staticProperty) {
136  $staticProperty->setAccessible(true);
137  $staticProperty->setValue(self::$backupStaticVariables[$class][$staticProperty->getName()]);
138  }
139  }
140  }
$_option $_optionId $class
Definition: date.phtml:13
$reflectionClass
Definition: categories.php:25

◆ startTestSuite()

startTestSuite ( )

Handler for 'startTestSuite' event

Definition at line 196 of file StaticProperties.php.

197  {
198  if (empty(self::$backupStaticVariables)) {
200  }
201  }

Field Documentation

◆ $_classesToSkip

$_classesToSkip
staticprotected
Initial value:
= [
\Magento\Framework\App\ObjectManager::class,
\Magento\TestFramework\Helper\Bootstrap::class,
\Magento\TestFramework\Event\Magento::class,
\Magento\TestFramework\Event\PhpUnit::class,
\Magento\TestFramework\Annotation\AppIsolation::class,
\Magento\TestFramework\Workaround\Cleanup\StaticProperties::class,
\Magento\Framework\Phrase::class,
]

Definition at line 35 of file StaticProperties.php.

◆ $_cleanableFolders

$_cleanableFolders
staticprotected
Initial value:
= [
'/dev/tests/integration/framework' => [],
]

Definition at line 24 of file StaticProperties.php.

◆ $backupStaticVariables

$backupStaticVariables = []
staticprotected

Definition at line 28 of file StaticProperties.php.

◆ $classes

$classes = []
staticprotected

Definition at line 112 of file StaticProperties.php.


The documentation for this class was generated from the following file: