packages/ztd-query-postgres/tests/Unit/Connection/Copy/TextFieldsTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Connection\Copy;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\TestCase;
9
10#[CoversClass(\ZtdQuery\Platform\Postgres\Connection\Copy\TextFields::class)]
11final class TextFieldsTest extends TestCase
12{
13    public function testEscape(): void
14    {
15        self::assertSame('a\\tb\\nc\\\\d', (new \ZtdQuery\Platform\Postgres\Connection\Copy\TextFields())->escape('a	b
16c\\d', '	'));
17    }
18
19    public function testDecodeFields(): void
20    {
21        self::assertSame(['1', null, 'line
22next'], (new \ZtdQuery\Platform\Postgres\Connection\Copy\TextFields())->decodeFields('1	\\N	line\\nnext', '	', '\\N'));
23
24        self::assertSame(['A', 'B', '	'], (new \ZtdQuery\Platform\Postgres\Connection\Copy\TextFields())->decodeFields('\\101	\\x42	\\t', '	', '\\N'));
25
26        self::assertSame(['a	b', ''], (new \ZtdQuery\Platform\Postgres\Connection\Copy\TextFields())->decodeFields('a\\	b	', '	', '\\N'));
27    }
28
29    public function testValidateSeparator(): void
30    {
31        (new \ZtdQuery\Platform\Postgres\Connection\Copy\TextFields())->validateSeparator('	');
32
33        self::assertSame('unchanged', (new \ZtdQuery\Platform\Postgres\Connection\Copy\TextFields())->escape('unchanged', "\t"));
34    }
35    public function testDecodeEscapeAdvancesOverOneEncodedByte(): void
36    {
37        $index = 0;
38        self::assertSame("\xFF", (new \ZtdQuery\Platform\Postgres\Connection\Copy\TextFields())->decodeEscape('\\377', $index));
39        self::assertSame(3, $index);
40    }
41
42    public function testOctalByteConsumesAtMostThreeDigits(): void
43    {
44        $index = 0;
45        self::assertSame("\xFF", (new \ZtdQuery\Platform\Postgres\Connection\Copy\TextFields())->octalByte('377rest', $index, '3'));
46        self::assertSame(2, $index);
47    }
48
49    public function testHexByteConsumesTwoDigitsAfterThePrefix(): void
50    {
51        $index = 0;
52        self::assertSame('A', (new \ZtdQuery\Platform\Postgres\Connection\Copy\TextFields())->hexByte('x41rest', $index, '4'));
53        self::assertSame(2, $index);
54    }
55}
56