Passed
Pull Request — master (#363)
by
unknown
09:15
created

testWithHasUnclosedParenthesis()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 16
rs 9.8666
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\SqlParser\Tests\Parser;
6
7
use PhpMyAdmin\SqlParser\Components\WithKeyword;
8
use PhpMyAdmin\SqlParser\Lexer;
9
use PhpMyAdmin\SqlParser\Parser;
10
use PhpMyAdmin\SqlParser\Tests\TestCase;
11
use stdClass;
12
13
class WithStatementTest extends TestCase
14
{
15
    /**
16
     * @dataProvider parseWith
17
     */
18
    public function testParse(string $test): void
19
    {
20
        $this->runParserTest($test);
21
    }
22
23
    /**
24
     * @return array<int,array<int, string>>
25
     */
26
    public function parseWith(): array
27
    {
28
        return [
29
            ['parser/parseWithStatement'],
30
            ['parser/parseWithStatement1'],
31
            ['parser/parseWithStatement2'],
32
            ['parser/parseWithStatement3'],
33
            ['parser/parseWithStatement4'],
34
            ['parser/parseWithStatement5'],
35
            ['parser/parseWithStatement6'],
36
            ['parser/parseWithStatementErr'],
37
            ['parser/parseWithStatementErr1'],
38
            ['parser/parseWithStatementErr2'],
39
            ['parser/parseWithStatementErr3'],
40
            ['parser/parseWithStatementErr4'],
41
            ['parser/parseWithStatementErr5'],
42
            ['parser/parseWithStatementErr6'],
43
            ['parser/parseWithStatementErr7'],
44
            ['parser/parseWithStatementErr8'],
45
        ];
46
    }
47
48
    public function testWith(): void
49
    {
50
        $sql = <<<SQL
51
WITH categories(identifier, name, parent_id) AS (
52
    SELECT c.identifier, c.name, c.parent_id FROM category c WHERE c.identifier = 'a'
53
    UNION ALL
54
    SELECT c.identifier, c.name, c.parent_id FROM categories, category c WHERE c.identifier = categories.parent_id
55
), foo AS ( SELECT * FROM test )
56
SELECT * FROM categories
57
SQL;
58
59
        $lexer = new Lexer($sql);
60
61
        $lexerErrors = $this->getErrorsAsArray($lexer);
62
        $this->assertCount(0, $lexerErrors);
63
        $parser = new Parser($lexer->list);
64
        $parserErrors = $this->getErrorsAsArray($parser);
65
        $this->assertCount(0, $parserErrors);
66
        $this->assertCount(1, $parser->statements);
67
68
        // phpcs:disable Generic.Files.LineLength.TooLong
69
        $expected = <<<SQL
70
WITH categories(identifier, name, parent_id) AS (SELECT c.identifier, c.name, c.parent_id FROM category AS `c` WHERE c.identifier = 'a' UNION ALL SELECT c.identifier, c.name, c.parent_id FROM categories, category AS `c` WHERE c.identifier = categories.parent_id), foo AS (SELECT * FROM test)
71
SQL;
72
        // phpcs:enable
73
        $this->assertEquals($expected, $parser->statements[0]->build());
74
    }
75
76
    public function testWithHasErrors(): void
77
    {
78
        $sql = <<<SQL
79
WITH categories(identifier, name, parent_id) AS (
80
    SOMETHING * FROM foo
81
)
82
SELECT * FROM categories
83
SQL;
84
85
        $lexer = new Lexer($sql);
86
87
        $lexerErrors = $this->getErrorsAsArray($lexer);
88
        $this->assertCount(0, $lexerErrors);
89
        $parser = new Parser($lexer->list);
90
        $parserErrors = $this->getErrorsAsArray($parser);
91
        $this->assertCount(4, $parserErrors);
92
    }
93
94
    public function testWithEmbedParenthesis(): void
95
    {
96
        $sql = <<<SQL
97
WITH categories AS (
98
    SELECT * FROM (SELECT * FROM foo)
99
)
100
SELECT * FROM categories
101
SQL;
102
103
        $lexer = new Lexer($sql);
104
        $lexerErrors = $this->getErrorsAsArray($lexer);
105
        $this->assertCount(0, $lexerErrors);
106
        $parser = new Parser($lexer->list);
107
        $parserErrors = $this->getErrorsAsArray($parser);
108
        $this->assertCount(0, $parserErrors);
109
110
        // phpcs:disable Generic.Files.LineLength.TooLong
111
        $expected = <<<SQL
112
WITH categories AS (SELECT * FROM (SELECT * FROM foo))
113
SQL;
114
        // phpcs:enable
115
        $this->assertEquals($expected, $parser->statements[0]->build());
116
    }
117
118
    public function testWithHasUnclosedParenthesis(): void
119
    {
120
        $sql = <<<SQL
121
WITH categories(identifier, name, parent_id) AS (
122
    SELECT * FROM (SELECT * FROM foo
123
)
124
SELECT * FROM categories
125
SQL;
126
127
        $lexer = new Lexer($sql);
128
129
        $lexerErrors = $this->getErrorsAsArray($lexer);
130
        $this->assertCount(0, $lexerErrors);
131
        $parser = new Parser($lexer->list);
132
        $parserErrors = $this->getErrorsAsArray($parser);
133
        $this->assertEquals($parserErrors[0][0], 'A closing bracket was expected.');
134
    }
135
136
    public function testBuildWrongWithKeyword(): void
137
    {
138
        $this->expectExceptionMessage('Can not build a component that is not a WithKeyword');
139
        WithKeyword::build(new stdClass());
0 ignored issues
show
Bug introduced by
new stdClass() of type stdClass is incompatible with the type PhpMyAdmin\SqlParser\Components\WithKeyword expected by parameter $component of PhpMyAdmin\SqlParser\Com...ts\WithKeyword::build(). ( Ignorable by Annotation )

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

139
        WithKeyword::build(/** @scrutinizer ignore-type */ new stdClass());
Loading history...
140
    }
141
142
    public function testBuildBadWithKeyword(): void
143
    {
144
        $this->expectExceptionMessage('No statement inside WITH');
145
        WithKeyword::build(new WithKeyword('test'));
146
    }
147
}
148