packages/sql-semantics-sqlite/tests/Unit/Statement/Expression/Literal/NullLiteralTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Literal;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Operation;
11use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Type\NullOnly;
13use SqlSemantics\Platform\Sqlite\Semantics;
14use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\NullLiteral;
15use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
16use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
17
18#[CoversClass(NullLiteral::class)]
19#[Medium]
20final class NullLiteralTest extends TestCase
21{
22    public function testDeriveScalarGivesTheNullOnlyType(): void
23    {
24        $operation = (new Semantics())->analyze('SELECT NULL', []);
25        $statement = $operation->statement;
26
27        self::assertInstanceOf(Select::class, $statement);
28        self::assertInstanceOf(ResultColumn::class, $statement->columns[0]);
29        self::assertInstanceOf(NullLiteral::class, $statement->columns[0]->expression);
30        $fact = $operation->facts->scalar($statement->columns[0]->expression);
31        self::assertInstanceOf(NullOnly::class, $fact->type);
32        self::assertSame(Nullability::Nullable, $fact->nullability);
33        self::assertNull($fact->resolution);
34        self::assertSame(Nullability::Nullable, $operation->field(0)->nullability);
35        self::assertSame([], $operation->facts->diagnostics);
36    }
37
38    public function testRenderWritesTheKeywordInUpperCase(): void
39    {
40        self::assertSame('SELECT NULL AS c1, NULL AS c2', (new Semantics())->analyze('select null AS c1, Null AS c2')->toString());
41    }
42
43    public function testRenderWritesANewlyBuiltLiteral(): void
44    {
45        $semantics = new Semantics();
46        $operation = new Operation($semantics->context(), new Select([new ResultColumn(new NullLiteral())]));
47
48        self::assertSame('SELECT NULL', $operation->toString());
49    }
50}
51