Passed
Pull Request — master (#397)
by Wilmer
02:36
created

CommandTest::testCreateView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 21
rs 9.7666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests\Command;
6
7
use Yiisoft\Db\Exception\Exception;
8
use Yiisoft\Db\Exception\InvalidConfigException;
9
use Yiisoft\Db\Exception\NotSupportedException;
10
use Yiisoft\Db\Tests\AbstractCommandTest;
11
use Yiisoft\Db\Tests\Support\Assert;
12
use Yiisoft\Db\Tests\Support\DbHelper;
13
use Yiisoft\Db\Tests\Support\TestTrait;
14
15
/**
16
 * @group db
17
 *
18
 * @psalm-suppress PropertyNotSetInConstructor
19
 */
20
final class CommandTest extends AbstractCommandTest
21
{
22
    use TestTrait;
23
24
    public function testBatchInsert(): void
25
    {
26
        $db = $this->getConnection();
27
28
        $command = $db->createCommand();
29
30
        $this->expectException(NotSupportedException::class);
31
        $this->expectExceptionMessage(
32
            'Yiisoft\Db\Tests\Support\Stub\Schema::loadTableSchema() is not supported by core-db.'
33
        );
34
35
        $command->batchInsert('table', ['column1', 'column2'], [['value1', 'value2'], ['value3', 'value4']]);
36
    }
37
38
    public function testCheckIntegrity(): void
39
    {
40
        $db = $this->getConnection();
41
42
        $command = $db->createCommand();
43
44
        $this->expectException(NotSupportedException::class);
45
        $this->expectExceptionMessage(
46
            'Yiisoft\Db\Tests\Support\Stub\DDLQueryBuilder does not support enabling/disabling integrity check.'
47
        );
48
49
        $command->checkIntegrity('schema', 'table')->execute();
50
    }
51
52
    public function testCreateTable(): void
53
    {
54
        $this->db = $this->getConnection();
55
56
        $command = $this->db->createCommand();
57
58
        $expected = <<<SQL
59
        CREATE TABLE [test_table] (
60
        \t[id] pk,
61
        \t[name] string(255) NOT NULL,
62
        \t[email] string(255) NOT NULL,
63
        \t[address] string(255) NOT NULL,
64
        \t[status] integer NOT NULL,
65
        \t[profile_id] integer NOT NULL,
66
        \t[created_at] timestamp NOT NULL,
67
        \t[updated_at] timestamp NOT NULL
68
        ) CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB
69
        SQL;
70
        $columns = [
71
            'id' => $this->primaryKey(5),
72
            'name' => $this->string(255)->notNull(),
73
            'email' => $this->string(255)->notNull(),
74
            'address' => $this->string(255)->notNull(),
75
            'status' => $this->integer()->notNull(),
76
            'profile_id' => $this->integer()->notNull(),
77
            'created_at' => $this->timestamp()->notNull(),
78
            'updated_at' => $this->timestamp()->notNull(),
79
        ];
80
        $options = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
81
        $sql = $command->createTable('test_table', $columns, $options)->getSql();
82
83
        Assert::equalsWithoutLE($expected, $sql);
84
    }
85
86
    public function testCreateView(): void
87
    {
88
        $db = $this->getConnection();
89
90
        $command = $db->createCommand();
91
92
        $sql = $command->createView(
93
            'view',
94
            <<<SQL
95
            SELECT * FROM table
96
            SQL,
97
        )->getSql();
98
99
        $this->assertSame(
100
            DbHelper::replaceQuotes(
101
                <<<SQL
102
                CREATE VIEW [[view]] AS SELECT * FROM table
103
                SQL,
104
                $db->getName(),
105
            ),
106
            $sql,
107
        );
108
    }
109
110
    public function testExecute(): void
111
    {
112
        $db = $this->getConnection();
113
114
        $command = $db->createCommand();
115
116
        $this->expectException(NotSupportedException::class);
117
        $this->expectExceptionMessage(
118
            'Yiisoft\Db\Tests\Support\Stub\Command::internalExecute() is not supported by core-db.'
119
        );
120
121
        $command->createTable('customer', ['id' => 'pk'])->execute();
122
    }
123
124
    public function testInsert(): void
125
    {
126
        $db = $this->getConnection();
127
128
        $command = $db->createCommand();
129
130
        $this->expectException(NotSupportedException::class);
131
        $this->expectExceptionMessage(
132
            'Yiisoft\Db\Tests\Support\Stub\Schema::loadTableSchema() is not supported by core-db.'
133
        );
134
135
        $command->insert('customer', ['email' => '[email protected]', 'name' => 'test', 'address' => 'test address']);
136
    }
137
138
    public function testQuery(): void
139
    {
140
        $db = $this->getConnection('customer');
141
142
        $command = $db->createCommand();
143
        $command->setSql(
144
            <<<SQL
145
            SELECT * FROM customer
146
            SQL
147
        );
148
149
        $this->expectException(NotSupportedException::class);
150
        $this->expectExceptionMessage(
151
            'Yiisoft\Db\Tests\Support\Stub\Command::internalExecute() is not supported by core-db.'
152
        );
153
154
        $command->query();
155
    }
156
157
    public function testQueryAll(): void
158
    {
159
        $db = $this->getConnection('customer');
160
161
        $command = $db->createCommand();
162
        $command->setSql(
163
            <<<SQL
164
            SELECT * FROM {{customer}}
165
            SQL,
166
        );
167
168
        $this->expectException(NotSupportedException::class);
169
        $this->expectExceptionMessage(
170
            'Yiisoft\Db\Tests\Support\Stub\Command::internalExecute() is not supported by core-db.'
171
        );
172
173
        $command->queryAll();
174
    }
175
176
    public function testRenameTable(): void
177
    {
178
        $db = $this->getConnection();
179
180
        $sql = $db->createCommand()->renameTable('table', 'newname')->getSql();
181
182
        $this->assertSame(
183
            DbHelper::replaceQuotes(
184
                <<<SQL
185
                RENAME TABLE [[table]] TO [[newname]]
186
                SQL,
187
                $db->getName(),
188
            ),
189
            $sql,
190
        );
191
    }
192
193
    public function testResetSequence(): void
194
    {
195
        $db = $this->getConnection();
196
197
        $this->expectException(NotSupportedException::class);
198
        $this->expectExceptionMessage(
199
            'Yiisoft\Db\Tests\Support\Stub\DMLQueryBuilder does not support resetting sequence.'
200
        );
201
202
        $db->createCommand()->resetSequence('table', 5);
203
    }
204
205
    /**
206
     * @dataProvider \Yiisoft\Db\Tests\Provider\CommandProvider::upsert()
207
     *
208
     * @throws Exception
209
     * @throws InvalidConfigException
210
     * @throws NotSupportedException
211
     */
212
    public function testUpsert(array $firstData, array $secondData): void
0 ignored issues
show
Unused Code introduced by
The parameter $secondData is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

212
    public function testUpsert(array $firstData, /** @scrutinizer ignore-unused */ array $secondData): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
213
    {
214
        $db = $this->getConnection();
215
216
        $command = $db->createCommand();
217
218
        $this->expectException(NotSupportedException::class);
219
        $this->expectExceptionMessage(
220
            'Yiisoft\Db\Tests\Support\Stub\DMLQueryBuilder does not support upsert.'
221
        );
222
223
        $command->upsert('table', $firstData);
224
    }
225
}
226