packages/sql-semantics-mysql/tests/Unit/Statement/Dml/Insert/InsertSetTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Dml\Insert;
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\Type\Nullability;
15use SqlSemantics\Platform\MySql\Semantics;
16use SqlSemantics\Platform\MySql\Statement\Dml\Assignment;
17use SqlSemantics\Platform\MySql\Statement\Dml\Insert\ColumnList;
18use SqlSemantics\Platform\MySql\Statement\Dml\Insert\InsertInto;
19use SqlSemantics\Platform\MySql\Statement\Dml\Insert\InsertSet;
20use SqlSemantics\Platform\MySql\Statement\Dml\Problem\DuplicateColumn;
21use SqlSemantics\Platform\MySql\Statement\Dml\WriteTarget;
22use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
23use SqlSemantics\Platform\MySql\Statement\Name\ColumnUse;
24use SqlSemantics\Platform\MySql\Statement\Type\Integral;
25use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
26
27#[CoversClass(InsertSet::class)]
28#[Medium]
29final class InsertSetTest extends TestCase
30{
31    public function testDeriveStatementReportsAColumnAssignedTwice(): void
32    {
33        $semantics = new Semantics();
34        $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))]);
35        $operation = $semantics->inspect('INSERT INTO t SET a = 1, b = a, a = 2', [$t]);
36
37        self::assertCount(1, $operation->facts->diagnostics);
38        self::assertInstanceOf(DuplicateColumn::class, $operation->facts->diagnostics[0]);
39    }
40
41    public function testRenderWritesTheAssignments(): void
42    {
43        self::assertSame('REPLACE INTO t SET a = 1', (new Semantics('mysql-5.6.51'))->inspect('replace t set a = 1')->toString());
44    }
45
46    public function testRenderRejectsAColumnList(): void
47    {
48        $this->expectExceptionMessage('INSERT ... SET names its columns in the assignments.');
49
50        new InsertSet(new InsertInto(false, null, false, new WriteTarget(new QualifiedName(new Name('t'))), new ColumnList([])), [new Assignment(new ColumnUse(new Name('a')), new NumberLiteral('1'))]);
51    }
52}
53