packages/sql-semantics-postgres/tests/Unit/Rules/Utility/OptionRulesTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Utility;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
11use SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic;
12use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
13use SqlSemantics\Platform\PostgreSql\Rules\Utility\OptionRules;
14use SqlSemantics\Platform\PostgreSql\Semantics;
15use SqlSemantics\Platform\PostgreSql\Statement\Option\Toggle;
16use SqlSemantics\Platform\PostgreSql\Statement\Utility\Maintenance\OptionKeyword;
17use SqlSemantics\Platform\PostgreSql\Statement\Utility\Maintenance\OptionSyntax;
18use SqlSemantics\Platform\PostgreSql\Statement\Utility\Maintenance\UtilityOption;
19
20#[CoversClass(OptionRules::class)]
21#[Medium]
22final class OptionRulesTest extends TestCase
23{
24    public function testWritableAcceptsWordsInTheOrderOfTheGrammar(): void
25    {
26        self::assertTrue((new OptionRules())->writable([new UtilityOption(new Name('full')), new UtilityOption(OptionKeyword::Analyse)], OptionSyntax::Words, ['full', 'freeze', 'verbose', 'analyze']));
27    }
28
29    public function testWritableRefusesWhatTheWordSyntaxCannotHold(): void
30    {
31        $rules = new OptionRules();
32        self::assertSame(
33            [false, false, false, false, false],
34            [
35                $rules->writable([new UtilityOption(new Name('verbose')), new UtilityOption(new Name('full'))], OptionSyntax::Words, ['full', 'verbose']),
36                $rules->writable([new UtilityOption(new Name('full')), new UtilityOption(new Name('full'))], OptionSyntax::Words, ['full', 'verbose']),
37                $rules->writable([new UtilityOption(new Name('full'), Toggle::True)], OptionSyntax::Words, ['full']),
38                $rules->writable([new UtilityOption(new Name('analyze'))], OptionSyntax::Words, ['analyze']),
39                $rules->writable([], OptionSyntax::Parenthesized, ['full']),
40            ],
41        );
42    }
43
44    public function testWriteWritesEachSyntax(): void
45    {
46        $semantics = new Semantics();
47        self::assertSame(
48            ['VACUUM FULL FREEZE VERBOSE ANALYSE', 'VACUUM (verbose, parallel 2) t', 'VACUUM t'],
49            [$semantics->inspect('VACUUM FULL FREEZE VERBOSE ANALYSE')->toString(), $semantics->inspect('VACUUM (VERBOSE, PARALLEL 2) t')->toString(), $semantics->inspect('VACUUM t')->toString()],
50        );
51    }
52
53    public function testFindAnswersTheLastOccurrence(): void
54    {
55        $last = new UtilityOption(new Name('verbose'), Toggle::False);
56        self::assertSame([$last, null], [(new OptionRules())->find([new UtilityOption(new Name('verbose')), $last], 'verbose'), (new OptionRules())->find([$last], 'full')]);
57    }
58
59    public function testKnownAddsTheOptionsOf17(): void
60    {
61        self::assertSame(
62            [false, true, ['verbose']],
63            [in_array('memory', (new OptionRules())->known('EXPLAIN', GrammarRelease::PostgreSql166), true), in_array('memory', (new OptionRules())->known('EXPLAIN', GrammarRelease::PostgreSql172), true), (new OptionRules())->known('CLUSTER', GrammarRelease::PostgreSql172)],
64        );
65    }
66
67    public function testDeriveReportsUnknownOptionsAndUnreadableValues(): void
68    {
69        self::assertSame(
70            ['unrecognized ANALYZE option "full"', 'verbose requires a Boolean value', 'buffer_usage_limit requires a parameter'],
71            array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), (new Semantics())->inspect("ANALYZE (FULL, VERBOSE 'yes', SKIP_LOCKED 0, BUFFER_USAGE_LIMIT) t")->facts->diagnostics),
72        );
73    }
74
75    public function testDeriveComparesNamesAfterFolding(): void
76    {
77        self::assertSame(
78            ['unrecognized CLUSTER option "Verbose"'],
79            array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), (new Semantics())->inspect('CLUSTER (VERBOSE, "Verbose") t')->facts->diagnostics),
80        );
81    }
82}
83