AppIsolation.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Implementation of the @magentoAppIsolation DocBlock annotation - isolation of global application objects in memory
  8. */
  9. namespace Magento\TestFramework\Annotation;
  10. class AppIsolation
  11. {
  12. /**
  13. * Flag to prevent an excessive test case isolation if the last test has been just isolated
  14. *
  15. * @var bool
  16. */
  17. private $_hasNonIsolatedTests = true;
  18. /**
  19. * @var \Magento\TestFramework\Application
  20. */
  21. private $_application;
  22. /**
  23. * @var array
  24. */
  25. private $serverGlobalBackup;
  26. /**
  27. * Constructor
  28. *
  29. * @param \Magento\TestFramework\Application $application
  30. */
  31. public function __construct(\Magento\TestFramework\Application $application)
  32. {
  33. $this->_application = $application;
  34. }
  35. /**
  36. * Isolate global application objects
  37. */
  38. protected function _isolateApp()
  39. {
  40. if ($this->_hasNonIsolatedTests) {
  41. $this->_application->reinitialize();
  42. $_SESSION = [];
  43. $_COOKIE = [];
  44. session_write_close();
  45. $this->_hasNonIsolatedTests = false;
  46. }
  47. }
  48. /**
  49. * Isolate application before running test case
  50. */
  51. public function startTestSuite()
  52. {
  53. $this->serverGlobalBackup = $_SERVER;
  54. $this->_isolateApp();
  55. }
  56. /**
  57. * Isolate application after running test case
  58. */
  59. public function endTestSuite()
  60. {
  61. $_SERVER = $this->serverGlobalBackup;
  62. }
  63. /**
  64. * Handler for 'endTest' event
  65. *
  66. * @param \PHPUnit\Framework\TestCase $test
  67. * @throws \Magento\Framework\Exception\LocalizedException
  68. */
  69. public function endTest(\PHPUnit\Framework\TestCase $test)
  70. {
  71. $this->_hasNonIsolatedTests = true;
  72. /* Determine an isolation from doc comment */
  73. $annotations = $test->getAnnotations();
  74. $annotations = array_replace((array) $annotations['class'], (array) $annotations['method']);
  75. if (isset($annotations['magentoAppIsolation'])) {
  76. $isolation = $annotations['magentoAppIsolation'];
  77. if ($isolation !== ['enabled'] && $isolation !== ['disabled']) {
  78. throw new \Magento\Framework\Exception\LocalizedException(
  79. __('Invalid "@magentoAppIsolation" annotation, can be "enabled" or "disabled" only.')
  80. );
  81. }
  82. $isIsolationEnabled = $isolation === ['enabled'];
  83. } else {
  84. /* Controller tests should be isolated by default */
  85. $isIsolationEnabled = $test instanceof \Magento\TestFramework\TestCase\AbstractController;
  86. }
  87. if ($isIsolationEnabled) {
  88. $this->_isolateApp();
  89. }
  90. }
  91. }