packages/sql-semantics-postgres/tests/Unit/Statement/Object/Attribute/BooleanArgumentTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Object\Attribute;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
13use SqlSemantics\Platform\PostgreSql\Model\Rendering\Lexical;
14use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name;
16use SqlSemantics\Platform\PostgreSql\Platform;
17use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
18use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
19use SqlSemantics\Platform\PostgreSql\Statement\Literal\SignedNumber;
20use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
21use SqlSemantics\Platform\PostgreSql\Statement\Name\DottedName;
22use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\BooleanArgument;
23use SqlSemantics\Platform\PostgreSql\Statement\Object\Attribute\Reading;
24use SqlSemantics\Platform\PostgreSql\Statement\Option\KeywordWord;
25use SqlSemantics\Platform\PostgreSql\Statement\Type\Designation\NamedDesignation;
26use SqlSemantics\Platform\PostgreSql\Statement\Type\TypeName;
27
28#[CoversClass(BooleanArgument::class)]
29#[Small]
30final class BooleanArgumentTest extends TestCase
31{
32    public function testValueReadsEachSpelling(): void
33    {
34        self::assertSame([true, false, true, false, true], [(new BooleanArgument())->value(), (new BooleanArgument(new SignedNumber(true, new IntegerConstant('0'))))->value(), (new BooleanArgument(new KeywordWord(new Name('on'))))->value(), (new BooleanArgument(new TypeName(new NamedDesignation(new DottedName([new Name('OFF')])))))->value(), (new BooleanArgument(new StringConstant('True')))->value()]);
35    }
36
37    public function testFitsOnlyABooleanReading(): void
38    {
39        self::assertSame([true, false], [(new BooleanArgument())->fits(Reading::Boolean), (new BooleanArgument())->fits(Reading::Text)]);
40    }
41
42    public function testDeriveClauseRecordsNothingForAWord(): void
43    {
44        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
45        (new BooleanArgument(new StringConstant('on')))->deriveClause($derivation, $derivation->environment());
46        self::assertSame([], $derivation->facts()->diagnostics);
47    }
48
49    public function testRenderWritesNothingForAnAttributeGivenAlone(): void
50    {
51        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
52        (new BooleanArgument())->render($out);
53        self::assertSame('', (new Lexical())->join($out->pieces()));
54    }
55
56    public function testRejectsAnotherInteger(): void
57    {
58        $this->expectExceptionMessage('A Boolean attribute is given alone, as 0 or 1, or as true, false, on or off.');
59        new BooleanArgument(new SignedNumber(false, new IntegerConstant('2')));
60    }
61}
62