packages/sql-faker/tests/Unit/PostgreSql/Generation/Value/OperatorDomainTest.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\OperatorDomain;
12
13#[CoversClass(OperatorDomain::class)]
14#[UsesClass(CharacterDomain::class)]
15final class OperatorDomainTest extends TestCase
16{
17    public function testChooseConstructsOperatorsWithoutCommentOpeners(): void
18    {
19        $domain = new OperatorDomain('+*/<>=!@#%^&|`?~-', '~!@#^&|`?%', ['+'], ['--', '/*']);
20        self::assertSame('?', $domain->choose(static fn (int $count): int => 0));
21        self::assertSame('?' . str_repeat('~', 31), $domain->choose(static fn (int $count): int => $count - 1));
22    }
23
24    #[\PHPUnit\Framework\Attributes\DataProvider('providerOperators')]
25    public function testMatchHonorsFixedTokensCommentsAndTrailingSignRules(string $value, bool $valid): void
26    {
27        $domain = new OperatorDomain('+*/<>=!@#%^&|`?~-', '~!@#^&|`?%', ['+', '-', '/', '<=', '!='], ['--', '/*']);
28        self::assertSame($valid, in_array(strlen($value), $domain->match($value), true));
29    }
30
31    /**
32     * @return iterable<array{string, bool}>
33     */
34    public static function providerOperators(): iterable
35    {
36        foreach (['?', '?|', '|-', '?+'] as $value) {
37            yield [$value, true];
38        }
39        foreach (['+', '<=', '!=', '--', '/*', '>-', 'a', str_repeat('?', 64)] as $value) {
40            yield [$value, false];
41        }
42    }
43
44}
45