Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BlockInstantiationTest.php
Go to the documentation of this file.
1 <?php
7 
9 
18 {
19  public function testBlockInstantiation()
20  {
21  $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
22  $invoker(
23  function ($module, $class, $area) {
24  $this->assertNotEmpty($module);
25  $this->assertTrue(class_exists($class), "Block class: {$class}");
27  \Magento\Framework\Config\ScopeInterface::class
28  )->setCurrentScope(
29  $area
30  );
32  \Magento\Framework\App\Http\Context::class
33  );
34  $context->setValue(Context::CONTEXT_AUTH, false, false);
35  $context->setValue(
37  \Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID,
38  \Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID
39  );
41 
42  try {
44  } catch (\Exception $e) {
45  throw new \Exception("Unable to instantiate '{$class}'", 0, $e);
46  }
47  },
48  $this->allBlocksDataProvider()
49  );
50  }
51 
55  public function allBlocksDataProvider()
56  {
57  $blockClass = '';
58  try {
61  \Magento\Store\Model\StoreManagerInterface::class
62  )->getStore()->setWebsiteId(
63  0
64  );
65 
66  $enabledModules = $this->_getEnabledModules();
67  $skipBlocks = $this->_getBlocksToSkip();
68  $templateBlocks = [];
70  foreach ($blockMods as $blockClass => $module) {
71  if (!isset($enabledModules[$module]) || isset($skipBlocks[$blockClass])) {
72  continue;
73  }
74  $class = new \ReflectionClass($blockClass);
75  if ($class->isAbstract() || !$class->isSubclassOf(\Magento\Framework\View\Element\Template::class)) {
76  continue;
77  }
78  $templateBlocks = $this->_addBlock($module, $blockClass, $class, $templateBlocks);
79  }
80  asort($templateBlocks);
81  return $templateBlocks;
82  } catch (\Exception $e) {
83  trigger_error(
84  "Corrupted data provider. Last known block instantiation attempt: '{$blockClass}'." .
85  " Exception: {$e}",
86  E_USER_ERROR
87  );
88  }
89  }
90 
96  protected function _getBlocksToSkip()
97  {
98  $result = [];
99  foreach (glob(__DIR__ . '/_files/skip_blocks*.php') as $file) {
100  $blocks = include $file;
101  $result = array_merge($result, $blocks);
102  }
103  return array_combine($result, $result);
104  }
105 
113  private function _addBlock($module, $blockClass, $class, $templateBlocks)
114  {
115  $area = 'frontend';
116  if ($module == 'Magento_Backend' || strpos(
117  $blockClass,
118  '\\Adminhtml\\'
119  ) !== false || strpos(
120  $blockClass,
121  '_Backend_'
122  ) !== false || $class->isSubclassOf(
123  \Magento\Backend\Block\Template::class
124  )
125  ) {
126  $area = 'adminhtml';
127  }
129  \Magento\Framework\App\AreaList::class
130  )->getArea(
131  \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE
132  )->load(
133  \Magento\Framework\App\Area::PART_CONFIG
134  );
135  $templateBlocks[$module . ', ' . $blockClass . ', ' . $area] = [$module, $blockClass, $area];
136  return $templateBlocks;
137  }
138 }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$_option $_optionId $class
Definition: date.phtml:13
static collectModuleClasses($subTypePattern='[A-Za-z]+')
Definition: Classes.php:187