packages/sql-semantics-mysql/tests/Unit/Rules/Dml/InsertFactsTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Dml;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Statement\Declaration\Column;
11use SqlSemantics\Platform\MySql\Model\Statement\Declaration\Table;
12use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\MySql\Model\Statement\Identifier\QualifiedName;
14use SqlSemantics\Platform\MySql\Model\Statement\Reference\Column\MissingColumn;
15use SqlSemantics\Platform\MySql\Model\Statement\Reference\Column\ResolvedColumn;
16use SqlSemantics\Platform\MySql\Model\Statement\Type\Dependent;
17use SqlSemantics\Platform\MySql\Model\Statement\Type\Invalid;
18use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
19use SqlSemantics\Platform\MySql\Rules\Dml\InsertFacts;
20use SqlSemantics\Platform\MySql\Semantics;
21use SqlSemantics\Platform\MySql\Statement\Dml\Insert\InsertRows;
22use SqlSemantics\Platform\MySql\Statement\Dml\Insert\InsertSet;
23use SqlSemantics\Platform\MySql\Statement\Type\Integral;
24use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
25
26#[CoversClass(InsertFacts::class)]
27#[Medium]
28final class InsertFactsTest extends TestCase
29{
30 public function testRowsChecksEveryRowAgainstTheFirst(): void
31 {
32 $operation = (new Semantics())->inspect('INSERT INTO t VALUES (1, 2), (3)');
33
34 self::assertSame(["Column count doesn't match value count at row 2"], array_map(static fn ($diagnostic): string => $diagnostic->message(), $operation->facts->diagnostics));
35 }
36
37 public function testRowsChecksAnEmptyRowAgainstTheOtherRows(): void
38 {
39 $semantics = new Semantics();
40 $tables = $semantics->inspect('CREATE TABLE t (a INT, b INT)')->declarations();
41 $messages = static fn (string $sql): array => array_map(static fn ($diagnostic): string => $diagnostic->message(), $semantics->inspect($sql, $tables)->facts->diagnostics);
42
43 self::assertSame(["Column count doesn't match value count at row 2"], $messages('INSERT INTO t VALUES (1, 2), ()'));
44 self::assertSame(["Column count doesn't match value count at row 2"], $messages('INSERT INTO t VALUES (), (1, 2)'));
45 self::assertSame([], $messages('INSERT INTO t VALUES (), ()'));
46 }
47
48 public function testSetDerivesTheAssignments(): void
49 {
50 $semantics = new Semantics();
51 $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull), new Column(new Name('b'), new Integral(IntegralKind::BigInt))]);
52 $operation = $semantics->inspect('INSERT INTO t SET a = 1, b = a', [$t]);
53 self::assertInstanceOf(InsertSet::class, $operation->statement);
54
55 self::assertInstanceOf(ResolvedColumn::class, $operation->facts->scalar($operation->statement->assignments[1]->value)->resolution);
56 }
57
58 public function testQueryDerivesTheSourceWithoutTheTable(): void
59 {
60 $semantics = new Semantics();
61 $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull), new Column(new Name('b'), new Integral(IntegralKind::BigInt))]);
62 $operation = $semantics->inspect('INSERT INTO t (a) SELECT b', [$t]);
63
64 self::assertInstanceOf(MissingColumn::class, $operation->facts->diagnostics[0]);
65 }
66
67 public function testOpenReportsAQualifiedStar(): void
68 {
69 $operation = (new Semantics('mysql-5.6.51'))->inspect('INSERT INTO t (t.*) VALUES (1)');
70
71 self::assertSame("Unknown column '*' in 'field list'", $operation->facts->diagnostics[0]->message());
72 }
73
74 public function testColumnDependsOnAnUndeclaredTable(): void
75 {
76 $operation = (new Semantics())->inspect('INSERT INTO t VALUES (DEFAULT)');
77 self::assertInstanceOf(InsertRows::class, $operation->statement);
78
79 self::assertInstanceOf(Dependent::class, $operation->facts->scalar($operation->statement->rows[0]->values[0])->type);
80 }
81
82 public function testColumnIsInvalidBeyondTheWrittenColumns(): void
83 {
84 $semantics = new Semantics();
85 $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull), new Column(new Name('b'), new Integral(IntegralKind::BigInt))]);
86 $operation = $semantics->inspect('INSERT INTO t (a) VALUES (1, DEFAULT)', [$t]);
87 self::assertInstanceOf(InsertRows::class, $operation->statement);
88
89 self::assertInstanceOf(Invalid::class, $operation->facts->scalar($operation->statement->rows[0]->values[1])->type);
90 }
91
92 public function testDuplicatesReportsAnAliasThatIsTheTableName(): void
93 {
94 $semantics = new Semantics();
95 $t = new Table(new QualifiedName(new Name('t'), new Name('(current)')), $semantics->profile(), [new Column(new Name('a'), new Integral(IntegralKind::Int), Nullability::NotNull), new Column(new Name('b'), new Integral(IntegralKind::BigInt))]);
96 $operation = $semantics->inspect('INSERT INTO t VALUES (1, 2) AS t ON DUPLICATE KEY UPDATE a = 1', [$t]);
97
98 self::assertSame('Not unique table/alias', $operation->facts->diagnostics[0]->message());
99 }
100}
101