Transaction.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Database transaction events manager
  8. */
  9. namespace Magento\TestFramework\Event;
  10. class Transaction
  11. {
  12. /**
  13. * @var \Magento\TestFramework\EventManager
  14. */
  15. protected $_eventManager;
  16. /**
  17. * @var \Magento\TestFramework\Event\Param\Transaction
  18. */
  19. protected $_eventParam;
  20. /**
  21. * @var bool
  22. */
  23. protected $_isTransactionActive = false;
  24. /**
  25. * Constructor
  26. *
  27. * @param \Magento\TestFramework\EventManager $eventManager
  28. */
  29. public function __construct(\Magento\TestFramework\EventManager $eventManager)
  30. {
  31. $this->_eventManager = $eventManager;
  32. }
  33. /**
  34. * Handler for 'startTest' event
  35. *
  36. * @param \PHPUnit\Framework\TestCase $test
  37. */
  38. public function startTest(\PHPUnit\Framework\TestCase $test)
  39. {
  40. $this->_processTransactionRequests('startTest', $test);
  41. }
  42. /**
  43. * Handler for 'endTest' event
  44. *
  45. * @param \PHPUnit\Framework\TestCase $test
  46. */
  47. public function endTest(\PHPUnit\Framework\TestCase $test)
  48. {
  49. $this->_processTransactionRequests('endTest', $test);
  50. }
  51. /**
  52. * Handler for 'endTestSuite' event
  53. */
  54. public function endTestSuite()
  55. {
  56. $this->_rollbackTransaction();
  57. }
  58. /**
  59. * Query whether there are any requests for transaction operations and performs them
  60. *
  61. * @param string $eventName
  62. * @param \PHPUnit\Framework\TestCase $test
  63. */
  64. protected function _processTransactionRequests($eventName, \PHPUnit\Framework\TestCase $test)
  65. {
  66. $param = $this->_getEventParam();
  67. $this->_eventManager->fireEvent($eventName . 'TransactionRequest', [$test, $param]);
  68. if ($param->isTransactionRollbackRequested()) {
  69. $this->_rollbackTransaction();
  70. }
  71. if ($param->isTransactionStartRequested()) {
  72. $this->_startTransaction($test);
  73. }
  74. }
  75. /**
  76. * Start transaction and fire 'startTransaction' event
  77. *
  78. * @param \PHPUnit\Framework\TestCase $test
  79. */
  80. protected function _startTransaction(\PHPUnit\Framework\TestCase $test)
  81. {
  82. if (!$this->_isTransactionActive) {
  83. $this->_getConnection()->beginTransparentTransaction();
  84. $this->_isTransactionActive = true;
  85. try {
  86. $this->_eventManager->fireEvent('startTransaction', [$test]);
  87. } catch (\Exception $e) {
  88. $test->getTestResultObject()->addFailure(
  89. $test,
  90. new \PHPUnit\Framework\AssertionFailedError((string)$e),
  91. 0
  92. );
  93. }
  94. }
  95. }
  96. /**
  97. * Rollback transaction and fire 'rollbackTransaction' event
  98. */
  99. protected function _rollbackTransaction()
  100. {
  101. if ($this->_isTransactionActive) {
  102. $this->_getConnection()->rollbackTransparentTransaction();
  103. $this->_isTransactionActive = false;
  104. $this->_eventManager->fireEvent('rollbackTransaction');
  105. $this->_getConnection()->closeConnection();
  106. }
  107. }
  108. /**
  109. * Retrieve database adapter instance
  110. *
  111. * @param string $connectionName
  112. * @return \Magento\Framework\DB\Adapter\AdapterInterface|\Magento\TestFramework\Db\Adapter\TransactionInterface
  113. * @throws \Magento\Framework\Exception\LocalizedException
  114. */
  115. protected function _getConnection($connectionName = \Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION)
  116. {
  117. /** @var $resource \Magento\Framework\App\ResourceConnection */
  118. $resource = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  119. ->get(\Magento\Framework\App\ResourceConnection::class);
  120. return $resource->getConnection($connectionName);
  121. }
  122. /**
  123. * Retrieve clean instance of transaction event parameter
  124. *
  125. * @return \Magento\TestFramework\Event\Param\Transaction
  126. */
  127. protected function _getEventParam()
  128. {
  129. /* reset object state instead of instantiating new object over and over again */
  130. if (!$this->_eventParam) {
  131. $this->_eventParam = new \Magento\TestFramework\Event\Param\Transaction();
  132. } else {
  133. $this->_eventParam->__construct();
  134. }
  135. return $this->_eventParam;
  136. }
  137. }