packages/sql-semantics-mysql/tests/Unit/Lowering/TableDefinition/Key/ReferenceRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\TableDefinition\Key;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Lowering\Lowering;
11use SqlSemantics\Platform\MySql\Lowering\TableDefinition\Key\ReferenceRule;
12use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
13use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
14use SqlSemantics\Platform\MySql\Platform;
15use SqlSemantics\Platform\MySql\Statement\Table\Key\ReferenceEvent;
16use SqlSemantics\Platform\MySql\Statement\Table\Key\ReferenceMatch;
17
18#[CoversClass(ReferenceRule::class)]
19#[Medium]
20final class ReferenceRuleTest extends TestCase
21{
22    public function testReferencesLowersTheClause(): void
23    {
24        $platform = new Platform();
25        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
26        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
27        $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT REFERENCES p (x) MATCH FULL)')->find('references')[0];
28
29        self::assertSame(ReferenceMatch::Full, (new ReferenceRule($lowering))->references($node)->match);
30    }
31
32    public function testOptionalLowersAnAbsentClause(): void
33    {
34        $platform = new Platform();
35        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
36        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
37        $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT)')->find('opt_references')[0];
38
39        self::assertNull((new ReferenceRule($lowering))->optional($node));
40    }
41
42    public function testColumnsLowersTheParentColumns(): void
43    {
44        $platform = new Platform();
45        $profile = $platform->profile('5.7.44', null, ParameterStyle::Native);
46        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
47        $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT REFERENCES p (x, y))')->find('opt_ref_list')[0];
48
49        self::assertCount(2, (new ReferenceRule($lowering))->columns($node) ?? []);
50    }
51
52    public function testActionsKeepsTheWrittenOrder(): void
53    {
54        $platform = new Platform();
55        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
56        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
57        $node = $platform->parser($profile)->parse('CREATE TABLE t (a INT REFERENCES p (x) ON DELETE CASCADE ON UPDATE SET NULL)')->find('opt_on_update_delete')[0];
58        $actions = (new ReferenceRule($lowering))->actions($node);
59
60        self::assertSame([ReferenceEvent::Delete, ReferenceEvent::Update], [$actions[0]->event, $actions[1]->event]);
61    }
62}
63