packages/sql-parser/tests/Unit/MySql/Source/LexHeaderTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\MySql\Source;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use RuntimeException;
11use SqlParser\MySql\Source\LexHeader;
12
13#[CoversClass(LexHeader::class)]
14#[Small]
15final class LexHeaderTest extends TestCase
16{
17    public function testParseReadsTheModernSpelling(): void
18    {
19        $source = "static const SYMBOL symbols[] = {\n    {SYM(\"&&\", AND_AND_SYM)},\n    {SYM_FN(\"ADDDATE\", ADDDATE_SYM)},\n    {SYM_HK(\"SELECT\", SELECT_SYM)},\n    {SYM_H(\"BKA\", BKA_HINT)},\n    {SYM(\"SOURCE_CONNECTION_AUTO_FAILOVER\",\n         SOURCE_CONNECTION_AUTO_FAILOVER_SYM)},\n};";
20        $tables = (new LexHeader())->parse($source);
21
22        self::assertSame(['&&' => 'AND_AND_SYM', 'SELECT' => 'SELECT_SYM', 'SOURCE_CONNECTION_AUTO_FAILOVER' => 'SOURCE_CONNECTION_AUTO_FAILOVER_SYM'], $tables['keywords']);
23        self::assertSame(['ADDDATE' => 'ADDDATE_SYM'], $tables['functions']);
24    }
25
26    public function testParseReadsTheLegacySpelling(): void
27    {
28        $source = "static SYMBOL symbols[] = {\n  { \"select\",\t\tSYM(SELECT_SYM)},\n};\nstatic SYMBOL sql_functions[] = {\n  { \"COUNT\",\t\tSYM(COUNT_SYM)},\n};";
29        $tables = (new LexHeader())->parse($source);
30
31        self::assertSame(['SELECT' => 'SELECT_SYM'], $tables['keywords']);
32        self::assertSame(['COUNT' => 'COUNT_SYM'], $tables['functions']);
33    }
34
35    public function testParseRejectsAFileWithoutKeywords(): void
36    {
37        $this->expectException(RuntimeException::class);
38
39        (new LexHeader())->parse('int x;');
40    }
41
42    public function testEntries(): void
43    {
44        $entries = (new LexHeader())->entries('{SYM_FN("x", X_SYM)} { "y", SYM(Y_SYM)}');
45
46        self::assertSame([['SYM_FN', 'X', 'X_SYM', 0], ['SYM', 'Y', 'Y_SYM', 21]], $entries);
47    }
48}
49