packages/sql-semantics-sqlite/tests/Unit/Model/Contract/LeafKeysTest.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\LeafKeys;
11use SqlSemantics\Platform\Sqlite\Platform;
12use SqlSemantics\Platform\Sqlite\Semantics;
13
14#[CoversClass(LeafKeys::class)]
15#[Medium]
16final class LeafKeysTest extends TestCase
17{
18    public function testKeyIdentifiesALiteralByItsValueAndAKeywordByItsTerminal(): void
19    {
20        $profile = (new Semantics())->profile();
21        $platform = Platform::shared();
22        $tokens = $platform->parser($profile)->tokenize('select 1');
23        $keys = $platform->leafKeys($profile);
24
25        self::assertSame('SELECT:SELECT', $keys->key($tokens[0], 'oneselect: SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt', 0));
26        self::assertSame('number:1', $keys->key($tokens[1], 'term: INTEGER', 0));
27    }
28
29    public function testKeyIsNullAtADeclaredNoisePosition(): void
30    {
31        $profile = (new Semantics())->profile();
32        $platform = Platform::shared();
33        $tokens = $platform->parser($profile)->tokenize('SELECT 1;');
34
35        self::assertNull($platform->leafKeys($profile)->key($tokens[2], 'ecmd: cmdx SEMI', 1));
36        self::assertSame('number:1', $platform->leafKeys($profile)->key($tokens[1], 'term: INTEGER', 0));
37    }
38
39}
40