packages/ztd-query-postgres/tests/Unit/Connection/Parameter/QuotedInputTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Connection\Parameter;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\TestCase;
9
10#[CoversClass(\ZtdQuery\Platform\Postgres\Connection\Parameter\QuotedInput::class)]
11#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Connection\Parameter\EscapeCursor::class)]
12#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\Postgres\Connection\Parameter\TokenBoundary::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 QuotedInputTest extends TestCase
16{
17 public function testConsumeIgnoredPreservesNestedCommentsAndContext(): void
18 {
19 $input = new \ZtdQuery\Platform\Postgres\Connection\Parameter\QuotedInput();
20 $cursor = new \ZtdQuery\Platform\Postgres\Connection\Parameter\EscapeCursor('/* ? /* nested */ ? */');
21 self::assertTrue($input->consumeIgnored($cursor));
22 self::assertSame($cursor->sql, $cursor->result);
23 self::assertSame(strlen($cursor->sql), $cursor->position);
24 self::assertTrue($cursor->expectsOperand);
25 self::assertFalse($input->consumeIgnored(new \ZtdQuery\Platform\Postgres\Connection\Parameter\EscapeCursor('x')));
26 }
27
28 public function testConsumeQuotedPreservesLiteralQuestionMarks(): void
29 {
30 $input = new \ZtdQuery\Platform\Postgres\Connection\Parameter\QuotedInput();
31 $cursor = new \ZtdQuery\Platform\Postgres\Connection\Parameter\EscapeCursor("'a''?b' tail");
32 self::assertTrue($input->consumeQuoted($cursor));
33 self::assertSame("'a''?b'", $cursor->result);
34 self::assertFalse($cursor->expectsOperand);
35 $dollar = new \ZtdQuery\Platform\Postgres\Connection\Parameter\EscapeCursor('$tag$?$tag$');
36 self::assertTrue($input->consumeQuoted($dollar));
37 self::assertSame('$tag$?$tag$', $dollar->result);
38 self::assertFalse($input->consumeQuoted(new \ZtdQuery\Platform\Postgres\Connection\Parameter\EscapeCursor('$1')));
39 }
40}
41