Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DbIsolation.php
Go to the documentation of this file.
1 <?php
7 
12 {
13  const MAGENTO_DB_ISOLATION = 'magentoDbIsolation';
14 
18  protected $_isIsolationActive = false;
19 
26  public function startTestTransactionRequest(
27  \PHPUnit\Framework\TestCase $test,
28  \Magento\TestFramework\Event\Param\Transaction $param
29  ) {
30  $methodIsolation = $this->_getIsolation($test);
31  if ($this->_isIsolationActive) {
32  if ($methodIsolation === false) {
33  $param->requestTransactionRollback();
34  }
35  } elseif ($methodIsolation || ($methodIsolation === null && $this->_getIsolation($test))) {
36  $param->requestTransactionStart();
37  }
38  }
39 
46  public function endTestTransactionRequest(
47  \PHPUnit\Framework\TestCase $test,
48  \Magento\TestFramework\Event\Param\Transaction $param
49  ) {
50  if ($this->_isIsolationActive && $this->_getIsolation($test)) {
51  $param->requestTransactionRollback();
52  }
53  }
54 
62  public function startTransaction(\PHPUnit\Framework\TestCase $test)
63  {
64  $this->_isIsolationActive = true;
65  }
66 
70  public function rollbackTransaction()
71  {
72  $this->_isIsolationActive = false;
73  }
74 
86  protected function _getIsolation(\PHPUnit\Framework\TestCase $test)
87  {
88  $annotations = $this->getAnnotations($test);
89  if (isset($annotations[self::MAGENTO_DB_ISOLATION])) {
90  $isolation = $annotations[self::MAGENTO_DB_ISOLATION];
91  if ($isolation !== ['enabled'] && $isolation !== ['disabled']) {
92  throw new \Magento\Framework\Exception\LocalizedException(
93  __('Invalid "@magentoDbIsolation" annotation, can be "enabled" or "disabled" only.')
94  );
95  }
96  return $isolation === ['enabled'];
97  }
98  return null;
99  }
100 
105  private function getAnnotations(\PHPUnit\Framework\TestCase $test)
106  {
107  $annotations = $test->getAnnotations();
108  return array_replace($annotations['class'], $annotations['method']);
109  }
110 }
endTestTransactionRequest(\PHPUnit\Framework\TestCase $test, \Magento\TestFramework\Event\Param\Transaction $param)
Definition: DbIsolation.php:46
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
startTestTransactionRequest(\PHPUnit\Framework\TestCase $test, \Magento\TestFramework\Event\Param\Transaction $param)
Definition: DbIsolation.php:26
_getIsolation(\PHPUnit\Framework\TestCase $test)
Definition: DbIsolation.php:86
startTransaction(\PHPUnit\Framework\TestCase $test)
Definition: DbIsolation.php:62