packages/sql-semantics-postgres/tests/Unit/Rules/Manipulation/AssignmentCastsTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Manipulation;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Rules\Manipulation\AssignmentCasts::class)]
12#[Medium]
13final class AssignmentCastsTest extends TestCase
14{
15 public function testRefusedAcceptsNumericAndStringTargets(): void
16 {
17 $casts = new \SqlSemantics\Platform\PostgreSql\Rules\Manipulation\AssignmentCasts();
18 self::assertSame([false, false, false, true, true], [$casts->refused(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Float8, \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int2), $casts->refused(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Bool, \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Varchar), $casts->refused(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Timestamptz, \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Date), $casts->refused(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Text, \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4), $casts->refused(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Date, \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Bool)]);
19 }
20
21 public function testRefusedAcceptsTypesOutsideTheTables(): void
22 {
23 self::assertFalse((new \SqlSemantics\Platform\PostgreSql\Rules\Manipulation\AssignmentCasts())->refused(\SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Xml, \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4));
24 }
25
26 public function testCheckReportsAValueTheColumnCannotTake(): void
27 {
28 $profile = new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
29 $t = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')), $profile, [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('b'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Text)]);
30 $u = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('u')), $profile, [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int8, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('c'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Bool, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull)]);
31 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics('pg-17.2');
32 $query = $semantics->inspect("UPDATE t SET a = 'x'::text", [$t, $u]);
33 self::assertSame(['column "a" is of type integer but expression is of type text'], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), $query->facts->diagnostics));
34 }
35
36 public function testCheckAcceptsAStringConstant(): void
37 {
38 $profile = new \SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile(\SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease::PostgreSql172);
39 $t = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')), $profile, [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int4, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('b'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Text)]);
40 $u = new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Table(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('u')), $profile, [new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('a'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Int8, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull), new \SqlSemantics\Platform\PostgreSql\Model\Statement\Declaration\Column(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('c'), \SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin::Bool, \SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability::NotNull)]);
41 $semantics = new \SqlSemantics\Platform\PostgreSql\Semantics('pg-17.2');
42 $query = $semantics->inspect("UPDATE t SET a = '1'", [$t, $u]);
43 self::assertSame([], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), $query->facts->diagnostics));
44 }
45}
46