packages/sql-semantics-mysql/tests/Unit/Rules/Noise/LeafNoiseTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Noise;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Lowering\Productions;
11use SqlSemantics\Platform\MySql\Rules\Noise\LeafNoise;
12
13#[CoversClass(LeafNoise::class)]
14#[Medium]
15final class LeafNoiseTest extends TestCase
16{
17    public function testPositionsDeclareTheOptionalLeafKeywordsAsNoise(): void
18    {
19        self::assertSame([0], LeafNoise::positions()['opt_as: AS']);
20        self::assertSame([0], LeafNoise::positions()['equal: EQ']);
21        self::assertSame([0], LeafNoise::positions()['equal: SET_VAR']);
22        self::assertSame([0], LeafNoise::positions()['opt_default: DEFAULT']);
23        self::assertSame([0], LeafNoise::positions()['opt_default: DEFAULT_SYM']);
24        self::assertSame([0, 1], LeafNoise::positions()['opt_wild: . *']);
25        self::assertSame([0], LeafNoise::positions()['opt_comma: ,']);
26        self::assertSame([0], LeafNoise::positions()['opt_storage: STORAGE_SYM']);
27        self::assertSame([0], LeafNoise::positions()['opt_table: TABLE_SYM']);
28        self::assertSame([0], LeafNoise::positions()['table_ident: . ident']);
29        self::assertSame([0], LeafNoise::positions()['field_ident: . ident']);
30        self::assertSame([1], LeafNoise::positions()['charset: CHAR_SYM SET']);
31        self::assertSame([1], LeafNoise::positions()['character_set: CHAR_SYM SET_SYM']);
32        self::assertCount(13, LeafNoise::positions());
33        self::assertArrayNotHasKey('optional_braces: ( )', LeafNoise::positions());
34        self::assertArrayNotHasKey('simple_ident_q: . ident . ident', LeafNoise::positions());
35        self::assertArrayNotHasKey('table_ident: ident', LeafNoise::positions());
36        self::assertArrayNotHasKey('charset: CHARSET', LeafNoise::positions());
37    }
38
39    public function testPositionsNameOnlyTerminalPositionsOfProductionsOfTheGrammars(): void
40    {
41        $productions = array_merge(
42            Productions::load(dirname(__DIR__, 4) . '/resources/productions/mysql-5.7.44.php')->all(),
43            Productions::load(dirname(__DIR__, 4) . '/resources/productions/mysql-8.0.44.php')->all(),
44        );
45        $arity = array_map(static fn (string $signature): int => count(explode(' ', trim(substr($signature, (int) strpos($signature, ':') + 1)))), array_combine(array_keys(LeafNoise::positions()), array_keys(LeafNoise::positions())));
46
47        self::assertSame([], array_values(array_diff(array_keys(LeafNoise::positions()), $productions)));
48        self::assertSame([], array_filter(LeafNoise::positions(), static fn (array $positions, string $signature): bool => max(-1, ...$positions) >= $arity[$signature], ARRAY_FILTER_USE_BOTH));
49    }
50
51    public function testSynonymsMergeTheTerminalsTheManualDefinesAsSynonyms(): void
52    {
53        self::assertSame([0 => 'CHARSET'], LeafNoise::synonyms()['charset: CHAR_SYM SET']);
54        self::assertSame([0 => 'CHARSET'], LeafNoise::synonyms()['character_set: CHAR_SYM SET_SYM']);
55        self::assertSame([0 => 'INDEX_SYM'], LeafNoise::synonyms()['key_or_index: KEY_SYM']);
56        self::assertSame([0 => 'INDEXES'], LeafNoise::synonyms()['keys_or_index: KEYS']);
57        self::assertSame([0 => 'INDEXES'], LeafNoise::synonyms()['keys_or_index: INDEX_SYM']);
58        self::assertSame([0 => 'TABLE_SYM'], LeafNoise::synonyms()['table_or_tables: TABLES']);
59        self::assertSame([0 => 'NOT_SYM'], LeafNoise::synonyms()['not: NOT2_SYM']);
60        self::assertSame([0 => 'NO_WRITE_TO_BINLOG'], LeafNoise::synonyms()['opt_no_write_to_binlog: LOCAL_SYM']);
61        self::assertSame([0 => 'SESSION_SYM'], LeafNoise::synonyms()['opt_var_ident_type: LOCAL_SYM .']);
62        self::assertSame([0 => 'SESSION_SYM'], LeafNoise::synonyms()['opt_rvalue_system_variable_type: LOCAL_SYM .']);
63        self::assertSame([0 => 'SESSION_SYM'], LeafNoise::synonyms()['opt_set_var_ident_type: LOCAL_SYM .']);
64        self::assertSame([0 => 'name:default'], LeafNoise::synonyms()['lvalue_variable: DEFAULT_SYM . ident']);
65        self::assertSame([0 => 'name:binary'], LeafNoise::synonyms()['charset_name: BINARY']);
66        self::assertSame([0 => 'name:binary'], LeafNoise::synonyms()['charset_name: BINARY_SYM']);
67        self::assertSame([0 => 'name:binary'], LeafNoise::synonyms()['old_or_new_charset_name: BINARY']);
68        self::assertSame([0 => 'name:binary'], LeafNoise::synonyms()['old_or_new_charset_name: BINARY_SYM']);
69        self::assertSame([0 => 'name:binary'], LeafNoise::synonyms()['collation_name: BINARY_SYM']);
70        self::assertCount(17, LeafNoise::synonyms());
71        self::assertArrayNotHasKey('key_or_index: INDEX_SYM', LeafNoise::synonyms());
72        self::assertArrayNotHasKey('not: NOT_SYM', LeafNoise::synonyms());
73    }
74
75    public function testSynonymsNameOnlyTerminalPositionsOfProductionsOfTheGrammars(): void
76    {
77        $productions = array_merge(
78            Productions::load(dirname(__DIR__, 4) . '/resources/productions/mysql-5.7.44.php')->all(),
79            Productions::load(dirname(__DIR__, 4) . '/resources/productions/mysql-8.0.44.php')->all(),
80        );
81        $arity = array_map(static fn (string $signature): int => count(explode(' ', trim(substr($signature, (int) strpos($signature, ':') + 1)))), array_combine(array_keys(LeafNoise::synonyms()), array_keys(LeafNoise::synonyms())));
82
83        self::assertSame([], array_values(array_diff(array_keys(LeafNoise::synonyms()), $productions)));
84        self::assertSame([], array_filter(LeafNoise::synonyms(), static fn (array $keys, string $signature): bool => max(-1, ...array_keys($keys)) >= $arity[$signature], ARRAY_FILTER_USE_BOTH));
85    }
86}
87