packages/sql-faker/tests/Unit/PostgreSql/Generation/Rewrite/Query/ParserOptionsRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\SqlFaker\PostgreSql\Generation\Rewrite\Query;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\DataProvider;
9use PHPUnit\Framework\Attributes\UsesClass;
10use PHPUnit\Framework\TestCase;
11use SqlFaker\Generation\Token\ProductionOccurrence;
12use SqlFaker\Generation\Token\TerminalOccurrence;
13use SqlFaker\Generation\Token\TerminalSequence;
14use SqlFaker\PostgreSql\Generation\Rewrite\Query\ParserOptionsRule;
15
16#[CoversClass(ParserOptionsRule::class)]
17#[UsesClass(ProductionOccurrence::class)]
18#[UsesClass(TerminalOccurrence::class)]
19#[UsesClass(TerminalSequence::class)]
20final class ParserOptionsRuleTest extends TestCase
21{
22    #[DataProvider('providerOptions')]
23    public function testRewriteRemovesOnlyOptionsForbiddenByTheirDirectOwner(string $scope, string $marker, string $option, bool $nested): void
24    {
25        $head = new TerminalOccurrence($marker, 10, $nested ? [0, 2] : [0], $nested ? [$scope, 'nested'] : [$scope]);
26        $value = new TerminalOccurrence('OPTION', 11, [0, 1], [$scope, $option]);
27        $input = new TerminalSequence([$head, $value], [$head, $value], [], [
28            new ProductionOccurrence(0, null, $scope, 0), new ProductionOccurrence(1, 0, $option, 0),
29        ]);
30        $rule = new ParserOptionsRule();
31        $result = $rule->rewrite($input);
32        self::assertSame($nested ? [$head, $value] : [$head], $result->terminals);
33        self::assertSame($input->original, $result->original);
34        self::assertSame($result, $rule->rewrite($result));
35    }
36
37    /**
38     * @return iterable<array{string, string, string, bool}>
39     */
40    public static function providerOptions(): iterable
41    {
42        yield ['ViewStmt', 'RECURSIVE', 'opt_check_option', false];
43        yield ['ViewStmt', 'RECURSIVE', 'opt_check_option', true];
44        yield ['CreateTrigStmt', 'CONSTRAINT', 'opt_or_replace', false];
45        yield ['CreateTrigStmt', 'CONSTRAINT', 'opt_or_replace', true];
46    }
47}
48