packages/sql-faker/tests/Unit/PostgreSql/Generation/Rewrite/Name/IndirectionStarRuleTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\SqlFaker\PostgreSql\Generation\Rewrite\Name;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\UsesClass;
9use PHPUnit\Framework\TestCase;
10use SqlFaker\Generation\Token\ProductionOccurrence;
11use SqlFaker\Generation\Token\TerminalOccurrence;
12use SqlFaker\Generation\Token\TerminalSequence;
13use SqlFaker\PostgreSql\Generation\Rewrite\Name\IndirectionStarRule;
14
15#[CoversClass(IndirectionStarRule::class)]
16#[UsesClass(ProductionOccurrence::class)]
17#[UsesClass(TerminalOccurrence::class)]
18#[UsesClass(TerminalSequence::class)]
19final class IndirectionStarRuleTest extends TestCase
20{
21 public function testRewriteRemovesOnlyNonFinalStarsWithinTheirOwnChain(): void
22 {
23 $dot = new TerminalOccurrence('.', 10, [0, 1, 2, 3], ['columnref', 'opt_indirection', 'opt_indirection', 'indirection_el']);
24 $star = new TerminalOccurrence('*', 11, $dot->ancestors, $dot->rules);
25 $nextDot = new TerminalOccurrence('.', 12, [0, 1, 4], ['columnref', 'opt_indirection', 'indirection_el']);
26 $lastStar = new TerminalOccurrence('*', 13, $nextDot->ancestors, $nextDot->rules);
27 $multiply = new TerminalOccurrence('*', 14, [5], ['a_expr']);
28 $otherDot = new TerminalOccurrence('.', 15, [6, 7], ['indirection', 'indirection_el']);
29 $otherStar = new TerminalOccurrence('*', 16, $otherDot->ancestors, $otherDot->rules);
30 $input = new TerminalSequence([$dot, $star, $nextDot, $lastStar, $multiply, $otherDot, $otherStar], [], [], [new ProductionOccurrence(3, 2, 'indirection_el', 0), new ProductionOccurrence(4, 1, 'indirection_el', 0), new ProductionOccurrence(7, 6, 'indirection_el', 0)]);
31 $rule = new IndirectionStarRule();
32 $result = $rule->rewrite($input);
33 self::assertSame([$nextDot, $lastStar, $multiply, $otherDot, $otherStar], $result->terminals);
34 self::assertSame($result, $rule->rewrite($result));
35 }
36
37 public function testRewriteKeepsNamedComponentsAndEmptyOccurrences(): void
38 {
39 $dot = new TerminalOccurrence('.', 2, [0, 1], ['indirection', 'indirection_el']);
40 $name = new TerminalOccurrence('IDENT', 3, $dot->ancestors, $dot->rules);
41 $input = new TerminalSequence([$dot, $name], [], [], [new ProductionOccurrence(1, 0, 'indirection_el', 0), new ProductionOccurrence(4, 0, 'indirection_el', 0)]);
42 self::assertSame($input, (new IndirectionStarRule())->rewrite($input));
43 }
44}
45