AppArea.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\TestFramework\Annotation;
  7. class AppArea
  8. {
  9. const ANNOTATION_NAME = 'magentoAppArea';
  10. /**
  11. * @var \Magento\TestFramework\Application
  12. */
  13. private $_application;
  14. /**
  15. * List of allowed areas.
  16. *
  17. * @var array
  18. */
  19. private $_allowedAreas = [
  20. \Magento\Framework\App\Area::AREA_GLOBAL,
  21. \Magento\Framework\App\Area::AREA_ADMINHTML,
  22. \Magento\Framework\App\Area::AREA_FRONTEND,
  23. \Magento\Framework\App\Area::AREA_WEBAPI_REST,
  24. \Magento\Framework\App\Area::AREA_WEBAPI_SOAP,
  25. \Magento\Framework\App\Area::AREA_CRONTAB,
  26. \Magento\Framework\App\Area::AREA_GRAPHQL
  27. ];
  28. /**
  29. * @param \Magento\TestFramework\Application $application
  30. */
  31. public function __construct(\Magento\TestFramework\Application $application)
  32. {
  33. $this->_application = $application;
  34. }
  35. /**
  36. * Get current application area
  37. *
  38. * @param array $annotations
  39. * @return string
  40. * @throws \Magento\Framework\Exception\LocalizedException
  41. */
  42. protected function _getTestAppArea($annotations)
  43. {
  44. $area = isset(
  45. $annotations['method'][self::ANNOTATION_NAME]
  46. ) ? current(
  47. $annotations['method'][self::ANNOTATION_NAME]
  48. ) : (isset(
  49. $annotations['class'][self::ANNOTATION_NAME]
  50. ) ? current(
  51. $annotations['class'][self::ANNOTATION_NAME]
  52. ) : \Magento\TestFramework\Application::DEFAULT_APP_AREA);
  53. if (false == in_array($area, $this->_allowedAreas)) {
  54. throw new \Magento\Framework\Exception\LocalizedException(
  55. __(
  56. 'Invalid "@magentoAppArea" annotation, can be "%1" only.',
  57. implode('", "', $this->_allowedAreas)
  58. )
  59. );
  60. }
  61. return $area;
  62. }
  63. /**
  64. * Start test case event observer
  65. *
  66. * @param \PHPUnit\Framework\TestCase $test
  67. */
  68. public function startTest(\PHPUnit\Framework\TestCase $test)
  69. {
  70. $area = $this->_getTestAppArea($test->getAnnotations());
  71. if ($this->_application->getArea() !== $area) {
  72. $this->_application->reinitialize();
  73. if ($this->_application->getArea() !== $area) {
  74. $this->_application->loadArea($area);
  75. }
  76. }
  77. }
  78. }