Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions
RestrictedCodeTest Class Reference
Inheritance diagram for RestrictedCodeTest:

Public Member Functions

 testPhpFiles ()
 

Static Public Member Functions

static setUpBeforeClass ()
 

Protected Member Functions

 setUp ()
 
 _testRestrictedClasses ($file)
 

Static Protected Member Functions

static _loadData (array &$data, $filePattern)
 
static _readList ($file)
 

Detailed Description

Tests to find usage of restricted code

Definition at line 13 of file RestrictedCodeTest.php.

Member Function Documentation

◆ _loadData()

static _loadData ( array &  $data,
  $filePattern 
)
staticprotected

Loads and merges data from fixtures

Parameters
array$data
string$filePattern
Returns
void

Definition at line 57 of file RestrictedCodeTest.php.

58  {
59  foreach (glob(__DIR__ . '/_files/' . $filePattern) as $file) {
60  $relativePath = str_replace(
61  '\\',
62  '/',
63  str_replace(BP . DIRECTORY_SEPARATOR, '', $file)
64  );
65  array_push(self::$_fixtureFiles, $relativePath);
66  $data = array_merge_recursive($data, self::_readList($file));
67  }
68  }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
const BP
Definition: autoload.php:14
$relativePath
Definition: get.php:35

◆ _readList()

static _readList (   $file)
staticprotected

Isolate including a file into a method to reduce scope

Parameters
string$file
Returns
array

Definition at line 76 of file RestrictedCodeTest.php.

77  {
78  return include $file;
79  }

◆ _testRestrictedClasses()

_testRestrictedClasses (   $file)
protected

Assert that restricted classes are not used in the file

Parameters
string$file
Returns
void

Definition at line 109 of file RestrictedCodeTest.php.

110  {
111  $content = file_get_contents($file);
112  foreach (self::$_classes as $restrictedClass => $classRules) {
113  foreach ($classRules['exclude'] as $skippedPathInfo) {
114  if (strpos($file, $this->getExcludedFilePath($skippedPathInfo)) === 0) {
115  continue 2;
116  }
117  }
118 
119  $this->assertFalse(
120  \Magento\TestFramework\Utility\CodeCheck::isClassUsed($restrictedClass, $content),
121  sprintf(
122  "Class '%s' is restricted in %s. Suggested replacement: %s",
123  $restrictedClass,
124  $file,
125  $classRules['replacement']
126  )
127  );
128  }
129  }

◆ setUp()

setUp ( )
protected

Definition at line 35 of file RestrictedCodeTest.php.

36  {
37  $this->componentRegistrar = new ComponentRegistrar();
38  }

◆ setUpBeforeClass()

static setUpBeforeClass ( )
static

Read fixtures into memory as arrays

Returns
void

Definition at line 45 of file RestrictedCodeTest.php.

46  {
47  self::_loadData(self::$_classes, 'restricted_classes*.php');
48  }
static _loadData(array &$data, $filePattern)

◆ testPhpFiles()

testPhpFiles ( )

Test that restricted entities are not used in PHP files

Returns
void

Definition at line 86 of file RestrictedCodeTest.php.

87  {
88  $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
89  $testFiles = \Magento\TestFramework\Utility\ChangedFiles::getPhpFiles(__DIR__ . '/../_files/changed_files*');
90  foreach (self::$_fixtureFiles as $fixtureFile) {
91  if (array_key_exists($fixtureFile, $testFiles)) {
92  unset($testFiles[$fixtureFile]);
93  }
94  }
95  $invoker(
96  function ($file) {
97  $this->_testRestrictedClasses($file);
98  },
99  $testFiles
100  );
101  }
static getPhpFiles($changedFilesList, $fileTypes=0)
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60

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