packages/sql-semantics-mysql/tests/Unit/Rules/TableChange/ColumnChangesTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\TableChange;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Statement\Fact\Diagnostic;
11use SqlSemantics\Platform\MySql\Model\Statement\Shape\OutputSlot;
12use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
13use SqlSemantics\Platform\MySql\Rules\TableChange\ColumnChanges;
14use SqlSemantics\Platform\MySql\Semantics;
15
16#[CoversClass(ColumnChanges::class)]
17#[Medium]
18final class ColumnChangesTest extends TestCase
19{
20    public function testApplyAnswersTheShapeAfterTheChange(): void
21    {
22        $semantics = new Semantics();
23        $table = $semantics->inspect('CREATE TABLE t (a INT, b INT)');
24        $alter = $semantics->inspect('ALTER TABLE t ADD c INT AFTER a, DROP b', [$table]);
25
26        self::assertSame(['a', 'c'], array_map(static fn (OutputSlot $slot): ?string => $slot->name?->value, $alter->facts->relation($alter->statement)->shape->slots));
27    }
28
29    public function testCommandReportsNothingForAnUndeclaredTable(): void
30    {
31        $semantics = new Semantics();
32
33        self::assertSame([], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('ALTER TABLE t DROP x, ADD a INT, ADD a INT')->facts->diagnostics));
34    }
35
36    public function testChangeReportsAnAbsentColumn(): void
37    {
38        $semantics = new Semantics();
39        $table = $semantics->inspect('CREATE TABLE t (a INT, b INT)');
40
41        self::assertSame(['Column x does not exist in the table.'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('ALTER TABLE t CHANGE x y INT, MODIFY a INT', [$table])->facts->diagnostics));
42    }
43
44    public function testDropReportsAColumnDroppedTwice(): void
45    {
46        $semantics = new Semantics();
47        $table = $semantics->inspect('CREATE TABLE t (a INT, b INT)');
48
49        self::assertSame(['Column a does not exist in the table.'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('ALTER TABLE t DROP a, DROP a', [$table])->facts->diagnostics));
50    }
51
52    public function testRenameReportsAnAbsentColumn(): void
53    {
54        $semantics = new Semantics();
55        $table = $semantics->inspect('CREATE TABLE t (a INT, b INT)');
56
57        self::assertSame(['Column x does not exist in the table.'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('ALTER TABLE t RENAME COLUMN x TO y', [$table])->facts->diagnostics));
58    }
59
60    public function testAlteredAcceptsAnAddedColumn(): void
61    {
62        $semantics = new Semantics();
63        $table = $semantics->inspect('CREATE TABLE t (a INT, b INT)');
64
65        self::assertSame(['Column x does not exist in the table.'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('ALTER TABLE t ADD c INT, ALTER c SET DEFAULT 1, ALTER x DROP DEFAULT', [$table])->facts->diagnostics));
66    }
67
68    public function testUseReportsAColumnAnEarlierActionUsed(): void
69    {
70        $semantics = new Semantics();
71        $table = $semantics->inspect('CREATE TABLE t (a INT, b INT)');
72
73        self::assertSame(['Column a does not exist in the table.'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('ALTER TABLE t CHANGE a c INT, DROP a', [$table])->facts->diagnostics));
74    }
75
76    public function testFindComparesWithoutRegardToCase(): void
77    {
78        $semantics = new Semantics();
79        $table = $semantics->inspect('CREATE TABLE t (a INT, b INT)');
80
81        self::assertSame([], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('ALTER TABLE t DROP A, ALTER B DROP DEFAULT', [$table])->facts->diagnostics));
82    }
83
84    public function testIndexAnswersNullForNoColumn(): void
85    {
86        self::assertNull((new ColumnChanges())->index(0));
87    }
88
89    public function testInsertPlacesFirst(): void
90    {
91        $semantics = new Semantics();
92        $table = $semantics->inspect('CREATE TABLE t (a INT, b INT)');
93        $alter = $semantics->inspect('ALTER TABLE t MODIFY b INT FIRST', [$table]);
94
95        self::assertSame(['b', 'a'], array_map(static fn (OutputSlot $slot): ?string => $slot->name?->value, $alter->facts->relation($alter->statement)->shape->slots));
96    }
97
98    public function testSlotAnswersTheDeclaredTypeOfTheNewDefinition(): void
99    {
100        $semantics = new Semantics();
101        $table = $semantics->inspect('CREATE TABLE t (a INT, b INT)');
102        $alter = $semantics->inspect('ALTER TABLE t ADD c INT NOT NULL', [$table]);
103
104        self::assertSame(Nullability::NotNull, $alter->facts->relation($alter->statement)->shape->slots[2]->nullability);
105    }
106
107    public function testDuplicatesReportsEveryLaterOccurrence(): void
108    {
109        $semantics = new Semantics();
110        $table = $semantics->inspect('CREATE TABLE t (a INT, b INT)');
111
112        self::assertSame(['Column a would be defined more than once.', 'Column A would be defined more than once.'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('ALTER TABLE t ADD A INT, RENAME COLUMN b TO a', [$table])->facts->diagnostics));
113    }
114
115    public function testImplicitAnswersTheInvisibleColumns(): void
116    {
117        $semantics = new Semantics();
118        $table = $semantics->inspect('CREATE TABLE t (a INT, h INT INVISIBLE)');
119        $alter = $semantics->inspect('ALTER TABLE t ADD i INT INVISIBLE, ALTER h SET DEFAULT (h + i)', [$table]);
120
121        self::assertSame([], $alter->facts->diagnostics);
122        self::assertSame(['a'], array_map(static fn (OutputSlot $slot): ?string => $slot->name?->value, $alter->facts->relation($alter->statement)->shape->slots));
123    }
124
125    public function testShowMakesAColumnVisible(): void
126    {
127        $semantics = new Semantics();
128        $table = $semantics->inspect('CREATE TABLE t (a INT, h INT INVISIBLE)');
129        $alter = $semantics->inspect('ALTER TABLE t ALTER h SET VISIBLE, ALTER a SET INVISIBLE', [$table]);
130
131        self::assertSame(['h'], array_map(static fn (OutputSlot $slot): ?string => $slot->name?->value, $alter->facts->relation($alter->statement)->shape->slots));
132    }
133
134    public function testInvisibleKeepsAnInvisibleColumnInTheChecks(): void
135    {
136        $semantics = new Semantics();
137        $table = $semantics->inspect('CREATE TABLE t (a INT, b INT)');
138
139        self::assertSame(['Column c would be defined more than once.'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('ALTER TABLE t ADD c INT INVISIBLE, ADD c INT', [$table])->facts->diagnostics));
140    }
141
142    public function testUnknownReportsAnAbsentPosition(): void
143    {
144        $semantics = new Semantics();
145        $table = $semantics->inspect('CREATE TABLE t (a INT, b INT)');
146
147        self::assertSame(['Column x does not exist in the table.'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('ALTER TABLE t ADD c INT AFTER x', [$table])->facts->diagnostics));
148    }
149
150    public function testReportReportsNothingForAnUndeclaredTable(): void
151    {
152        $semantics = new Semantics();
153
154        self::assertSame([], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('ALTER TABLE t ADD c INT AFTER x')->facts->diagnostics));
155    }
156}
157