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

CommandTest::testQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 24
rs 9.7333
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 testAddDefaultValue(): 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\DDLQueryBuilder does not support adding default value constraints.'
33
        );
34
35
        $command->addDefaultValue('name', 'table', 'column', 'value');
36
    }
37
38
    public function testBatchInsert(): 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\Schema::loadTableSchema() is not supported by core-db.'
47
        );
48
49
        $command->batchInsert('table', ['column1', 'column2'], [['value1', 'value2'], ['value3', 'value4']]);
50
    }
51
52
    public function testCheckIntegrity(): void
53
    {
54
        $db = $this->getConnection();
55
56
        $command = $db->createCommand();
57
58
        $this->expectException(NotSupportedException::class);
59
        $this->expectExceptionMessage(
60
            'Yiisoft\Db\Tests\Support\Stub\DDLQueryBuilder does not support enabling/disabling integrity check.'
61
        );
62
63
        $command->checkIntegrity('schema', 'table')->execute();
64
    }
65
66
    public function testCreateTable(): void
67
    {
68
        $this->db = $this->getConnection();
69
70
        $command = $this->db->createCommand();
71
72
        $expected = <<<SQL
73
        CREATE TABLE [test_table] (
74
        \t[id] pk,
75
        \t[name] string(255) NOT NULL,
76
        \t[email] string(255) NOT NULL,
77
        \t[address] string(255) NOT NULL,
78
        \t[status] integer NOT NULL,
79
        \t[profile_id] integer NOT NULL,
80
        \t[created_at] timestamp NOT NULL,
81
        \t[updated_at] timestamp NOT NULL
82
        ) CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB
83
        SQL;
84
        $columns = [
85
            'id' => $this->primaryKey(5),
86
            'name' => $this->string(255)->notNull(),
87
            'email' => $this->string(255)->notNull(),
88
            'address' => $this->string(255)->notNull(),
89
            'status' => $this->integer()->notNull(),
90
            'profile_id' => $this->integer()->notNull(),
91
            'created_at' => $this->timestamp()->notNull(),
92
            'updated_at' => $this->timestamp()->notNull(),
93
        ];
94
        $options = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
95
        $sql = $command->createTable('test_table', $columns, $options)->getSql();
96
97
        Assert::equalsWithoutLE($expected, $sql);
98
    }
99
100
    public function testDropDefaultValue(): void
101
    {
102
        $db = $this->getConnection();
103
104
        $command = $db->createCommand();
105
106
        $this->expectException(NotSupportedException::class);
107
        $this->expectExceptionMessage(
108
            'Yiisoft\Db\Tests\Support\Stub\DDLQueryBuilder does not support dropping default value constraints.'
109
        );
110
111
        $command->dropDefaultValue('column', 'table');
112
    }
113
114
    public function testExecute(): void
115
    {
116
        $db = $this->getConnection();
117
118
        $command = $db->createCommand();
119
120
        $this->expectException(NotSupportedException::class);
121
        $this->expectExceptionMessage(
122
            'Yiisoft\Db\Tests\Support\Stub\Command::internalExecute() is not supported by core-db.'
123
        );
124
125
        $command->createTable('customer', ['id' => 'pk'])->execute();
126
    }
127
128
    public function testInsert(): void
129
    {
130
        $db = $this->getConnection();
131
132
        $command = $db->createCommand();
133
134
        $this->expectException(NotSupportedException::class);
135
        $this->expectExceptionMessage(
136
            'Yiisoft\Db\Tests\Support\Stub\Schema::loadTableSchema() is not supported by core-db.'
137
        );
138
139
        $command->insert('customer', ['email' => '[email protected]', 'name' => 'test', 'address' => 'test address']);
140
    }
141
142
    public function testQuery(): void
143
    {
144
        $db = $this->getConnection();
145
146
        $db->open();
147
        $pdo = $db->getPdo();
148
        $pdo?->exec(
149
            <<<SQL
150
            CREATE TABLE customer (id integer PRIMARY KEY)
151
            SQL
152
        );
153
        $command = $db->createCommand();
154
        $command->setSql(
155
            <<<SQL
156
            SELECT * FROM customer
157
            SQL
158
        );
159
160
        $this->expectException(NotSupportedException::class);
161
        $this->expectExceptionMessage(
162
            'Yiisoft\Db\Tests\Support\Stub\Command::internalExecute() is not supported by core-db.'
163
        );
164
165
        $command->query();
166
    }
167
168
    public function testQueryAll(): void
169
    {
170
        $db = $this->getConnection();
171
172
        $db->open();
173
        $pdo = $db->getPdo();
174
        $pdo?->exec(
175
            <<<SQL
176
            CREATE TABLE customer (id integer PRIMARY KEY)
177
            SQL
178
        );
179
        $command = $db->createCommand();
180
        $command->setSql(
181
            <<<SQL
182
            SELECT * FROM {{customer}}
183
            SQL,
184
        );
185
186
        $this->expectException(NotSupportedException::class);
187
        $this->expectExceptionMessage(
188
            'Yiisoft\Db\Tests\Support\Stub\Command::internalExecute() is not supported by core-db.'
189
        );
190
191
        $command->queryAll();
192
    }
193
194
    public function testRenameTable(): void
195
    {
196
        $db = $this->getConnection();
197
198
        $sql = $db->createCommand()->renameTable('table', 'newname')->getSql();
199
200
        $this->assertSame(
201
            DbHelper::replaceQuotes(
202
                <<<SQL
203
                RENAME TABLE [[table]] TO [[newname]]
204
                SQL,
205
                $db->getName(),
206
            ),
207
            $sql,
208
        );
209
    }
210
211
    public function testResetSequence(): void
212
    {
213
        $db = $this->getConnection();
214
215
        $this->expectException(NotSupportedException::class);
216
        $this->expectExceptionMessage(
217
            'Yiisoft\Db\Tests\Support\Stub\DMLQueryBuilder does not support resetting sequence.'
218
        );
219
220
        $db->createCommand()->resetSequence('table', 5);
221
    }
222
223
    /**
224
     * @dataProvider \Yiisoft\Db\Tests\Provider\CommandProvider::upsert()
225
     *
226
     * @throws Exception
227
     * @throws InvalidConfigException
228
     * @throws NotSupportedException
229
     */
230
    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

230
    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...
231
    {
232
        $db = $this->getConnection();
233
234
        $command = $db->createCommand();
235
236
        $this->expectException(NotSupportedException::class);
237
        $this->expectExceptionMessage(
238
            'Yiisoft\Db\Tests\Support\Stub\DMLQueryBuilder does not support upsert.'
239
        );
240
241
        $command->upsert('table', $firstData);
242
    }
243
}
244