Passed
Pull Request — master (#380)
by Wilmer
13:00 queued 10:01
created

QueryBuilderTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Importance

Changes 13
Bugs 4 Features 1
Metric Value
eloc 73
c 13
b 4
f 1
dl 0
loc 124
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testAddCommentOnColumn() 0 11 1
A testRenameTable() 0 12 1
A testAddCommentOnTable() 0 11 1
A testBuildFrom() 0 11 1
A testDropColumn() 0 12 1
A testDropCommentFromColumn() 0 12 1
A testCreateTable() 0 31 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Tests\QueryBuilder;
6
7
use Yiisoft\Db\Query\Query;
8
use Yiisoft\Db\Schema\SchemaBuilderTrait;
9
use Yiisoft\Db\Tests\AbstractQueryBuilderTest;
10
use Yiisoft\Db\Tests\Support\Assert;
11
use Yiisoft\Db\Tests\Support\TestTrait;
12
13
/**
14
 * @group db
15
 */
16
final class QueryBuilderTest extends AbstractQueryBuilderTest
17
{
18
    use SchemaBuilderTrait;
19
    use TestTrait;
20
21
    public function testAddCommentOnColumn(): void
22
    {
23
        $db = $this->getConnection();
24
25
        $qb = $db->getQueryBuilder();
26
27
        $this->assertSame(
28
            <<<SQL
29
            COMMENT ON COLUMN `customer`.`name` IS 'This is name'
30
            SQL,
31
            $qb->addCommentOnColumn('customer', 'name', 'This is name')
32
        );
33
    }
34
35
    public function testAddCommentOnTable(): void
36
    {
37
        $db = $this->getConnection();
38
39
        $qb = $db->getQueryBuilder();
40
41
        $this->assertSame(
42
            <<<SQL
43
            COMMENT ON TABLE `customer` IS 'Customer table'
44
            SQL,
45
            $qb->addCommentOnTable('customer', 'Customer table')
46
        );
47
    }
48
49
    /**
50
     *  @dataProvider \Yiisoft\Db\Tests\Provider\QueryBuilderProvider::fromCases()
51
     */
52
    public function testBuildFrom(mixed $table, string $expectedSql, array $expectedParams = []): void
53
    {
54
        $db = $this->getConnection();
55
56
        $qb = $db->getQueryBuilder();
57
        $query = (new Query($db))->from($table);
58
59
        [$sql, $params] = $qb->build($query);
60
61
        $this->assertSame($expectedSql, $sql);
62
        $this->assertSame($expectedParams, $params);
63
    }
64
65
    public function testCreateTable(): void
66
    {
67
        $this->db = $this->getConnection();
68
69
        $qb = $this->db->getQueryBuilder();
70
        $expected = <<<SQL
71
        CREATE TABLE `test_table` (
72
        \t`id` pk,
73
        \t`name` string(255) NOT NULL,
74
        \t`email` string(255) NOT NULL,
75
        \t`address` string(255) NOT NULL,
76
        \t`status` integer NOT NULL,
77
        \t`profile_id` integer NOT NULL,
78
        \t`created_at` timestamp NOT NULL,
79
        \t`updated_at` timestamp NOT NULL
80
        ) CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB
81
        SQL;
82
        $columns = [
83
            'id' => $this->primaryKey(5),
84
            'name' => $this->string(255)->notNull(),
85
            'email' => $this->string(255)->notNull(),
86
            'address' => $this->string(255)->notNull(),
87
            'status' => $this->integer()->notNull(),
88
            'profile_id' => $this->integer()->notNull(),
89
            'created_at' => $this->timestamp()->notNull(),
90
            'updated_at' => $this->timestamp()->notNull(),
91
        ];
92
        $options = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
93
        $sql = $qb->createTable('test_table', $columns, $options);
94
95
        Assert::equalsWithoutLE($expected, $sql);
96
    }
97
98
    public function testDropColumn(): void
99
    {
100
        $db = $this->getConnection();
101
102
        $qb = $db->getQueryBuilder();
103
        $sql = $qb->dropColumn('test_table', 'test_column');
104
105
        $this->assertSame(
106
            <<<SQL
107
            ALTER TABLE `test_table` DROP COLUMN `test_column`
108
            SQL,
109
            $sql,
110
        );
111
    }
112
113
    public function testDropCommentFromColumn(): void
114
    {
115
        $db = $this->getConnection();
116
117
        $qb = $db->getQueryBuilder();
118
        $sql = $qb->dropCommentFromColumn('test_table', 'test_column');
119
120
        $this->assertSame(
121
            <<<SQL
122
            COMMENT ON COLUMN `test_table`.`test_column` IS NULL
123
            SQL,
124
            $sql,
125
        );
126
    }
127
128
    public function testRenameTable(): void
129
    {
130
        $db = $this->getConnection();
131
132
        $qb = $db->getQueryBuilder();
133
        $sql = $qb->renameTable('table_from', 'table_to');
134
135
        $this->assertSame(
136
            <<<SQL
137
            RENAME TABLE `table_from` TO `table_to`
138
            SQL,
139
            $sql,
140
        );
141
    }
142
}
143