Passed
Pull Request — master (#334)
by Antoine
12:37
created

WithStatementTest::testBuildWrongWithKeyword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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\Tests\TestCase;
9
use PhpMyAdmin\SqlParser\Lexer;
10
use PhpMyAdmin\SqlParser\Parser;
11
use stdClass;
12
13
class WithStatementTest extends TestCase
14
{
15
    public function testWith(): void
16
    {
17
        $sql = <<<SQL
18
WITH categories(identifier, name, parent_id) AS (
19
    SELECT c.identifier, c.name, c.parent_id FROM category c WHERE c.identifier = 'a'
20
    UNION ALL
21
    SELECT c.identifier, c.name, c.parent_id FROM categories, category c WHERE c.identifier = categories.parent_id
22
), foo AS ( SELECT * FROM test )
23
SELECT * FROM categories
24
SQL;
25
26
        $lexer = new Lexer($sql);
27
28
        $lexerErrors = $this->getErrorsAsArray($lexer);
29
        $this->assertCount(0, $lexerErrors);
30
        $parser = new Parser($lexer->list);
31
        $parserErrors = $this->getErrorsAsArray($parser);
32
        $this->assertCount(0, $parserErrors);
33
        $this->assertCount(2, $parser->statements);
34
35
        // phpcs:disable Generic.Files.LineLength.TooLong
36
        $expected = <<<SQL
37
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)
38
SQL;
39
        // phpcs:enable
40
        $this->assertEquals($expected, $parser->statements[0]->build());
41
        $this->assertEquals('SELECT * FROM categories', $parser->statements[1]->build());
42
    }
43
44
    public function testWithHasErrors(): void
45
    {
46
        $sql = <<<SQL
47
WITH categories(identifier, name, parent_id) AS (
48
    SOMETHING * FROM foo
49
)
50
SELECT * FROM categories
51
SQL;
52
53
        $lexer = new Lexer($sql);
54
55
        $lexerErrors = $this->getErrorsAsArray($lexer);
56
        $this->assertCount(0, $lexerErrors);
57
        $parser = new Parser($lexer->list);
58
        $parserErrors = $this->getErrorsAsArray($parser);
59
        $this->assertCount(2, $parserErrors);
60
    }
61
62
    public function testWithEmbedParenthesis(): void
63
    {
64
        $sql = <<<SQL
65
WITH categories AS (
66
    SELECT * FROM (SELECT * FROM foo)
67
)
68
SELECT * FROM categories
69
SQL;
70
71
        $lexer = new Lexer($sql);
72
        $lexerErrors = $this->getErrorsAsArray($lexer);
73
        $this->assertCount(0, $lexerErrors);
74
        $parser = new Parser($lexer->list);
75
        $parserErrors = $this->getErrorsAsArray($parser);
76
        $this->assertCount(0, $parserErrors);
77
78
        // phpcs:disable Generic.Files.LineLength.TooLong
79
        $expected = <<<SQL
80
WITH categories AS (SELECT * FROM (SELECT * FROM foo))
81
SQL;
82
        // phpcs:enable
83
        $this->assertEquals($expected, $parser->statements[0]->build());
84
    }
85
86
    public function testWithHasUnclosedParenthesis(): void
87
    {
88
        $sql = <<<SQL
89
WITH categories(identifier, name, parent_id) AS (
90
    SELECT * FROM (SELECT * FROM foo
91
)
92
SELECT * FROM categories
93
SQL;
94
95
        $lexer = new Lexer($sql);
96
97
        $lexerErrors = $this->getErrorsAsArray($lexer);
98
        $this->assertCount(0, $lexerErrors);
99
        $parser = new Parser($lexer->list);
100
        $parserErrors = $this->getErrorsAsArray($parser);
101
        $this->assertEquals($parserErrors[0][0], 'A closing bracket was expected.');
102
    }
103
104
    public function testBuildWrongWithKeyword(): void
105
    {
106
        $this->expectExceptionMessage('Can not build a component that is not a WithKeyword');
107
        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

107
        WithKeyword::build(/** @scrutinizer ignore-type */ new stdClass());
Loading history...
108
    }
109
}
110