Bootstrap.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Helper providing exclusive restricted access to the underlying bootstrap instance
  8. */
  9. namespace Magento\TestFramework\Helper;
  10. class Bootstrap
  11. {
  12. /**
  13. * @var \Magento\TestFramework\Helper\Bootstrap
  14. */
  15. private static $_instance;
  16. /**
  17. * @var \Magento\Framework\ObjectManagerInterface
  18. */
  19. private static $_objectManager;
  20. /**
  21. * @var \Magento\TestFramework\Bootstrap
  22. */
  23. protected $_bootstrap;
  24. /**
  25. * Set self instance for static access
  26. *
  27. * @param \Magento\TestFramework\Helper\Bootstrap $instance
  28. * @throws \Magento\Framework\Exception\LocalizedException
  29. */
  30. public static function setInstance(\Magento\TestFramework\Helper\Bootstrap $instance)
  31. {
  32. if (self::$_instance) {
  33. throw new \Magento\Framework\Exception\LocalizedException(__('Helper instance cannot be redefined.'));
  34. }
  35. self::$_instance = $instance;
  36. }
  37. /**
  38. * Self instance getter
  39. *
  40. * @return \Magento\TestFramework\Helper\Bootstrap
  41. * @throws \Magento\Framework\Exception\LocalizedException
  42. */
  43. public static function getInstance()
  44. {
  45. if (!self::$_instance) {
  46. throw new \Magento\Framework\Exception\LocalizedException(__('Helper instance is not defined yet.'));
  47. }
  48. return self::$_instance;
  49. }
  50. /**
  51. * Check the possibility to send headers or to use headers related function (like set_cookie)
  52. *
  53. * @return bool
  54. */
  55. public static function canTestHeaders()
  56. {
  57. if (!headers_sent() && extension_loaded('xdebug') && function_exists('xdebug_get_headers')) {
  58. return true;
  59. }
  60. return false;
  61. }
  62. /**
  63. * Constructor
  64. *
  65. * @param \Magento\TestFramework\Bootstrap $bootstrap
  66. */
  67. public function __construct(\Magento\TestFramework\Bootstrap $bootstrap)
  68. {
  69. $this->_bootstrap = $bootstrap;
  70. }
  71. /**
  72. * Retrieve application installation directory
  73. *
  74. * @return string
  75. */
  76. public function getAppTempDir()
  77. {
  78. return $this->_bootstrap->getApplication()->getTempDir();
  79. }
  80. /**
  81. * Retrieve application initialization options
  82. *
  83. * @return array
  84. */
  85. public function getAppInitParams()
  86. {
  87. return $this->_bootstrap->getApplication()->getInitParams();
  88. }
  89. /**
  90. * Reinitialize the application instance optionally passing parameters to be overridden.
  91. * Intended to be used for the tests isolation purposes.
  92. *
  93. * @param array $overriddenParams
  94. */
  95. public function reinitialize(array $overriddenParams = [])
  96. {
  97. $this->_bootstrap->getApplication()->reinitialize($overriddenParams);
  98. }
  99. /**
  100. * Perform the full request processing by the application instance.
  101. * Intended to be used by the controller tests.
  102. */
  103. public function runApp()
  104. {
  105. $this->_bootstrap->getApplication()->run();
  106. }
  107. /**
  108. * Retrieve object manager
  109. *
  110. * @return \Magento\Framework\ObjectManagerInterface
  111. */
  112. public static function getObjectManager()
  113. {
  114. return self::$_objectManager;
  115. }
  116. /**
  117. * Set object manager
  118. *
  119. * @param \Magento\Framework\ObjectManagerInterface $objectManager
  120. */
  121. public static function setObjectManager(\Magento\Framework\ObjectManagerInterface $objectManager)
  122. {
  123. self::$_objectManager = $objectManager;
  124. }
  125. /**
  126. * Get bootstrap object
  127. *
  128. * @return \Magento\TestFramework\Bootstrap
  129. */
  130. public function getBootstrap()
  131. {
  132. return $this->_bootstrap;
  133. }
  134. /**
  135. * Load area
  136. * @param string $areaCode
  137. */
  138. public function loadArea($areaCode)
  139. {
  140. self::$_objectManager->get(\Magento\Framework\App\State::class)->setAreaCode($areaCode);
  141. self::$_objectManager->get(\Magento\Framework\App\AreaList::class)->getArea($areaCode)->load();
  142. }
  143. }