packages/sql-semantics-sqlite/tests/Unit/Statement/Expression/Literal/BlobLiteralTest.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\BlobLiteral;
15use SqlSemantics\Platform\Sqlite\Statement\Query\ResultColumn;
16use SqlSemantics\Platform\Sqlite\Statement\Query\Select;
17use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
18
19#[CoversClass(BlobLiteral::class)]
20#[Medium]
21final class BlobLiteralTest extends TestCase
22{
23 public function testAcceptsAnEmptyBlob(): void
24 {
25 self::assertSame('', (new BlobLiteral(''))->hex);
26 }
27
28 public function testDeriveScalarGivesBlob(): void
29 {
30 $operation = (new Semantics())->analyze("SELECT x'0aff'", []);
31 $statement = $operation->statement;
32
33 self::assertInstanceOf(Select::class, $statement);
34 self::assertInstanceOf(ResultColumn::class, $statement->columns[0]);
35 self::assertInstanceOf(BlobLiteral::class, $statement->columns[0]->expression);
36 self::assertSame('0AFF', $statement->columns[0]->expression->hex);
37 $fact = $operation->facts->scalar($statement->columns[0]->expression);
38 self::assertEquals(new Known(Storage::Blob), $fact->type);
39 self::assertSame(Nullability::NotNull, $fact->nullability);
40 self::assertNull($fact->resolution);
41 self::assertSame([], $operation->facts->diagnostics);
42 }
43
44 public function testRenderWritesTheDigitsInUpperCase(): void
45 {
46 self::assertSame('SELECT x\'0AFF\' AS c1, x\'\' AS c2', (new Semantics())->analyze('select X\'0aFf\' AS c1, x\'\' AS c2')->toString());
47 }
48
49 public function testRenderWritesANewlyBuiltBlob(): void
50 {
51 $semantics = new Semantics();
52 $operation = new Operation($semantics->context(), new Select([new ResultColumn(new BlobLiteral('00FF')), new ResultColumn(new BlobLiteral(''))]));
53
54 self::assertSame("SELECT x'00FF', x''", $operation->toString());
55 }
56}
57