packages/sql-semantics-mysql/tests/Unit/Lowering/Dispatch/StatementRoutesTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Dispatch;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Lowering\Dispatch\Family;
11use SqlSemantics\Platform\MySql\Lowering\Dispatch\StatementRoutes;
12use SqlSemantics\Platform\MySql\Model\Lowering\Productions;
13
14#[CoversClass(StatementRoutes::class)]
15#[Medium]
16final class StatementRoutesTest extends TestCase
17{
18    public function testFamilyNamesTheOwnerOfAStatementRule(): void
19    {
20        $routes = new StatementRoutes();
21
22        self::assertSame(Family::Query, $routes->family('statement: select'));
23        self::assertSame(Family::Query, $routes->family('simple_statement: select_stmt'));
24        self::assertSame(Family::Definition, $routes->family('statement: create'));
25        self::assertSame(Family::Definition, $routes->family('simple_statement: create'));
26        self::assertSame(Family::TableDefinition, $routes->family('simple_statement: create_table_stmt'));
27        self::assertSame(Family::Replication, $routes->family('statement: slave'));
28        self::assertSame(Family::Account, $routes->family('simple_statement: set_role_stmt'));
29        self::assertSame(Family::Utility, $routes->family('statement: set'));
30        self::assertNull($routes->family('create: CREATE opt_table_options TABLE_SYM opt_if_not_exists table_ident create2'));
31        self::assertNull($routes->family('statement:'));
32    }
33
34    public function testFamilyRoutesEveryStatementAlternativeOfEveryRelease(): void
35    {
36        $directory = dirname(__DIR__, 4) . '/resources/productions/';
37        $paths = glob($directory . 'mysql-*.php');
38        self::assertIsArray($paths);
39        $signatures = array_merge(...array_map(static fn (string $path): array => Productions::load($path)->all(), $paths));
40        $statements = array_values(array_filter($signatures, static fn (string $signature): bool => str_starts_with($signature, 'statement: ') || str_starts_with($signature, 'simple_statement: ')));
41        $unrouted = array_values(array_filter($statements, static fn (string $signature): bool => (new StatementRoutes())->family($signature) === null));
42
43        self::assertCount(9, $paths);
44        self::assertGreaterThan(180, count(array_unique($statements)));
45        self::assertSame([], $unrouted);
46    }
47}
48