packages/sql-semantics-postgres/tests/Unit/Rules/Expression/ColumnFactsTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Expression;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
13use SqlSemantics\Platform\PostgreSql\Model\Resolution\Environment;
14use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Column\AliasTarget;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\Field;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Shape\OutputSlot;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Invalid;
19use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
20use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
21use SqlSemantics\Platform\PostgreSql\Platform;
22use SqlSemantics\Platform\PostgreSql\Rules\Expression\ColumnFacts;
23use SqlSemantics\Platform\PostgreSql\Statement\Expression\Problem\AmbiguousOutputName;
24use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
25
26#[CoversClass(ColumnFacts::class)]
27#[Small]
28final class ColumnFactsTest extends TestCase
29{
30 public function testDeriveRefersToAnOutputColumnByName(): void
31 {
32 $context = (new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true);
33 $field = new Field(0, new OutputSlot(new Name('x'), new Known(Builtin::Int4), Nullability::NotNull));
34 $fact = (new ColumnFacts())->derive(new Derivation($context), new Environment($context, null, [], [], [$field]), [new Name('x')]);
35 self::assertEquals(new AliasTarget($field), $fact->resolution);
36 self::assertEquals(new Known(Builtin::Int4), $fact->type);
37 }
38
39 public function testAliasReportsSeveralOutputColumns(): void
40 {
41 $context = (new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true);
42 $field = new Field(0, new OutputSlot(new Name('x'), new Known(Builtin::Int4), Nullability::NotNull));
43 $other = new Field(1, new OutputSlot(new Name('x'), new Known(Builtin::Text), Nullability::NotNull));
44 $fact = (new ColumnFacts())->alias(new Derivation($context), new Environment($context, null, [], [], [$field, $other]), new Name('x'));
45 self::assertInstanceOf(Invalid::class, $fact->type);
46 self::assertInstanceOf(AmbiguousOutputName::class, $fact->type->cause);
47 }
48}
49