packages/ztd-query-postgres/tests/Unit/Sql/Lexing/IdentifierDecoderTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Sql\Lexing;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\TestCase;
9
10#[CoversClass(\ZtdQuery\Platform\Postgres\Sql\Lexing\IdentifierDecoder::class)]
11#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Sql\PgSqlLexerProfile::class)]
12#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Sql\PostgreSqlLexicalMasker::class)]
13#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Sql\Lexing\CommentSpan::class)]
14#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Sql\Lexing\QuotedSpan::class)]
15final class IdentifierDecoderTest extends TestCase
16{
17    public function testUnquoteIdentifier(): void
18    {
19        self::assertSame('User"Name', (new \ZtdQuery\Platform\Postgres\Sql\Lexing\IdentifierDecoder())->unquoteIdentifier('"User""Name"'));
20    }
21
22    public function testStripSchemaPrefix(): void
23    {
24        self::assertSame('"Users"', (new \ZtdQuery\Platform\Postgres\Sql\Lexing\IdentifierDecoder())->stripSchemaPrefix('tenant."Users"'));
25    }
26
27    public function testParseColumnList(): void
28    {
29        self::assertSame(['id', 'Name'], (new \ZtdQuery\Platform\Postgres\Sql\Lexing\IdentifierDecoder())->parseColumnList('id, "Name"'));
30    }
31
32    public function testTruncateIdentifierAt(): void
33    {
34        $sql = 'tenant."Users"';
35        $stream = \ZtdQuery\Sql\SqlTokenStream::tokenize($sql, \ZtdQuery\Platform\Postgres\Sql\PgSqlLexerProfile::create());
36        $tokens = $stream->significantTokens();
37
38        self::assertSame(['name' => 'tenant', 'next' => 1], (new \ZtdQuery\Platform\Postgres\Sql\Lexing\IdentifierDecoder())->truncateIdentifierAt($stream, 0));
39    }
40}
41