packages/sql-semantics-sqlite/tests/Unit/Statement/Connection/NameOperandTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Connection;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
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\Connection\Attach;
15use SqlSemantics\Platform\Sqlite\Statement\Connection\NameOperand;
16use SqlSemantics\Platform\Sqlite\Statement\Type\Storage;
17
18#[CoversClass(NameOperand::class)]
19#[Medium]
20final class NameOperandTest extends TestCase
21{
22 public function testDeriveScalarIsATextConstantAndNoColumnReference(): void
23 {
24 $operation = (new Semantics())->analyze("ATTACH 'file.db' AS aux", []);
25 $statement = $operation->statement;
26
27 self::assertInstanceOf(Attach::class, $statement);
28 self::assertInstanceOf(NameOperand::class, $statement->schema);
29 self::assertEquals(new Known(Storage::Text), $operation->facts->scalar($statement->schema)->type);
30 self::assertSame(Nullability::NotNull, $operation->facts->scalar($statement->schema)->nullability);
31 self::assertNull($operation->facts->scalar($statement->schema)->resolution);
32 self::assertSame([], $operation->facts->diagnostics);
33 }
34
35 public function testRenderWritesTheNameAsAnIdentifier(): void
36 {
37 $semantics = new Semantics();
38
39 self::assertSame('DETACH aux', $semantics->analyze('DETACH aux')->toString());
40 self::assertSame('DETACH `my db`', $semantics->analyze('DETACH [my db]')->toString());
41 self::assertSame('aux', (new NameOperand(new Name('aux')))->name->value);
42 }
43}
44