Passed
Push — master ( cee3e6...16d2f0 )
by William
11:48 queued 12s
created

testBuilderWithCommentsOnOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Tests\Builder;
6
7
use PhpMyAdmin\SqlParser\Parser;
8
use PhpMyAdmin\SqlParser\Tests\TestCase;
9
10
class AlterStatementTest extends TestCase
11
{
12
    public function testBuilder(): void
13
    {
14
        $query = 'ALTER TABLE `actor` ' .
15
            'ADD PRIMARY KEY (`actor_id`), ' .
16
            'ADD KEY `idx_actor_last_name` (`last_name`)';
17
18
        $parser = new Parser($query);
19
        $stmt = $parser->statements[0];
20
21
        $this->assertEquals($query, $stmt->build());
22
    }
23
24
    public function testBuilderWithExpression(): void
25
    {
26
        $query = 'ALTER TABLE `table` '
27
                . 'ADD UNIQUE KEY `functional_index`'
28
                . ' (`field1`,`field2`, (IFNULL(`field3`,0)))';
29
30
        $parser = new Parser($query);
31
        $stmt = $parser->statements[0];
32
33
        $this->assertEquals($query, $stmt->build());
34
    }
35
36
    public function testBuilderWithComments(): void
37
    {
38
        $query = 'ALTER /* comment */ TABLE `actor` ' .
39
            'ADD PRIMARY KEY (`actor_id`), -- comment at the end of the line' . "\n" .
40
            'ADD KEY `idx_actor_last_name` (`last_name`) -- and that is the last comment.';
41
42
        $expectedQuery = 'ALTER TABLE `actor` ' .
43
            'ADD PRIMARY KEY (`actor_id`), ' .
44
            'ADD KEY `idx_actor_last_name` (`last_name`)';
45
46
        $parser = new Parser($query);
47
        $stmt = $parser->statements[0];
48
49
        $this->assertEquals($expectedQuery, $stmt->build());
50
    }
51
52
    public function testBuilderWithCommentsOnOptions(): void
53
    {
54
        $query = 'ALTER EVENT `myEvent` /* comment */ ' .
55
            'ON SCHEDULE -- Comment at the end of the line' . "\n" .
56
            'AT "2023-01-01 01:23:45"';
57
58
        $expectedQuery = 'ALTER EVENT `myEvent` ' .
59
            'ON SCHEDULE AT "2023-01-01 01:23:45"';
60
61
        $parser = new Parser($query);
62
        $stmt = $parser->statements[0];
63
64
        $this->assertEquals($expectedQuery, $stmt->build());
65
    }
66
67
    public function testBuilderCompressed(): void
68
    {
69
        $query = 'ALTER TABLE `user` CHANGE `message` `message` TEXT COMPRESSED';
70
        $parser = new Parser($query);
71
        $stmt = $parser->statements[0];
72
        $this->assertEquals($query, $stmt->build());
73
    }
74
75
    public function testBuilderPartitions(): void
76
    {
77
        $parser = new Parser('ALTER TABLE t1 PARTITION BY HASH(id) PARTITIONS 8');
78
        $stmt = $parser->statements[0];
79
80
        $this->assertEquals('ALTER TABLE t1 PARTITION BY  HASH(id) PARTITIONS 8', $stmt->build());
81
82
        $parser = new Parser('ALTER TABLE t1 ADD PARTITION (PARTITION p3 VALUES LESS THAN (2002))');
83
        $stmt = $parser->statements[0];
84
85
        $this->assertEquals(
86
            "ALTER TABLE t1 ADD PARTITION (\n" .
87
            "PARTITION p3 VALUES LESS THAN (2002)\n" .
88
            ')',
89
            $stmt->build()
90
        );
91
92
        $parser = new Parser('ALTER TABLE p PARTITION BY LINEAR KEY ALGORITHM=2 (id) PARTITIONS 32;');
93
        $stmt = $parser->statements[0];
94
95
        $this->assertEquals(
96
            'ALTER TABLE p PARTITION BY  LINEAR KEY ALGORITHM=2 (id) PARTITIONS 32',
97
            $stmt->build()
98
        );
99
100
        $parser = new Parser('ALTER TABLE t1 DROP PARTITION p0, p1;');
101
        $stmt = $parser->statements[0];
102
103
        $this->assertEquals(
104
            'ALTER TABLE t1 DROP PARTITION  p0, p1',
105
            $stmt->build()
106
        );
107
    }
108
109
    public function testBuilderEventWithDefiner(): void
110
    {
111
        $query = 'ALTER DEFINER=user EVENT myEvent ENABLE';
112
        $parser = new Parser($query);
113
        $stmt = $parser->statements[0];
114
        $this->assertEquals($query, $stmt->build());
115
    }
116
}
117