BlockInstantiationTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Test\Integrity\Modular;
  7. use Magento\Customer\Model\Context;
  8. /**
  9. * This test ensures that all blocks have the appropriate constructor arguments that allow
  10. * them to be instantiated via the objectManager.
  11. *
  12. * @magentoAppIsolation enabled
  13. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  14. */
  15. class BlockInstantiationTest extends \Magento\TestFramework\TestCase\AbstractIntegrity
  16. {
  17. public function testBlockInstantiation()
  18. {
  19. $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
  20. $invoker(
  21. function ($module, $class, $area) {
  22. $this->assertNotEmpty($module);
  23. $this->assertTrue(class_exists($class), "Block class: {$class}");
  24. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  25. \Magento\Framework\Config\ScopeInterface::class
  26. )->setCurrentScope(
  27. $area
  28. );
  29. $context = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  30. \Magento\Framework\App\Http\Context::class
  31. );
  32. $context->setValue(Context::CONTEXT_AUTH, false, false);
  33. $context->setValue(
  34. Context::CONTEXT_GROUP,
  35. \Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID,
  36. \Magento\Customer\Model\GroupManagement::NOT_LOGGED_IN_ID
  37. );
  38. \Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea($area);
  39. try {
  40. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create($class);
  41. } catch (\Exception $e) {
  42. throw new \Exception("Unable to instantiate '{$class}'", 0, $e);
  43. }
  44. },
  45. $this->allBlocksDataProvider()
  46. );
  47. }
  48. /**
  49. * @return array
  50. */
  51. public function allBlocksDataProvider()
  52. {
  53. $blockClass = '';
  54. try {
  55. /** @var $website \Magento\Store\Model\Website */
  56. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  57. \Magento\Store\Model\StoreManagerInterface::class
  58. )->getStore()->setWebsiteId(
  59. 0
  60. );
  61. $enabledModules = $this->_getEnabledModules();
  62. $skipBlocks = $this->_getBlocksToSkip();
  63. $templateBlocks = [];
  64. $blockMods = \Magento\Framework\App\Utility\Classes::collectModuleClasses('Block');
  65. foreach ($blockMods as $blockClass => $module) {
  66. if (!isset($enabledModules[$module]) || isset($skipBlocks[$blockClass])) {
  67. continue;
  68. }
  69. $class = new \ReflectionClass($blockClass);
  70. if ($class->isAbstract() || !$class->isSubclassOf(\Magento\Framework\View\Element\Template::class)) {
  71. continue;
  72. }
  73. $templateBlocks = $this->_addBlock($module, $blockClass, $class, $templateBlocks);
  74. }
  75. asort($templateBlocks);
  76. return $templateBlocks;
  77. } catch (\Exception $e) {
  78. trigger_error(
  79. "Corrupted data provider. Last known block instantiation attempt: '{$blockClass}'." .
  80. " Exception: {$e}",
  81. E_USER_ERROR
  82. );
  83. }
  84. }
  85. /**
  86. * Loads block classes, that should not be instantiated during the instantiation test
  87. *
  88. * @return array
  89. */
  90. protected function _getBlocksToSkip()
  91. {
  92. $result = [];
  93. foreach (glob(__DIR__ . '/_files/skip_blocks*.php') as $file) {
  94. $blocks = include $file;
  95. $result = array_merge($result, $blocks);
  96. }
  97. return array_combine($result, $result);
  98. }
  99. /**
  100. * @param $module
  101. * @param $blockClass
  102. * @param $class
  103. * @param $templateBlocks
  104. * @return mixed
  105. */
  106. private function _addBlock($module, $blockClass, $class, $templateBlocks)
  107. {
  108. $area = 'frontend';
  109. if ($module == 'Magento_Backend' || strpos(
  110. $blockClass,
  111. '\\Adminhtml\\'
  112. ) !== false || strpos(
  113. $blockClass,
  114. '_Backend_'
  115. ) !== false || $class->isSubclassOf(
  116. \Magento\Backend\Block\Template::class
  117. )
  118. ) {
  119. $area = 'adminhtml';
  120. }
  121. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  122. \Magento\Framework\App\AreaList::class
  123. )->getArea(
  124. \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE
  125. )->load(
  126. \Magento\Framework\App\Area::PART_CONFIG
  127. );
  128. $templateBlocks[$module . ', ' . $blockClass . ', ' . $area] = [$module, $blockClass, $area];
  129. return $templateBlocks;
  130. }
  131. }