packages/sql-semantics-sqlite/tests/Unit/Statement/Expression/Literal/CurrentTimeTest.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\Known;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Type\Nullability;
13use SqlSemantics\Platform\Sqlite\Semantics;
14use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\CurrentTime;
15use SqlSemantics\Platform\Sqlite\Statement\Expression\Literal\TimeKeyword;
16use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
17use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
18use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
19
20#[CoversClass(CurrentTime::class)]
21#[Medium]
22final class CurrentTimeTest extends TestCase
23{
24 public function testDeriveScalarGivesTextThatIsNeverNull(): void
25 {
26 $operation = (new Semantics())->analyze('SELECT CURRENT_TIME, CURRENT_DATE, CURRENT_TIMESTAMP', []);
27 $statement = $operation->statement;
28
29 self::assertInstanceOf(Select::class, $statement);
30 self::assertInstanceOf(ResultColumn::class, $statement->columns[1]);
31 self::assertInstanceOf(CurrentTime::class, $statement->columns[1]->expression);
32 self::assertSame(TimeKeyword::Date, $statement->columns[1]->expression->keyword);
33 $fact = $operation->facts->scalar($statement->columns[1]->expression);
34 self::assertEquals(new Known(Storage::Text), $fact->type);
35 self::assertSame(Nullability::NotNull, $fact->nullability);
36 self::assertNull($fact->resolution);
37 self::assertEquals(new Known(Storage::Text), $operation->field(0)->type);
38 self::assertEquals(new Known(Storage::Text), $operation->field(2)->type);
39 self::assertSame([], $operation->facts->diagnostics);
40 }
41
42 public function testRenderWritesTheKeywordInUpperCase(): void
43 {
44 self::assertSame('SELECT CURRENT_TIME AS c1, CURRENT_DATE AS c2, CURRENT_TIMESTAMP AS c3', (new Semantics())->analyze('select current_time AS c1, Current_Date AS c2, current_timestamp AS c3')->toString());
45 }
46
47 public function testRenderWritesANewlyBuiltKeyword(): void
48 {
49 $semantics = new Semantics();
50 $operation = new Operation($semantics->context(), new Select([new ResultColumn(new CurrentTime(TimeKeyword::Timestamp))]));
51
52 self::assertSame('SELECT CURRENT_TIMESTAMP', $operation->toString());
53 }
54}
55