Passed
Pull Request — master (#380)
by Wilmer
16:46 queued 13:55
created

CommonConnectionPDOTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 92
c 2
b 0
f 0
dl 0
loc 157
rs 10
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testClone() 0 34 1
A testGetLastInsertID() 0 12 1
A testQuoteValue() 0 27 2
A testInsertEx() 0 10 1
A testQuoteValueEscapingValueFull() 0 28 3
A generateQuoterEscapingValues() 0 23 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests\Common;
6
7
use Yiisoft\Db\Tests\AbstractConnectionPDOTest;
8
use Yiisoft\Db\Tests\Support\TestTrait;
9
10
use function chr;
11
use function mb_chr;
12
use function str_replace;
13
14
/**
15
 * @group mssql
16
 * @group mysql
17
 * @group pgsql
18
 * @group oracle
19
 * @group sqlite
20
 */
21
abstract class CommonConnectionPDOTest extends AbstractConnectionPDOTest
22
{
23
    use TestTrait;
24
25
    /**
26
     * Ensure database connection is reset on when a connection is cloned.
27
     *
28
     * Make sure each connection element has its own PDO instance i.e. own connection to the DB.
29
     * Also, transaction elements should not be shared between two connections.
30
     */
31
    public function testClone(): void
32
    {
33
        $db = $this->getConnection();
34
35
        $this->assertNull($db->getTransaction());
36
        $this->assertNull($db->getPDO());
37
38
        $db->open();
39
40
        $this->assertNull($db->getTransaction());
41
        $this->assertNotNull($db->getPDO());
42
43
        $conn2 = clone $db;
44
45
        $this->assertNull($db->getTransaction());
46
        $this->assertNotNull($db->getPDO());
47
48
        $this->assertNull($conn2->getTransaction());
49
        $this->assertNull($conn2->getPDO());
50
51
        $db->beginTransaction();
52
53
        $this->assertNotNull($db->getTransaction());
54
        $this->assertNotNull($db->getPDO());
55
56
        $this->assertNull($conn2->getTransaction());
57
        $this->assertNull($conn2->getPDO());
58
59
        $conn3 = clone $db;
60
61
        $this->assertNotNull($db->getTransaction());
62
        $this->assertNotNull($db->getPDO());
63
        $this->assertNull($conn3->getTransaction());
64
        $this->assertNull($conn3->getPDO());
65
    }
66
67
    public function testGetLastInsertID(): void
68
    {
69
        $db = $this->getConnectionWithData();
70
71
        $command = $db->createCommand();
72
        $command->insert(
73
            'customer',
74
            ['name' => 'test1', 'email' => '[email protected]', 'address' => 'address1', 'status' => 1],
75
        )->execute();
76
77
        $this->assertSame('4', $db->getLastInsertID());
78
        $this->assertSame('4', $db->getLastInsertID('customer'));
79
    }
80
81
    public function testInsertEx(): void
82
    {
83
        $db = $this->getConnectionWithData();
84
85
        $result = $db
86
            ->createCommand()
87
            ->insertEx('customer', ['name' => 'testParams', 'email' => '[email protected]', 'address' => '1']);
88
89
        $this->assertIsArray($result);
90
        $this->assertNotNull($result['id']);
91
    }
92
93
    public function testQuoteValue(): void
94
    {
95
        $db = $this->getConnectionWithData();
96
97
        $db->createCommand(
98
            <<<SQL
99
            DELETE FROM {{quoter}}
100
            SQL
101
        )->execute();
102
        $data = $this->generateQuoterEscapingValues();
103
104
        foreach ($data as $index => $value) {
105
            $quotedName = $db->quoteValue('testValue_' . $index);
106
            $quoteValue = $db->quoteValue($value);
107
108
            $db->createCommand(
109
                <<<SQL
110
                INSERT INTO {{quoter}} ([[name]], [[description]]) values ($quotedName, $quoteValue)
111
                SQL
112
            )->execute();
113
            $result = $db->createCommand(
114
                <<<SQL
115
                SELECT * FROM {{quoter}} WHERE [[name]]=$quotedName
116
                SQL
117
            )->queryOne();
118
119
            $this->assertSame($value, $result['description']);
120
        }
121
    }
122
123
    public function testQuoteValueEscapingValueFull()
124
    {
125
        $db = $this->getConnectionWithData();
126
127
        $template = 'aaaaa{1}aaa{1}aaaabbbbb{2}bbbb{2}bbbb';
128
        $db->createCommand(
129
            <<<SQL
130
            DELETE FROM {{quoter}}
131
            SQL
132
        )->execute();
133
134
        for ($symbol1 = 1; $symbol1 <= 127; $symbol1++) {
135
            for ($symbol2 = 1; $symbol2 <= 127; $symbol2++) {
136
                $quotedName = $db->quoteValue('test_' . $symbol1 . '_' . $symbol2);
137
                $testString = str_replace(['{1}', '{2}',], [chr($symbol1), chr($symbol2)], $template);
138
                $quoteValue = $db->quoteValue($testString);
139
                $db->createCommand(
140
                    <<<SQL
141
                    INSERT INTO {{quoter}} ([[name]], [[description]]) values ($quotedName, $quoteValue)
142
                    SQL
143
                )->execute();
144
                $result = $db->createCommand(
145
                    <<<SQL
146
                    SELECT * FROM {{quoter}} WHERE [[name]]=$quotedName
147
                    SQL
148
                )->queryOne();
149
150
                $this->assertSame($testString, $result['description']);
151
            }
152
        }
153
    }
154
155
    protected function generateQuoterEscapingValues(): array
156
    {
157
        $result = [];
158
        $stringLength = 16;
159
160
        for ($i = 32; $i < 128 - $stringLength; $i += $stringLength) {
161
            $str = '';
162
163
            for ($symbol = $i; $symbol < $i + $stringLength; $symbol++) {
164
                $str .= mb_chr($symbol, 'UTF-8');
165
            }
166
167
            $result[] = $str;
168
            $str = '';
169
170
            for ($symbol = $i; $symbol < $i + $stringLength; $symbol++) {
171
                $str .= mb_chr($symbol, 'UTF-8') . mb_chr($symbol, 'UTF-8');
172
            }
173
174
            $result[] = $str;
175
        }
176
177
        return $result;
178
    }
179
}
180