packages/sql-semantics-sqlite/tests/Unit/Statement/Expression/Literal/TextLiteralTest.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\TextLiteral;
15use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
16use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
17use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
18
19#[CoversClass(TextLiteral::class)]
20#[Medium]
21final class TextLiteralTest extends TestCase
22{
23    public function testDeriveScalarGivesTextAndKeepsTheDecodedValue(): void
24    {
25        $operation = (new Semantics())->analyze("SELECT 'it''s', ''", []);
26        $statement = $operation->statement;
27
28        self::assertInstanceOf(Select::class, $statement);
29        self::assertInstanceOf(ResultColumn::class, $statement->columns[0]);
30        self::assertInstanceOf(ResultColumn::class, $statement->columns[1]);
31        self::assertInstanceOf(TextLiteral::class, $statement->columns[0]->expression);
32        self::assertInstanceOf(TextLiteral::class, $statement->columns[1]->expression);
33        self::assertSame("it's", $statement->columns[0]->expression->value);
34        self::assertSame('', $statement->columns[1]->expression->value);
35        $fact = $operation->facts->scalar($statement->columns[0]->expression);
36        self::assertEquals(new Known(Storage::Text), $fact->type);
37        self::assertSame(Nullability::NotNull, $fact->nullability);
38        self::assertNull($fact->resolution);
39        self::assertSame([], $operation->facts->diagnostics);
40    }
41
42    public function testRenderDoublesEveryEmbeddedQuote(): void
43    {
44        self::assertSame("SELECT 'it''s', '', ''''", (new Semantics())->analyze("select 'it''s', '', ''''")->toString());
45    }
46
47    public function testRenderWritesANewlyBuiltLiteral(): void
48    {
49        $semantics = new Semantics();
50        $operation = new Operation($semantics->context(), new Select([new ResultColumn(new TextLiteral("it's")), new ResultColumn(new TextLiteral('a"b'))]));
51
52        self::assertSame("SELECT 'it''s', 'a\"b'", $operation->toString());
53    }
54}
55