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

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Utility;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\PostgreSql\Rules\Utility\OptionArguments;
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\UtilityOption;
19
20#[CoversClass(OptionArguments::class)]
21#[Small]
22final class OptionArgumentsTest extends TestCase
23{
24    public function testBooleanReadsTheValuesTheServerAccepts(): void
25    {
26        $values = new OptionArguments();
27        self::assertSame(
28            [true, true, false, true, false, true, false],
29            [
30                $values->boolean(new UtilityOption(new Name('verbose'))),
31                $values->boolean(new UtilityOption(new Name('verbose'), Toggle::On)),
32                $values->boolean(new UtilityOption(new Name('verbose'), Toggle::False)),
33                $values->boolean(new UtilityOption(new Name('verbose'), new StringConstant('TRUE'))),
34                $values->boolean(new UtilityOption(new Name('verbose'), new Word(new Name('Off')))),
35                $values->boolean(new UtilityOption(new Name('verbose'), new SignedNumber(false, new IntegerConstant('1')))),
36                $values->boolean(new UtilityOption(new Name('verbose'), new SignedNumber(true, new IntegerConstant('0')))),
37            ],
38        );
39    }
40
41    public function testBooleanRefusesOtherValues(): void
42    {
43        $values = new OptionArguments();
44        self::assertSame(
45            [null, null, null, null],
46            [
47                $values->boolean(new UtilityOption(new Name('verbose'), new StringConstant('yes'))),
48                $values->boolean(new UtilityOption(new Name('verbose'), new SignedNumber(false, new IntegerConstant('2')))),
49                $values->boolean(new UtilityOption(new Name('verbose'), new SignedNumber(true, new IntegerConstant('1')))),
50                $values->boolean(new UtilityOption(new Name('verbose'), new SignedNumber(false, new NumericConstant('1.0')))),
51            ],
52        );
53    }
54
55    public function testIntegerReadsIntegersThatFitIn32Bits(): void
56    {
57        $values = new OptionArguments();
58        self::assertSame(
59            [2147483647, -2147483647, null, null, null],
60            [
61                $values->integer(new UtilityOption(new Name('parallel'), new SignedNumber(false, new IntegerConstant('2147483647')))),
62                $values->integer(new UtilityOption(new Name('parallel'), new SignedNumber(true, new IntegerConstant('2147483647')))),
63                $values->integer(new UtilityOption(new Name('parallel'), new SignedNumber(false, new NumericConstant('2147483648')))),
64                $values->integer(new UtilityOption(new Name('parallel'), new StringConstant('2'))),
65                $values->integer(new UtilityOption(new Name('parallel'))),
66            ],
67        );
68    }
69
70    public function testEnabledReadsTheLastOccurrence(): void
71    {
72        $options = [new UtilityOption(new Name('full')), new UtilityOption(new Name('full'), Toggle::False), new UtilityOption(new Name('freeze'), new StringConstant('maybe'))];
73        self::assertSame(
74            [false, true, false],
75            [(new OptionArguments())->enabled($options, 'full', true), (new OptionArguments())->enabled($options, 'freeze', true), (new OptionArguments())->enabled($options, 'verbose', false)],
76        );
77    }
78}
79