packages/sql-faker/tests/Unit/PostgreSql/Generation/Value/DollarQuotedDomainTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\SqlFaker\PostgreSql\Generation\Value;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\UsesClass;
9use PHPUnit\Framework\TestCase;
10use SqlFaker\Generation\Value\CharacterDomain;
11use SqlFaker\PostgreSql\Generation\Value\DollarQuotedDomain;
12
13#[CoversClass(DollarQuotedDomain::class)]
14#[UsesClass(CharacterDomain::class)]
15final class DollarQuotedDomainTest extends TestCase
16{
17    public function testChooseReusesTheChosenTagWithoutASecondTagDecision(): void
18    {
19        $domain = new DollarQuotedDomain(new CharacterDomain(['a', 'b'], 1, 1), new CharacterDomain(['x'], 1, 1));
20        self::assertSame('$b$x$b$', $domain->choose(static fn (int $count): int => $count - 1));
21        self::assertSame('$$x$$', (new DollarQuotedDomain(new CharacterDomain(['a'], 0, 0), new CharacterDomain(['x'], 1, 1)))->choose(static fn (int $count): int => 0));
22    }
23
24    public function testMatchBindsTagsAndStopsAtTheFirstIdenticalDelimiter(): void
25    {
26        $domain = new DollarQuotedDomain(new CharacterDomain(['a'], 0, 3), new CharacterDomain(['x'], 0, 3));
27        self::assertSame([7], $domain->match('$a$x$a$'));
28        self::assertSame([5], $domain->match('$$x$$tail$$'));
29        self::assertSame([], $domain->match('$a$x$b$'));
30        self::assertSame([], $domain->match("$$\0$$"));
31        self::assertSame([], $domain->match('plain'));
32        self::assertSame([], $domain->match('$a'));
33    }
34
35    public function testMatchRequiresADollarAndALegalTagAtTheOffset(): void
36    {
37        $domain = new DollarQuotedDomain(new CharacterDomain(['a'], 0, 3), new CharacterDomain(['x'], 0, 3));
38        self::assertSame([], $domain->match('x$a$x$a$'));
39        self::assertSame([], $domain->match('$b$x$b$'));
40        self::assertSame([8], $domain->match('!$a$x$a$', 1));
41    }
42
43    public function testMatchClosesAtTheDelimiterAfterTheTagAndOnlyRejectsNulBytesInsideTheBody(): void
44    {
45        $domain = new DollarQuotedDomain(new CharacterDomain(['a'], 0, 3), new CharacterDomain(['x'], 0, 3));
46        self::assertSame([4], $domain->match('$$$$'));
47        self::assertSame([5], $domain->match("\$\$x\$\$\0"));
48        self::assertSame([11], $domain->match("\$aaa\$x\$aaa\$\0"));
49    }
50}
51