packages/sql-semantics-sqlite/tests/Unit/Model/Statement/Fact/ScalarFactTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Model\Statement\Fact;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Fact\ScalarFact;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Reference\Column\ResolvedColumn;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Known;
13use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
14use SqlSemantics\Platform\Sqlite\Semantics;
15use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
16
17#[CoversClass(ScalarFact::class)]
18#[Medium]
19final class ScalarFactTest extends TestCase
20{
21    public function testResolutionIsNullForAnExpressionThatIsNoNameUse(): void
22    {
23        $query = (new Semantics())->analyze("SELECT 'a'");
24        $expression = $query->field(0)->expression;
25
26        self::assertNotNull($expression);
27        $fact = $query->facts->scalar($expression);
28        self::assertInstanceOf(Known::class, $fact->type);
29        self::assertSame(Storage::Text, $fact->type->descriptor);
30        self::assertSame(Nullability::NotNull, $fact->nullability);
31        self::assertNull($fact->resolution);
32    }
33
34    public function testResolutionIsKeptForANameUse(): void
35    {
36        $semantics = new Semantics();
37        $table = $semantics->analyze('CREATE TABLE t (a INTEGER)');
38        $query = $semantics->analyze('SELECT a FROM t WHERE a > 1', [$table]);
39        $expression = $query->field('a')->expression;
40
41        self::assertNotNull($expression);
42        $fact = $query->facts->scalar($expression);
43        self::assertInstanceOf(ResolvedColumn::class, $fact->resolution);
44        self::assertSame($table->declarations()[0]->columns[0], $fact->resolution->declaration());
45        self::assertSame(Nullability::Nullable, $fact->nullability);
46    }
47
48    public function testTheFactIsAPlainValue(): void
49    {
50        $fact = new ScalarFact(new Known(Storage::Real), Nullability::Dependent);
51
52        self::assertSame(Nullability::Dependent, $fact->nullability);
53        self::assertNull($fact->resolution);
54    }
55}
56