Passed
Push — master ( b0ca68...10680f )
by Wilmer
27:02 queued 23:37
created

testCommitTransactionsWithSavepoints()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 36
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 48
rs 9.344
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
        $command = $db->createCommand();
43
        $transaction = $db->beginTransaction();
44
45
        $this->assertSame(1, $transaction->getLevel());
46
47
        $command->insert('profile', ['description' => 'test transaction1'])->execute();
48
        $transaction->begin();
49
50
        $this->assertSame(2, $transaction->getLevel());
51
52
        $command->insert('profile', ['description' => 'test transaction2'])->execute();
53
        $transaction->commit();
54
55
        $this->assertSame(1, $transaction->getLevel());
56
57
        $command->insert('profile', ['description' => 'test transaction3'])->execute();
58
        $transaction->commit();
59
60
        $this->assertSame(0, $transaction->getLevel());
61
        $this->assertFalse($transaction->isActive());
62
        $this->assertNull($db->getTransaction());
63
        $this->assertEquals(
64
            '1',
65
            $db->createCommand(
66
                <<<SQL
67
                SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction1'
68
                SQL,
69
            )->queryScalar()
70
        );
71
        $this->assertEquals(
72
            '1',
73
            $db->createCommand(
74
                <<<SQL
75
                SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction2'
76
                SQL,
77
            )->queryScalar()
78
        );
79
        $this->assertEquals(
80
            '1',
81
            $db->createCommand(
82
                <<<SQL
83
                SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction3'
84
                SQL,
85
            )->queryScalar()
86
        );
87
    }
88
89
    /**
90
     * @throws Exception
91
     * @throws InvalidConfigException
92
     * @throws NotSupportedException
93
     * @throws Throwable
94
     */
95
    public function testPartialRollbackTransactionsWithSavePoints(): void
96
    {
97
        $db = $this->getConnection(true);
98
        $db->open();
99
100
        $command = $db->createCommand();
101
        $transaction = $db->beginTransaction();
102
103
        $this->assertSame(1, $transaction->getLevel());
104
105
        $command->insert('profile', ['description' => 'test transaction1'])->execute();
106
        $transaction->begin();
107
108
        $this->assertSame(2, $transaction->getLevel());
109
110
        $command->insert('profile', ['description' => 'test transaction2'])->execute();
111
        $transaction->rollBack();
112
113
        $this->assertSame(1, $transaction->getLevel());
114
        $this->assertTrue($transaction->isActive());
115
116
        $command->insert('profile', ['description' => 'test transaction3'])->execute();
117
        $transaction->commit();
118
119
        $this->assertSame(0, $transaction->getLevel());
120
        $this->assertFalse($transaction->isActive());
121
        $this->assertNull($db->getTransaction());
122
        $this->assertEquals(
123
            '1',
124
            $db->createCommand(
125
                <<<SQL
126
                SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction1'
127
                SQL,
128
            )->queryScalar(),
129
        );
130
        $this->assertEquals(
131
            '0',
132
            $db->createCommand(
133
                <<<SQL
134
                SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction2'
135
                SQL,
136
            )->queryScalar(),
137
        );
138
        $this->assertEquals(
139
            '1',
140
            $db->createCommand(
141
                <<<SQL
142
                SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction3'
143
                SQL,
144
            )->queryScalar(),
145
        );
146
    }
147
148
    /**
149
     * @throws Exception
150
     * @throws NotSupportedException
151
     * @throws InvalidConfigException
152
     * @throws Throwable
153
     */
154
    public function testRollbackTransactionsWithSavePoints(): void
155
    {
156
        $db = $this->getConnection(true);
157
        $db->open();
158
159
        $command = $db->createCommand();
160
        $transaction = $db->beginTransaction();
161
162
        $this->assertSame(1, $transaction->getLevel());
163
164
        $command->insert('profile', ['description' => 'test transaction'])->execute();
165
        $transaction->begin();
166
167
        $this->assertSame(2, $transaction->getLevel());
168
169
        $command->insert('profile', ['description' => 'test transaction'])->execute();
170
        $transaction->rollBack();
171
172
        $this->assertSame(1, $transaction->getLevel());
173
        $this->assertTrue($transaction->isActive());
174
175
        $command->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->assertEquals(
182
            '0',
183
            $db->createCommand(
184
                <<<SQL
185
                SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction'
186
                SQL,
187
            )->queryScalar()
188
        );
189
    }
190
}
191