DbIsolation.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\TestFramework\Annotation;
  7. /**
  8. * Implementation of the @magentoDbIsolation DocBlock annotation
  9. */
  10. class DbIsolation
  11. {
  12. const MAGENTO_DB_ISOLATION = 'magentoDbIsolation';
  13. /**
  14. * @var bool
  15. */
  16. protected $_isIsolationActive = false;
  17. /**
  18. * Handler for 'startTestTransactionRequest' event
  19. *
  20. * @param \PHPUnit\Framework\TestCase $test
  21. * @param \Magento\TestFramework\Event\Param\Transaction $param
  22. */
  23. public function startTestTransactionRequest(
  24. \PHPUnit\Framework\TestCase $test,
  25. \Magento\TestFramework\Event\Param\Transaction $param
  26. ) {
  27. $methodIsolation = $this->_getIsolation($test);
  28. if ($this->_isIsolationActive) {
  29. if ($methodIsolation === false) {
  30. $param->requestTransactionRollback();
  31. }
  32. } elseif ($methodIsolation || ($methodIsolation === null && $this->_getIsolation($test))) {
  33. $param->requestTransactionStart();
  34. }
  35. }
  36. /**
  37. * Handler for 'endTestTransactionRequest' event
  38. *
  39. * @param \PHPUnit\Framework\TestCase $test
  40. * @param \Magento\TestFramework\Event\Param\Transaction $param
  41. */
  42. public function endTestTransactionRequest(
  43. \PHPUnit\Framework\TestCase $test,
  44. \Magento\TestFramework\Event\Param\Transaction $param
  45. ) {
  46. if ($this->_isIsolationActive && $this->_getIsolation($test)) {
  47. $param->requestTransactionRollback();
  48. }
  49. }
  50. /**
  51. * Handler for 'startTransaction' event
  52. *
  53. * @param \PHPUnit\Framework\TestCase $test
  54. *
  55. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  56. */
  57. public function startTransaction(\PHPUnit\Framework\TestCase $test)
  58. {
  59. $this->_isIsolationActive = true;
  60. }
  61. /**
  62. * Handler for 'rollbackTransaction' event
  63. */
  64. public function rollbackTransaction()
  65. {
  66. $this->_isIsolationActive = false;
  67. }
  68. /**
  69. * Retrieve database isolation annotation value for the current scope.
  70. * Possible results:
  71. * NULL - annotation is not defined
  72. * TRUE - annotation is defined as 'enabled'
  73. * FALSE - annotation is defined as 'disabled'
  74. *
  75. * @param \PHPUnit\Framework\TestCase $test
  76. * @return bool|null Returns NULL, if isolation is not defined for the current scope
  77. * @throws \Magento\Framework\Exception\LocalizedException
  78. */
  79. protected function _getIsolation(\PHPUnit\Framework\TestCase $test)
  80. {
  81. $annotations = $this->getAnnotations($test);
  82. if (isset($annotations[self::MAGENTO_DB_ISOLATION])) {
  83. $isolation = $annotations[self::MAGENTO_DB_ISOLATION];
  84. if ($isolation !== ['enabled'] && $isolation !== ['disabled']) {
  85. throw new \Magento\Framework\Exception\LocalizedException(
  86. __('Invalid "@magentoDbIsolation" annotation, can be "enabled" or "disabled" only.')
  87. );
  88. }
  89. return $isolation === ['enabled'];
  90. }
  91. return null;
  92. }
  93. /**
  94. * @param \PHPUnit\Framework\TestCase $test
  95. * @return array
  96. */
  97. private function getAnnotations(\PHPUnit\Framework\TestCase $test)
  98. {
  99. $annotations = $test->getAnnotations();
  100. return array_replace($annotations['class'], $annotations['method']);
  101. }
  102. }