packages/sql-semantics-sqlite/tests/Unit/Model/Contract/CodecTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Model\Contract;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\Sqlite\Model\Contract\Codec;
11use SqlSemantics\Platform\Sqlite\Model\Contract\NameUse;
12use SqlSemantics\Platform\Sqlite\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\Sqlite\Platform;
14use SqlSemantics\Platform\Sqlite\Semantics;
15
16#[CoversClass(Codec::class)]
17#[Medium]
18final class CodecTest extends TestCase
19{
20    public function testNameIsWrittenBareWhenTheProfileAllowsIt(): void
21    {
22        $codec = Platform::shared()->codec((new Semantics())->profile());
23
24        self::assertSame('users', $codec->name(new Name('users'), NameUse::Relation));
25    }
26
27    public function testNameIsQuotedWhenItWouldNotDecodeToItselfBare(): void
28    {
29        $codec = Platform::shared()->codec((new Semantics())->profile());
30
31        self::assertSame('`order items`', $codec->name(new Name('order items'), NameUse::Column));
32        self::assertSame('`select`', $codec->name(new Name('select'), NameUse::Alias));
33    }
34
35    public function testNameSpellingDecodesToTheSameNameWhenAnalyzedAgain(): void
36    {
37        $semantics = new Semantics();
38        $spelling = Platform::shared()->codec($semantics->profile())->name(new Name('a`b'), NameUse::Column);
39
40        $query = $semantics->inspect('SELECT ' . $spelling);
41
42        self::assertSame('a`b', $query->field(0)->name?->value);
43    }
44}
45