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

CommandTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 217
Duplicated Lines 0 %

Importance

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

12 Methods

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

224
    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...
225
    {
226
        $db = $this->getConnectionWithData();
227
228
        $command = $db->createCommand();
229
230
        $this->expectException(NotSupportedException::class);
231
        $this->expectExceptionMessage(
232
            'Yiisoft\Db\Tests\Support\Stubs\DMLQueryBuilder does not support upsert.'
233
        );
234
235
        $command->upsert('table', $firstData);
236
    }
237
}
238