Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AppIsolation.php
Go to the documentation of this file.
1 <?php
11 
13 {
19  private $_hasNonIsolatedTests = true;
20 
24  private $_application;
25 
29  private $serverGlobalBackup;
30 
36  public function __construct(\Magento\TestFramework\Application $application)
37  {
38  $this->_application = $application;
39  }
40 
44  protected function _isolateApp()
45  {
46  if ($this->_hasNonIsolatedTests) {
47  $this->_application->reinitialize();
48  $_SESSION = [];
49  $_COOKIE = [];
50  session_write_close();
51  $this->_hasNonIsolatedTests = false;
52  }
53  }
54 
58  public function startTestSuite()
59  {
60  $this->serverGlobalBackup = $_SERVER;
61  $this->_isolateApp();
62  }
63 
67  public function endTestSuite()
68  {
69  $_SERVER = $this->serverGlobalBackup;
70  }
71 
78  public function endTest(\PHPUnit\Framework\TestCase $test)
79  {
80  $this->_hasNonIsolatedTests = true;
81 
82  /* Determine an isolation from doc comment */
83  $annotations = $test->getAnnotations();
84  $annotations = array_replace((array) $annotations['class'], (array) $annotations['method']);
85  if (isset($annotations['magentoAppIsolation'])) {
86  $isolation = $annotations['magentoAppIsolation'];
87  if ($isolation !== ['enabled'] && $isolation !== ['disabled']) {
88  throw new \Magento\Framework\Exception\LocalizedException(
89  __('Invalid "@magentoAppIsolation" annotation, can be "enabled" or "disabled" only.')
90  );
91  }
92  $isIsolationEnabled = $isolation === ['enabled'];
93  } else {
94  /* Controller tests should be isolated by default */
95  $isIsolationEnabled = $test instanceof \Magento\TestFramework\TestCase\AbstractController;
96  }
97 
98  if ($isIsolationEnabled) {
99  $this->_isolateApp();
100  }
101  }
102 }
__construct(\Magento\TestFramework\Application $application)
endTest(\PHPUnit\Framework\TestCase $test)
__()
Definition: __.php:13
$application
Definition: bootstrap.php:58