packages/sql-semantics-mysql/tests/Unit/Rules/Routine/RowAliasesTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Routine;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Contract\AnalysisContext;
11use SqlSemantics\Platform\MySql\Model\Statement\Fact\Diagnostic;
12use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Comparison;
13use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
14use SqlSemantics\Platform\MySql\Model\Statement\Reference\Column\ResolvedColumn;
15use SqlSemantics\Platform\MySql\Rules\Routine\RowAliases;
16use SqlSemantics\Platform\MySql\Semantics;
17use SqlSemantics\Platform\MySql\Statement\Expression\Operator\Arithmetic;
18use SqlSemantics\Platform\MySql\Statement\Routine\CreateTrigger;
19use SqlSemantics\Platform\MySql\Statement\Utility\Set\NameAssignment;
20use SqlSemantics\Platform\MySql\Statement\Utility\Set\SetVariables;
21
22#[CoversClass(RowAliases::class)]
23#[Medium]
24final class RowAliasesTest extends TestCase
25{
26    public function testVisibleResolvesTheOldRowToTheTriggerTable(): void
27    {
28        $semantics = new Semantics();
29        $table = $semantics->inspect('CREATE TABLE t (a INT)');
30        $operation = $semantics->inspect('CREATE TRIGGER tr BEFORE UPDATE ON t FOR EACH ROW SET NEW.a = OLD.a + 1', [$table]);
31        $trigger = $operation->statement;
32        self::assertInstanceOf(CreateTrigger::class, $trigger);
33        $set = $trigger->body;
34        self::assertInstanceOf(SetVariables::class, $set);
35        $item = $set->items[0];
36        self::assertInstanceOf(NameAssignment::class, $item);
37        $value = $item->value;
38        self::assertInstanceOf(Arithmetic::class, $value);
39        $resolution = $operation->facts->scalar($value->left)->resolution;
40
41        self::assertInstanceOf(ResolvedColumn::class, $resolution);
42        self::assertSame($trigger->table, $resolution->relation);
43        self::assertSame('a', $resolution->declaration()?->name->value);
44        self::assertSame([], $operation->facts->diagnostics);
45    }
46
47    public function testVisibleMakesBothRowsVisibleInAnUpdateTriggerUnderAnySpelling(): void
48    {
49        $semantics = new Semantics();
50        $table = $semantics->inspect('CREATE TABLE t (a INT)');
51
52        self::assertSame([], $semantics->inspect('CREATE TRIGGER tr AFTER UPDATE ON t FOR EACH ROW SET @x = OLD.a + New.a + nEw.a + old.a', [$table])->facts->diagnostics);
53    }
54
55    public function testVisibleHasNoOldRowInAnInsertTrigger(): void
56    {
57        $semantics = new Semantics();
58        $table = $semantics->inspect('CREATE TABLE t (a INT)');
59        $operation = $semantics->inspect('CREATE TRIGGER tr AFTER INSERT ON t FOR EACH ROW SET @x = NEW.a + OLD.a', [$table]);
60
61        self::assertSame(['Column OLD.a does not exist.'], array_map(static fn (Diagnostic $problem): string => $problem->message(), $operation->facts->diagnostics));
62    }
63
64    public function testVisibleHasNoNewRowInADeleteTrigger(): void
65    {
66        $semantics = new Semantics();
67        $table = $semantics->inspect('CREATE TABLE t (a INT)');
68        $operation = $semantics->inspect('CREATE TRIGGER tr AFTER DELETE ON t FOR EACH ROW SET @x = OLD.a + NEW.a', [$table]);
69
70        self::assertSame(['Column NEW.a does not exist.'], array_map(static fn (Diagnostic $problem): string => $problem->message(), $operation->facts->diagnostics));
71    }
72
73    public function testVisibleReachesNoColumnWithoutTheQualifier(): void
74    {
75        $semantics = new Semantics();
76        $table = $semantics->inspect('CREATE TABLE t (a INT)');
77        $operation = $semantics->inspect('CREATE TRIGGER tr AFTER INSERT ON t FOR EACH ROW SET @x = a + 1', [$table]);
78
79        self::assertSame(['Column a does not exist.'], array_map(static fn (Diagnostic $problem): string => $problem->message(), $operation->facts->diagnostics));
80    }
81
82    public function testSpellingsListsEveryLetterCaseUnderCaseSensitiveNames(): void
83    {
84        $context = new AnalysisContext((new Semantics())->profile(), [new Name('db')]);
85
86        self::assertSame(['NEW', 'nEW', 'NeW', 'neW', 'NEw', 'nEw', 'New', 'new'], (new RowAliases())->spellings('NEW', $context));
87    }
88
89    public function testSpellingsKeepsOneSpellingUnderCaseInsensitiveNames(): void
90    {
91        $context = new AnalysisContext((new Semantics())->profile(), [new Name('db')], [], true, Comparison::AsciiInsensitive);
92
93        self::assertSame(['OLD'], (new RowAliases())->spellings('OLD', $context));
94    }
95}
96