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