|
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\AbstractSyntaxTree\SqlAstJoin; |
|
17
|
|
|
use Addiks\StoredSQL\AbstractSyntaxTree\SqlAstNode; |
|
18
|
|
|
use Addiks\StoredSQL\AbstractSyntaxTree\SqlAstSelect; |
|
19
|
|
|
use Addiks\StoredSQL\Parsing\SqlParser; |
|
20
|
|
|
use Addiks\StoredSQL\Parsing\SqlParserClass; |
|
21
|
|
|
use PHPUnit\Framework\TestCase; |
|
22
|
|
|
use Addiks\StoredSQL\AbstractSyntaxTree\SqlAstWhere; |
|
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
|
|
|
|
|
57
|
|
|
/** @var SqlAstSelect $select */ |
|
58
|
|
|
$select = $detectedContent[0]; |
|
59
|
|
|
|
|
60
|
|
|
$this->assertTrue($select instanceof SqlAstSelect); |
|
61
|
|
|
|
|
62
|
|
|
/** @var string $regeneratedSql */ |
|
63
|
|
|
$regeneratedSql = $select->toSql(); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
#var_dump($regeneratedSql); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** @test */ |
|
69
|
|
|
public function shouldParseConditionalSql(): void |
|
70
|
|
|
{ |
|
71
|
|
|
/** @var SqlParser $parser */ |
|
72
|
|
|
$parser = SqlParserClass::defaultParser(); |
|
73
|
|
|
|
|
74
|
|
|
try { |
|
75
|
|
|
/** @var array<SqlAstNode> $detectedContent */ |
|
76
|
|
|
$detectedContent = $parser->parseSql(" |
|
77
|
|
|
LEFT JOIN files f ON(u.id = f.owner_id) |
|
78
|
|
|
WHERE f.name LIKE '%.pdf' |
|
79
|
|
|
AND f.type = 'symbolic' |
|
80
|
|
|
OR f.foo IS NULL |
|
81
|
|
|
", [SqlAstJoin::class, SqlAstWhere::class]); |
|
82
|
|
|
|
|
83
|
|
|
} catch (UnparsableSqlException $exception) { |
|
84
|
|
|
echo $exception->asciiLocationDump(); |
|
85
|
|
|
|
|
86
|
|
|
throw $exception; |
|
87
|
|
|
|
|
88
|
|
|
} catch (UnlexableSqlException $exception) { |
|
89
|
|
|
echo $exception->asciiLocationDump(); |
|
90
|
|
|
|
|
91
|
|
|
throw $exception; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$this->assertEquals(2, count($detectedContent)); |
|
95
|
|
|
$this->assertTrue($detectedContent[0] instanceof SqlAstJoin); |
|
96
|
|
|
$this->assertTrue($detectedContent[1] instanceof SqlAstWhere); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|