Passed
Pull Request — master (#20)
by Wilmer
13:35
created

CommandTest::testMultiStatementSupport()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 46
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 33
nc 1
nop 0
dl 0
loc 46
rs 9.392
c 2
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Sqlite\Tests;
6
7
use Yiisoft\Db\Exception\Exception;
8
use Yiisoft\Db\Exception\InvalidConfigException;
9
use Yiisoft\Db\Exception\NotSupportedException;
10
use Yiisoft\Db\Tests\CommandTest as AbstractCommandTest;
11
12
class CommandTest extends AbstractCommandTest
13
{
14
    protected ?string $driverName = 'sqlite';
15
16
    public function testAutoQuoting(): void
17
    {
18
        $db = $this->getConnection(false);
19
20
        $sql = 'SELECT [[id]], [[t.name]] FROM {{customer}} t';
21
22
        $command = $db->createCommand($sql);
23
24
        $this->assertEquals('SELECT `id`, `t`.`name` FROM `customer` t', $command->getSql());
25
    }
26
27
    /**
28
     * @dataProvider upsertProvider
29
     *
30
     * @param array $firstData
31
     * @param array $secondData
32
     *
33
     * @throws Exception
34
     * @throws InvalidConfigException
35
     * @throws NotSupportedException
36
     */
37
    public function testUpsert(array $firstData, array $secondData)
38
    {
39
        if (version_compare($this->getConnection(false)->getServerVersion(), '3.8.3', '<')) {
40
            $this->markTestSkipped('SQLite < 3.8.3 does not support "WITH" keyword.');
41
42
            return;
43
        }
44
45
        parent::testUpsert($firstData, $secondData);
46
    }
47
48
    public function testAddDropPrimaryKey(): void
49
    {
50
        $this->markTestSkipped('SQLite does not support adding/dropping primary keys.');
51
    }
52
53
    public function testAddDropCheck(): void
54
    {
55
        $this->markTestSkipped('SQLite does not support adding/dropping check constraints.');
56
    }
57
58
    public function testMultiStatementSupport(): void
59
    {
60
        $db = $this->getConnection(false, true);
61
62
        $sql = <<<'SQL'
63
DROP TABLE IF EXISTS {{T_multistatement}};
64
CREATE TABLE {{T_multistatement}} (
65
    [[intcol]] INTEGER,
66
    [[textcol]] TEXT
67
);
68
INSERT INTO {{T_multistatement}} VALUES(41, :val1);
69
INSERT INTO {{T_multistatement}} VALUES(42, :val2);
70
SQL;
71
72
        $db->createCommand($sql, [
73
            'val1' => 'foo',
74
            'val2' => 'bar',
75
        ])->execute();
76
77
        $this->assertSame([
78
            [
79
                'intcol' => '41',
80
                'textcol' => 'foo',
81
            ],
82
            [
83
                'intcol' => '42',
84
                'textcol' => 'bar',
85
            ],
86
        ], $db->createCommand('SELECT * FROM {{T_multistatement}}')->queryAll());
87
88
        $sql = <<<'SQL'
89
UPDATE {{T_multistatement}} SET [[intcol]] = :newInt WHERE [[textcol]] = :val1;
90
DELETE FROM {{T_multistatement}} WHERE [[textcol]] = :val2;
91
SELECT * FROM {{T_multistatement}}
92
SQL;
93
94
        $this->assertSame([
95
            [
96
                'intcol' => '410',
97
                'textcol' => 'foo',
98
            ],
99
        ], $db->createCommand($sql, [
100
            'newInt' => 410,
101
            'val1' => 'foo',
102
            'val2' => 'bar',
103
        ])->queryAll());
104
    }
105
106
    public function batchInsertSqlProvider(): array
107
    {
108
        $parent = parent::batchInsertSqlProvider();
109
        unset($parent['wrongBehavior']); /** Produces SQL syntax error: General error: 1 near ".": syntax error */
110
111
        return $parent;
112
    }
113
}
114