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

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\SqlFaker\PostgreSql\Generation\Rewrite;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\UsesClass;
9use PHPUnit\Framework\TestCase;
10use SqlFaker\Generation\Derivation\DerivationTrace;
11use SqlFaker\Grammar\Model\NonTerminal;
12use SqlFaker\Grammar\Model\Production;
13use SqlFaker\Grammar\Model\Terminal;
14use SqlFaker\PostgreSql\Generation\Rewrite\CopySourceRule;
15
16#[CoversClass(CopySourceRule::class)]
17#[UsesClass(DerivationTrace::class)]
18#[UsesClass(NonTerminal::class)]
19#[UsesClass(Production::class)]
20#[UsesClass(Terminal::class)]
21#[UsesClass(\SqlFaker\Generation\Token\ProductionOccurrence::class)]
22#[UsesClass(\SqlFaker\Generation\Token\TerminalOccurrence::class)]
23#[UsesClass(\SqlFaker\Generation\Token\TerminalSequence::class)]
24final class CopySourceRuleTest extends TestCase
25{
26    public function testRewriteReplacesProgramStdinAndRemovesOnlyDirectCopyToWhere(): void
27    {
28        $trace = new DerivationTrace('CopyStmt');
29        $trace->expand(0, new Production([new NonTerminal('copy_from'), new NonTerminal('opt_program'), new NonTerminal('copy_file_name'), new NonTerminal('where_clause'), new NonTerminal('query')]), 0);
30        $trace->expand(0, new Production([new Terminal('TO')]), 1);
31        $trace->expand(1, new Production([new Terminal('PROGRAM')]), 1);
32        $trace->expand(2, new Production([new Terminal('STDIN')]), 1);
33        $trace->expand(3, new Production([new Terminal('WHERE'), new Terminal('ICONST')]), 1);
34        $trace->expand(5, new Production([new NonTerminal('where_clause')]), 0);
35        $trace->expand(5, new Production([new Terminal('WHERE'), new Terminal('ICONST')]), 1);
36        $input = $trace->terminals();
37        $result = (new CopySourceRule())->rewrite($input);
38        self::assertSame(['TO', 'PROGRAM', 'SCONST', 'WHERE', 'ICONST'], $result->names());
39        self::assertSame($input->original, $result->original);
40        self::assertCount(count($result->terminals), array_unique(array_map(static fn ($terminal): int => $terminal->id, $result->terminals)));
41        self::assertSame($input->terminals[5], $result->terminals[3]);
42    }
43
44    public function testRewritePreservesCopyFromStdinWithoutProgram(): void
45    {
46        $trace = new DerivationTrace('CopyStmt');
47        $trace->expand(0, new Production([new NonTerminal('copy_from'), new NonTerminal('opt_program'), new NonTerminal('copy_file_name'), new NonTerminal('where_clause')]), 0);
48        $trace->expand(0, new Production([new Terminal('FROM')]), 0);
49        $trace->expand(1, new Production([]), 0);
50        $trace->expand(1, new Production([new Terminal('STDIN')]), 1);
51        $trace->expand(2, new Production([new Terminal('WHERE'), new Terminal('ICONST')]), 1);
52        $input = $trace->terminals();
53        self::assertSame($input, (new CopySourceRule())->rewrite($input));
54    }
55}
56