packages/ztd-query-postgres/tests/Unit/Connection/Parameter/OperandInputTest.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\OperandInput::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)]
13final class OperandInputTest extends TestCase
14{
15 public function testConsumeOperatorDistinguishesPlaceholdersAndOperators(): void
16 {
17 $input = new \ZtdQuery\Platform\Postgres\Connection\Parameter\OperandInput();
18 $cursor = new \ZtdQuery\Platform\Postgres\Connection\Parameter\EscapeCursor('? ?|');
19 self::assertTrue($input->consumeOperator($cursor));
20 self::assertSame('?', $cursor->result);
21 self::assertFalse($cursor->expectsOperand);
22 $cursor->preserve(1);
23 self::assertTrue($input->consumeOperator($cursor));
24 self::assertSame('? ??|', $cursor->result);
25 self::assertTrue($cursor->expectsOperand);
26 self::assertSame(4, $cursor->position);
27 self::assertFalse($input->consumeOperator(new \ZtdQuery\Platform\Postgres\Connection\Parameter\EscapeCursor('word')));
28 }
29
30 public function testConsumeOperandTracksKeywordsAndNamedParameters(): void
31 {
32 $input = new \ZtdQuery\Platform\Postgres\Connection\Parameter\OperandInput();
33 $cursor = new \ZtdQuery\Platform\Postgres\Connection\Parameter\EscapeCursor('SELECT');
34 self::assertTrue($input->consumeOperand($cursor));
35 self::assertTrue($cursor->expectsOperand);
36 self::assertSame('SELECT', $cursor->result);
37 $parameter = new \ZtdQuery\Platform\Postgres\Connection\Parameter\EscapeCursor(':select');
38 $parameter->preserve(1);
39 self::assertTrue($input->consumeOperand($parameter));
40 self::assertFalse($parameter->expectsOperand);
41 $number = new \ZtdQuery\Platform\Postgres\Connection\Parameter\EscapeCursor('12.5');
42 self::assertTrue($input->consumeOperand($number));
43 self::assertSame('12.5', $number->result);
44 self::assertFalse($number->expectsOperand);
45 }
46}
47