packages/sql-semantics-mysql/tests/Unit/Rules/TableChange/ColumnNamesTest.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\Rules\TableChange\ColumnNames;
12use SqlSemantics\Platform\MySql\Semantics;
13
14#[CoversClass(ColumnNames::class)]
15#[Medium]
16final class ColumnNamesTest extends TestCase
17{
18 public function testCheckReportsEveryAbsentName(): void
19 {
20 $semantics = new Semantics();
21 $table = $semantics->inspect('CREATE TABLE t (a INT, b INT)');
22
23 self::assertSame(['Column x does not exist in the table.', 'Column y does not exist in the table.'], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('ALTER TABLE t PARTITION BY KEY (a, x, y)', [$table])->facts->diagnostics));
24 }
25
26 public function testPresentLeavesAnUndeclaredTableUndecided(): void
27 {
28 $semantics = new Semantics();
29
30 self::assertSame([], array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('ALTER TABLE t PARTITION BY KEY (x)')->facts->diagnostics));
31 }
32
33 public function testCountComparesWithoutRegardToCase(): void
34 {
35 $semantics = new Semantics();
36 $table = $semantics->inspect('CREATE TABLE t (a INT, b INT)');
37
38 self::assertSame([], $semantics->inspect('ALTER TABLE t PARTITION BY KEY (A)', [$table])->facts->diagnostics);
39 }
40
41 public function testWriteWritesTheParenthesizedNames(): void
42 {
43 self::assertSame('ALTER TABLE t PARTITION BY KEY (`a b`, c)', (new Semantics())->inspect('ALTER TABLE t PARTITION BY KEY (`a b`, c)')->toString());
44 }
45}
46