123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- /**
- * Implementation of the @magentoAppIsolation DocBlock annotation - isolation of global application objects in memory
- */
- namespace Magento\TestFramework\Annotation;
- class AppIsolation
- {
- /**
- * Flag to prevent an excessive test case isolation if the last test has been just isolated
- *
- * @var bool
- */
- private $_hasNonIsolatedTests = true;
- /**
- * @var \Magento\TestFramework\Application
- */
- private $_application;
- /**
- * @var array
- */
- private $serverGlobalBackup;
- /**
- * Constructor
- *
- * @param \Magento\TestFramework\Application $application
- */
- public function __construct(\Magento\TestFramework\Application $application)
- {
- $this->_application = $application;
- }
- /**
- * Isolate global application objects
- */
- protected function _isolateApp()
- {
- if ($this->_hasNonIsolatedTests) {
- $this->_application->reinitialize();
- $_SESSION = [];
- $_COOKIE = [];
- session_write_close();
- $this->_hasNonIsolatedTests = false;
- }
- }
- /**
- * Isolate application before running test case
- */
- public function startTestSuite()
- {
- $this->serverGlobalBackup = $_SERVER;
- $this->_isolateApp();
- }
- /**
- * Isolate application after running test case
- */
- public function endTestSuite()
- {
- $_SERVER = $this->serverGlobalBackup;
- }
- /**
- * Handler for 'endTest' event
- *
- * @param \PHPUnit\Framework\TestCase $test
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function endTest(\PHPUnit\Framework\TestCase $test)
- {
- $this->_hasNonIsolatedTests = true;
- /* Determine an isolation from doc comment */
- $annotations = $test->getAnnotations();
- $annotations = array_replace((array) $annotations['class'], (array) $annotations['method']);
- if (isset($annotations['magentoAppIsolation'])) {
- $isolation = $annotations['magentoAppIsolation'];
- if ($isolation !== ['enabled'] && $isolation !== ['disabled']) {
- throw new \Magento\Framework\Exception\LocalizedException(
- __('Invalid "@magentoAppIsolation" annotation, can be "enabled" or "disabled" only.')
- );
- }
- $isIsolationEnabled = $isolation === ['enabled'];
- } else {
- /* Controller tests should be isolated by default */
- $isIsolationEnabled = $test instanceof \Magento\TestFramework\TestCase\AbstractController;
- }
- if ($isIsolationEnabled) {
- $this->_isolateApp();
- }
- }
- }
|