Test Failed
Push — master ( 3fa183...47fb36 )
by Gerrit
02:37
created

ParseSqlTest::shouldParseSomeSql()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 17
rs 10
1
<?php
2
/**
3
 * Copyright (C) 2019  Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 *
8
 * @license GPL-3.0
9
 * @author Gerrit Addiks <[email protected]>
10
 */
11
12
namespace Addiks\StoredSQL\Tests\Behaviour;
13
14
use Addiks\StoredSQL\Exception\UnlexableSqlException;
15
use Addiks\StoredSQL\Exception\UnparsableSqlException;
16
use Addiks\StoredSQL\Parsing\AbstractSyntaxTree\SqlAstJoin;
17
use Addiks\StoredSQL\Parsing\AbstractSyntaxTree\SqlAstNode;
18
use Addiks\StoredSQL\Parsing\AbstractSyntaxTree\SqlAstSelect;
19
use Addiks\StoredSQL\Parsing\AbstractSyntaxTree\SqlAstWhereCondition;
20
use Addiks\StoredSQL\Parsing\SqlParser;
21
use Addiks\StoredSQL\Parsing\SqlParserClass;
22
use PHPUnit\Framework\TestCase;
23
24
final class ParseSqlTest extends TestCase
25
{
26
    /** @test */
27
    public function shouldParseSelectSql(): void
28
    {
29
        /** @var SqlParser $parser */
30
        $parser = SqlParserClass::defaultParser();
31
32
        try {
33
            /** @var array<SqlAstNode> $detectedContent */
34
            $detectedContent = $parser->parseSql("
35
                SELECT u.name, u.email, f.name, f.size
36
                FROM users u
37
                LEFT JOIN files f ON(u.id = f.owner_id)
38
                WHERE f.name LIKE '%.pdf'
39
                AND f.type = 'symbolic'
40
                OR f.foo IS NULL
41
                ORDER BY f.size DESC, f.owner ASC
42
            ", [SqlAstSelect::class]);
43
44
        } catch (UnparsableSqlException $exception) {
45
            echo $exception->asciiLocationDump();
46
47
            throw $exception;
48
49
        } catch (UnlexableSqlException $exception) {
50
            echo $exception->asciiLocationDump();
51
52
            throw $exception;
53
        }
54
55
        $this->assertEquals(1, count($detectedContent));
56
        $this->assertTrue($detectedContent[0] instanceof SqlAstSelect);
57
    }
58
59
    /** @test */
60
    public function shouldParseConditionalSql(): void
61
    {
62
        /** @var SqlParser $parser */
63
        $parser = SqlParserClass::defaultParser();
64
65
        try {
66
            /** @var array<SqlAstNode> $detectedContent */
67
            $detectedContent = $parser->parseSql("
68
                LEFT JOIN files f ON(u.id = f.owner_id)
69
                WHERE f.name LIKE '%.pdf'
70
                AND f.type = 'symbolic'
71
                OR f.foo IS NULL
72
            ", [SqlAstJoin::class, SqlAstWhereCondition::class]);
73
74
        } catch (UnparsableSqlException $exception) {
75
            echo $exception->asciiLocationDump();
76
77
            throw $exception;
78
79
        } catch (UnlexableSqlException $exception) {
80
            echo $exception->asciiLocationDump();
81
82
            throw $exception;
83
        }
84
85
        $this->assertEquals(2, count($detectedContent));
86
        $this->assertTrue($detectedContent[0] instanceof SqlAstJoin);
87
        $this->assertTrue($detectedContent[1] instanceof SqlAstWhereCondition);
88
    }
89
}
90