packages/sql-semantics-postgres/tests/Unit/Statement/Expression/Predicate/NormalizationTestTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Predicate;
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\Type\Known;
16use SqlSemantics\Platform\PostgreSql\Platform;
17use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
18use SqlSemantics\Platform\PostgreSql\Statement\Expression\Operator\Negation;
19use SqlSemantics\Platform\PostgreSql\Statement\Expression\Predicate\NormalizationTest;
20use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
21use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
22use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
23use SqlSemantics\Platform\PostgreSql\Statement\Option\NormalForm;
24use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
25
26#[CoversClass(NormalizationTest::class)]
27#[Small]
28final class NormalizationTestTest extends TestCase
29{
30    public function testDeriveScalarIsBooleanForText(): void
31    {
32        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
33        $fact = $derivation->scalar(new NormalizationTest(new Constant(new StringConstant('a')), false), $derivation->environment());
34        self::assertEquals(new Known(Builtin::Bool), $fact->type);
35    }
36
37    public function testRenderWritesTheForm(): void
38    {
39        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
40        (new NormalizationTest(new NullLiteral(), true, NormalForm::Nfd))->render($out);
41        self::assertSame('NULL IS NOT NFD NORMALIZED', (new Lexical())->join($out->pieces()));
42    }
43
44    public function testRejectsANegation(): void
45    {
46        $this->expectExceptionMessage('The tested value needs parentheses to keep its place.');
47        new NormalizationTest(new Negation(new NullLiteral()), false);
48    }
49}
50