Passed
Pull Request — master (#420)
by Wilmer
02:59
created

testRollbackTransactionsWithSavePoints()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 23
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 33
rs 9.552
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests\Common;
6
7
use Throwable;
8
use Yiisoft\Db\Exception\Exception;
9
use Yiisoft\Db\Exception\InvalidConfigException;
10
use Yiisoft\Db\Exception\NotSupportedException;
11
use Yiisoft\Db\Tests\AbstractConnectionPDOTest;
12
use Yiisoft\Db\Tests\Support\DbHelper;
13
14
abstract class CommonConnectionPDOTest extends AbstractConnectionPDOTest
15
{
16
    /**
17
     * @throws Exception
18
     * @throws InvalidConfigException
19
     */
20
    public function testCreateCommandWithLoggerProfiler(): void
21
    {
22
        $db = $this->getConnection();
23
24
        $db->setLogger(DbHelper::getLogger());
25
        $db->setProfiler(DbHelper::getProfiler());
26
        $command = $db->createCommand('SELECT 1');
27
28
        $this->assertSame('SELECT 1', $command->getSql());
29
        $this->assertSame([], $command->getParams());
30
    }
31
32
    /**
33
     * @throws Exception
34
     * @throws InvalidConfigException
35
     * @throws NotSupportedException
36
     * @throws Throwable
37
     */
38
    public function testCommitTransactionsWithSavepoints(): void
39
    {
40
        $db = $this->getConnection(true);
41
42
        $transaction = $db->beginTransaction();
43
44
        $this->assertSame(1, $transaction->getLevel());
45
46
        $db->createCommand()->insert('profile', ['description' => 'test transaction1'])->execute();
47
48
        $transaction->begin();
49
50
        $this->assertSame(2, $transaction->getLevel());
51
52
        $db->createCommand()->insert('profile', ['description' => 'test transaction2'])->execute();
53
54
        $transaction->commit();
55
56
        $this->assertSame(1, $transaction->getLevel());
57
58
        $db->createCommand()->insert('profile', ['description' => 'test transaction3'])->execute();
59
        $transaction->commit();
60
61
        $this->assertSame(0, $transaction->getLevel());
62
        $this->assertFalse($transaction->isActive());
63
        $this->assertNull($db->getTransaction());
64
        $this->assertSame(
65
            '1',
66
            $db->createCommand(
67
                <<<SQL
68
                SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction1'
69
                SQL,
70
            )->queryScalar()
71
        );
72
        $this->assertSame(
73
            '1',
74
            $db->createCommand(
75
                <<<SQL
76
                SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction2'
77
                SQL,
78
            )->queryScalar()
79
        );
80
        $this->assertSame(
81
            '1',
82
            $db->createCommand(
83
                <<<SQL
84
                SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction3'
85
                SQL,
86
            )->queryScalar()
87
        );
88
    }
89
90
    /**
91
     * @throws Exception
92
     * @throws InvalidConfigException
93
     * @throws NotSupportedException
94
     * @throws Throwable
95
     */
96
    public function testPartialRollbackTransactionsWithSavePoints(): void
97
    {
98
        $db = $this->getConnection(true);
99
        $db->open();
100
101
        $transaction = $db->beginTransaction();
102
103
        $this->assertSame(1, $transaction->getLevel());
104
105
        $db->createCommand()->insert('profile', ['description' => 'test transaction1'])->execute();
106
        $transaction->begin();
107
108
        $this->assertSame(2, $transaction->getLevel());
109
110
        $db->createCommand()->insert('profile', ['description' => 'test transaction2'])->execute();
111
112
        $transaction->rollBack();
113
114
        $this->assertSame(1, $transaction->getLevel());
115
        $this->assertTrue($transaction->isActive());
116
117
        $db->createCommand()->insert('profile', ['description' => 'test transaction3'])->execute();
118
        $transaction->commit();
119
120
        $this->assertSame(0, $transaction->getLevel());
121
        $this->assertFalse($transaction->isActive());
122
        $this->assertNull($db->getTransaction());
123
        $this->assertSame(
124
            '1',
125
            $db->createCommand(
126
                <<<SQL
127
                SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction1'
128
                SQL,
129
            )->queryScalar(),
130
        );
131
        $this->assertSame(
132
            '0',
133
            $db->createCommand(
134
                <<<SQL
135
                SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction2'
136
                SQL,
137
            )->queryScalar(),
138
        );
139
        $this->assertSame(
140
            '1',
141
            $db->createCommand(
142
                <<<SQL
143
                SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction3'
144
                SQL,
145
            )->queryScalar(),
146
        );
147
    }
148
149
    /**
150
     * @throws Exception
151
     * @throws NotSupportedException
152
     * @throws InvalidConfigException
153
     * @throws Throwable
154
     */
155
    public function testRollbackTransactionsWithSavePoints(): void
156
    {
157
        $db = $this->getConnection(true);
158
        $db->open();
159
160
        $transaction = $db->beginTransaction();
161
162
        $this->assertSame(1, $transaction->getLevel());
163
164
        $db->createCommand()->insert('profile', ['description' => 'test transaction'])->execute();
165
        $transaction->begin();
166
167
        $this->assertSame(2, $transaction->getLevel());
168
169
        $db->createCommand()->insert('profile', ['description' => 'test transaction'])->execute();
170
        $transaction->rollBack();
171
172
        $this->assertSame(1, $transaction->getLevel());
173
        $this->assertTrue($transaction->isActive());
174
175
        $db->createCommand()->insert('profile', ['description' => 'test transaction'])->execute();
176
        $transaction->rollBack();
177
178
        $this->assertSame(0, $transaction->getLevel());
179
        $this->assertFalse($transaction->isActive());
180
        $this->assertNull($db->getTransaction());
181
        $this->assertSame(
182
            '0',
183
            $db->createCommand(
184
                <<<SQL
185
                SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction'
186
                SQL,
187
            )->queryScalar()
188
        );
189
    }
190
}
191