packages/sql-semantics-postgres/tests/Unit/Statement/Utility/Maintenance/UtilityOptionTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Utility\Maintenance;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\PostgreSql\Semantics;
12use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
13use SqlSemantics\Platform\PostgreSql\Statement\Literal\NumericConstant;
14use SqlSemantics\Platform\PostgreSql\Statement\Literal\SignedNumber;
15use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
16use SqlSemantics\Platform\PostgreSql\Statement\Option\Toggle;
17use SqlSemantics\Platform\PostgreSql\Statement\Option\Word;
18use SqlSemantics\Platform\PostgreSql\Statement\Utility\Maintenance\OptionKeyword;
19use SqlSemantics\Platform\PostgreSql\Statement\Utility\Maintenance\UtilityOption;
20
21#[CoversClass(UtilityOption::class)]
22#[Medium]
23final class UtilityOptionTest extends TestCase
24{
25    public function testOptionAnswersTheNameOrTheKeywordOption(): void
26    {
27        self::assertSame(['costs', 'analyze'], [(new UtilityOption(new Name('costs')))->option(), (new UtilityOption(OptionKeyword::Analyse))->option()]);
28    }
29
30    public function testTextAnswersTheValueAsTheServerReceivesIt(): void
31    {
32        self::assertSame(
33            [null, 'json', 'a b', 'on', '-12', '1.50', '2.0e-3'],
34            [
35                (new UtilityOption(new Name('x')))->text(),
36                (new UtilityOption(new Name('x'), new Word(new Name('json'))))->text(),
37                (new UtilityOption(new Name('x'), new StringConstant('a b')))->text(),
38                (new UtilityOption(new Name('x'), Toggle::On))->text(),
39                (new UtilityOption(new Name('x'), new SignedNumber(true, new IntegerConstant('12'))))->text(),
40                (new UtilityOption(new Name('x'), new SignedNumber(false, new NumericConstant('1.50'))))->text(),
41                (new UtilityOption(new Name('x'), new SignedNumber(false, new NumericConstant('2.0e-3'))))->text(),
42            ],
43        );
44    }
45
46    public function testDeriveClauseRecordsNoProblemForAValue(): void
47    {
48        self::assertSame([], (new Semantics())->analyze("VACUUM (TRUNCATE 'on', PARALLEL 2, INDEX_CLEANUP auto) t")->facts->diagnostics);
49    }
50
51    public function testRenderQuotesFormatOnlyBeforeJson(): void
52    {
53        $semantics = new Semantics();
54        self::assertSame(
55            ['EXPLAIN (FORMAT json) SELECT 1', 'EXPLAIN ("format" json) SELECT 1', 'EXPLAIN (format xml) SELECT 1', 'EXPLAIN (costs FALSE, "analyze") SELECT 1'],
56            [$semantics->analyze('EXPLAIN (FORMAT JSON) SELECT 1')->toString(), $semantics->analyze('EXPLAIN ("format" json) SELECT 1')->toString(), $semantics->analyze('EXPLAIN ("format" xml) SELECT 1')->toString(), $semantics->analyze('EXPLAIN (COSTS false, "analyze") SELECT 1')->toString()],
57        );
58    }
59}
60