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

CommandTest::testBatchInsert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 12
rs 10
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}} WHERE id=:id
183
            SQL,
184
            [':id' => 1],
0 ignored issues
show
Unused Code introduced by
The call to Yiisoft\Db\Command\Command::setSql() has too many arguments starting with array(':id' => 1). ( Ignorable by Annotation )

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

184
        $command->/** @scrutinizer ignore-call */ 
185
                  setSql(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
185
        );
186
187
        $this->expectException(NotSupportedException::class);
188
        $this->expectExceptionMessage(
189
            'Yiisoft\Db\Tests\Support\Stub\Command::internalExecute() is not supported by core-db.'
190
        );
191
192
        $command->queryAll();
193
    }
194
195
    public function testRenameTable(): void
196
    {
197
        $db = $this->getConnection();
198
199
        $sql = $db->createCommand()->renameTable('table', 'newname')->getSql();
200
201
        $this->assertSame(
202
            DbHelper::replaceQuotes(
203
                <<<SQL
204
                RENAME TABLE [[table]] TO [[newname]]
205
                SQL,
206
                $db->getName(),
207
            ),
208
            $sql,
209
        );
210
    }
211
212
    public function testResetSequence(): void
213
    {
214
        $db = $this->getConnection();
215
216
        $this->expectException(NotSupportedException::class);
217
        $this->expectExceptionMessage(
218
            'Yiisoft\Db\Tests\Support\Stub\DMLQueryBuilder does not support resetting sequence.'
219
        );
220
221
        $db->createCommand()->resetSequence('table', 5);
222
    }
223
224
    /**
225
     * @dataProvider \Yiisoft\Db\Tests\Provider\CommandProvider::upsert()
226
     *
227
     * @throws Exception
228
     * @throws InvalidConfigException
229
     * @throws NotSupportedException
230
     */
231
    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

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