packages/sql-semantics-mysql/tests/Unit/Rules/Dml/WriteScopeTest.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\Type\Nullability;
16use SqlSemantics\Platform\MySql\Rules\Dml\WriteScope;
17use SqlSemantics\Platform\MySql\Semantics;
18use SqlSemantics\Platform\MySql\Statement\Dml\Problem\DuplicateColumn;
19use SqlSemantics\Platform\MySql\Statement\Dml\Update;
20use SqlSemantics\Platform\MySql\Statement\Type\Integral;
21use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
22
23#[CoversClass(WriteScope::class)]
24#[Medium]
25final class WriteScopeTest extends TestCase
26{
27    public function testFieldTakesTheColumnFact(): void
28    {
29        $semantics = new Semantics();
30        $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))]);
31        $operation = $semantics->inspect('UPDATE t SET b = 1', [$t]);
32        self::assertInstanceOf(Update::class, $operation->statement);
33        $column = $operation->statement->assignments[0]->column;
34        $field = (new WriteScope())->field(3, $column, $operation->facts->scalar($column));
35
36        self::assertSame(3, $field->position);
37        self::assertSame('b', $field->column()?->name->value);
38    }
39
40    public function testValueDerivesDefaultAsTheColumn(): void
41    {
42        $semantics = new Semantics();
43        $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))]);
44        $operation = $semantics->inspect('UPDATE t SET a = DEFAULT', [$t]);
45        self::assertInstanceOf(Update::class, $operation->statement);
46
47        self::assertSame(Nullability::NotNull, $operation->facts->scalar($operation->statement->assignments[0]->value)->nullability);
48    }
49
50    public function testValueReportsARowAsTheWrittenValue(): void
51    {
52        $operation = (new Semantics())->inspect('UPDATE t SET a = (1, 2)');
53
54        self::assertSame(['Operand should contain 1 column(s), not 2.'], array_map(static fn ($diagnostic): string => $diagnostic->message(), $operation->facts->diagnostics));
55    }
56
57    public function testAssignAnswersTheAssignedColumns(): void
58    {
59        $semantics = new Semantics();
60        $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))]);
61        $operation = $semantics->inspect('INSERT INTO t SET b = 1, z = 2', [$t]);
62
63        self::assertCount(1, $operation->facts->diagnostics);
64        self::assertInstanceOf(MissingColumn::class, $operation->facts->diagnostics[0]);
65    }
66
67    public function testDistinctReportsAColumnWrittenTwice(): void
68    {
69        $semantics = new Semantics();
70        $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))]);
71        $operation = $semantics->inspect('INSERT INTO t (b, t.b) VALUES (1, 2)', [$t]);
72
73        self::assertInstanceOf(DuplicateColumn::class, $operation->facts->diagnostics[0]);
74    }
75}
76