packages/sql-semantics-mysql/tests/Unit/Lowering/Dispatch/DefinitionRoutesTest.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\DefinitionRoutes;
11use SqlSemantics\Platform\MySql\Lowering\Dispatch\Family;
12use SqlSemantics\Platform\MySql\Model\Lowering\Productions;
13
14#[CoversClass(DefinitionRoutes::class)]
15#[Medium]
16final class DefinitionRoutesTest extends TestCase
17{
18    public function testFamilyRoutesByTheWholeLeadingSymbols(): void
19    {
20        $routes = new DefinitionRoutes();
21
22        self::assertSame(Family::TableDefinition, $routes->family('create: CREATE opt_table_options TABLE_SYM opt_if_not_exists table_ident create2'));
23        self::assertSame(Family::Definition, $routes->family('create: CREATE view_or_trigger_or_sp_or_event'));
24        self::assertSame(Family::Server, $routes->family('create: CREATE TABLESPACE_SYM tablespace_info'));
25        self::assertSame(Family::Server, $routes->family('create: CREATE TABLESPACE tablespace_info'));
26        self::assertSame(Family::Account, $routes->family('alter: alter_user_command user_func IDENTIFIED_SYM BY TEXT_STRING'));
27        self::assertSame(Family::TableChange, $routes->family('drop: DROP opt_temporary table_or_tables if_exists table_list opt_restrict'));
28        self::assertSame(Family::Routine, $routes->family('drop: DROP TRIGGER_SYM if_exists sp_name'));
29        self::assertNull($routes->family('drop: DROP TRIGGERS_SYM if_exists sp_name'));
30        self::assertNull($routes->family('statement: create'));
31    }
32
33    public function testFamilyRoutesEveryCreateAlterAndDropAlternativeOfEveryRelease(): void
34    {
35        $directory = dirname(__DIR__, 4) . '/resources/productions/';
36        $paths = glob($directory . 'mysql-*.php');
37        self::assertIsArray($paths);
38        $signatures = array_merge(...array_map(static fn (string $path): array => Productions::load($path)->all(), $paths));
39        $definitions = array_values(array_filter($signatures, static fn (string $signature): bool => str_starts_with($signature, 'create: ') || str_starts_with($signature, 'alter: ') || str_starts_with($signature, 'drop: ')));
40        $unrouted = array_values(array_filter($definitions, static fn (string $signature): bool => (new DefinitionRoutes())->family($signature) === null));
41
42        self::assertGreaterThan(50, count(array_unique($definitions)));
43        self::assertSame([], $unrouted);
44    }
45}
46