|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Tests\Common; |
|
6
|
|
|
|
|
7
|
|
|
use Yiisoft\Db\Tests\AbstractConnectionPDOTest; |
|
8
|
|
|
|
|
9
|
|
|
abstract class CommonConnectionPDOTest extends AbstractConnectionPDOTest |
|
10
|
|
|
{ |
|
11
|
|
|
public function testCommitTransactionsWithSavepoints(): void |
|
12
|
|
|
{ |
|
13
|
|
|
$db = $this->getConnection(true); |
|
14
|
|
|
|
|
15
|
|
|
$transaction = $db->beginTransaction(); |
|
16
|
|
|
|
|
17
|
|
|
$this->assertSame(1, $transaction->getLevel()); |
|
18
|
|
|
|
|
19
|
|
|
$db->createCommand()->insert('profile', ['description' => 'test transaction1'])->execute(); |
|
20
|
|
|
|
|
21
|
|
|
$transaction->begin(); |
|
22
|
|
|
|
|
23
|
|
|
$this->assertSame(2, $transaction->getLevel()); |
|
24
|
|
|
|
|
25
|
|
|
$db->createCommand()->insert('profile', ['description' => 'test transaction2'])->execute(); |
|
26
|
|
|
|
|
27
|
|
|
$transaction->commit(); |
|
28
|
|
|
|
|
29
|
|
|
$this->assertSame(1, $transaction->getLevel()); |
|
30
|
|
|
|
|
31
|
|
|
$db->createCommand()->insert('profile', ['description' => 'test transaction3'])->execute(); |
|
32
|
|
|
$transaction->commit(); |
|
33
|
|
|
|
|
34
|
|
|
$this->assertSame(0, $transaction->getLevel()); |
|
35
|
|
|
$this->assertFalse($transaction->isActive()); |
|
36
|
|
|
$this->assertNull($db->getTransaction()); |
|
37
|
|
|
$this->assertSame( |
|
38
|
|
|
'1', |
|
39
|
|
|
$db->createCommand( |
|
40
|
|
|
<<<SQL |
|
41
|
|
|
SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction1' |
|
42
|
|
|
SQL, |
|
43
|
|
|
)->queryScalar() |
|
44
|
|
|
); |
|
45
|
|
|
$this->assertSame( |
|
46
|
|
|
'1', |
|
47
|
|
|
$db->createCommand( |
|
48
|
|
|
<<<SQL |
|
49
|
|
|
SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction2' |
|
50
|
|
|
SQL, |
|
51
|
|
|
)->queryScalar() |
|
52
|
|
|
); |
|
53
|
|
|
$this->assertSame( |
|
54
|
|
|
'1', |
|
55
|
|
|
$db->createCommand( |
|
56
|
|
|
<<<SQL |
|
57
|
|
|
SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction3' |
|
58
|
|
|
SQL, |
|
59
|
|
|
)->queryScalar() |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function testPartialRollbackTransactionsWithSavePoints(): void |
|
64
|
|
|
{ |
|
65
|
|
|
$db = $this->getConnection(true); |
|
66
|
|
|
$db->open(); |
|
67
|
|
|
|
|
68
|
|
|
$transaction = $db->beginTransaction(); |
|
69
|
|
|
|
|
70
|
|
|
$this->assertSame(1, $transaction->getLevel()); |
|
71
|
|
|
|
|
72
|
|
|
$db->createCommand()->insert('profile', ['description' => 'test transaction1'])->execute(); |
|
73
|
|
|
$transaction->begin(); |
|
74
|
|
|
|
|
75
|
|
|
$this->assertSame(2, $transaction->getLevel()); |
|
76
|
|
|
|
|
77
|
|
|
$db->createCommand()->insert('profile', ['description' => 'test transaction2'])->execute(); |
|
78
|
|
|
|
|
79
|
|
|
$transaction->rollBack(); |
|
80
|
|
|
|
|
81
|
|
|
$this->assertSame(1, $transaction->getLevel()); |
|
82
|
|
|
$this->assertTrue($transaction->isActive()); |
|
83
|
|
|
|
|
84
|
|
|
$db->createCommand()->insert('profile', ['description' => 'test transaction3'])->execute(); |
|
85
|
|
|
$transaction->commit(); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertSame(0, $transaction->getLevel()); |
|
88
|
|
|
$this->assertFalse($transaction->isActive()); |
|
89
|
|
|
$this->assertNull($db->getTransaction()); |
|
90
|
|
|
$this->assertSame( |
|
91
|
|
|
'1', |
|
92
|
|
|
$db->createCommand( |
|
93
|
|
|
<<<SQL |
|
94
|
|
|
SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction1' |
|
95
|
|
|
SQL, |
|
96
|
|
|
)->queryScalar(), |
|
97
|
|
|
); |
|
98
|
|
|
$this->assertSame( |
|
99
|
|
|
'0', |
|
100
|
|
|
$db->createCommand( |
|
101
|
|
|
<<<SQL |
|
102
|
|
|
SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction2' |
|
103
|
|
|
SQL, |
|
104
|
|
|
)->queryScalar(), |
|
105
|
|
|
); |
|
106
|
|
|
$this->assertSame( |
|
107
|
|
|
'1', |
|
108
|
|
|
$db->createCommand( |
|
109
|
|
|
<<<SQL |
|
110
|
|
|
SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction3' |
|
111
|
|
|
SQL, |
|
112
|
|
|
)->queryScalar(), |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function testRollbackTransactionsWithSavePoints(): void |
|
117
|
|
|
{ |
|
118
|
|
|
$db = $this->getConnection(true); |
|
119
|
|
|
$db->open(); |
|
120
|
|
|
|
|
121
|
|
|
$transaction = $db->beginTransaction(); |
|
122
|
|
|
|
|
123
|
|
|
$this->assertSame(1, $transaction->getLevel()); |
|
124
|
|
|
|
|
125
|
|
|
$db->createCommand()->insert('profile', ['description' => 'test transaction'])->execute(); |
|
126
|
|
|
$transaction->begin(); |
|
127
|
|
|
|
|
128
|
|
|
$this->assertSame(2, $transaction->getLevel()); |
|
129
|
|
|
|
|
130
|
|
|
$db->createCommand()->insert('profile', ['description' => 'test transaction'])->execute(); |
|
131
|
|
|
$transaction->rollBack(); |
|
132
|
|
|
|
|
133
|
|
|
$this->assertSame(1, $transaction->getLevel()); |
|
134
|
|
|
$this->assertTrue($transaction->isActive()); |
|
135
|
|
|
|
|
136
|
|
|
$db->createCommand()->insert('profile', ['description' => 'test transaction'])->execute(); |
|
137
|
|
|
$transaction->rollBack(); |
|
138
|
|
|
|
|
139
|
|
|
$this->assertSame(0, $transaction->getLevel()); |
|
140
|
|
|
$this->assertFalse($transaction->isActive()); |
|
141
|
|
|
$this->assertNull($db->getTransaction()); |
|
142
|
|
|
$this->assertSame( |
|
143
|
|
|
'0', |
|
144
|
|
|
$db->createCommand( |
|
145
|
|
|
<<<SQL |
|
146
|
|
|
SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction' |
|
147
|
|
|
SQL, |
|
148
|
|
|
)->queryScalar() |
|
149
|
|
|
); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|