packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Text/OverlayTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Invocation\Text;
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\Fact\ScalarFact;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
18use SqlSemantics\Platform\PostgreSql\Platform;
19use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
20use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Text\Overlay;
21use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
22use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
23use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
25use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
26
27#[CoversClass(Overlay::class)]
28#[Small]
29final class OverlayTest extends TestCase
30{
31    public function testOutputNameIsOverlay(): void
32    {
33        self::assertSame('overlay', (new Overlay(new NullLiteral(), new NullLiteral(), new NullLiteral()))->outputName()->value);
34    }
35
36    public function testDeriveScalarIsTheStringType(): void
37    {
38        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
39        $fact = $derivation->scalar(new Overlay(new Constant(new StringConstant('abc')), new Constant(new StringConstant('x')), new Constant(new IntegerConstant('1')), new Constant(new IntegerConstant('1'))), $derivation->environment());
40        self::assertEquals(new ScalarFact(new Known(Builtin::Text), Nullability::NotNull), $fact);
41    }
42
43    public function testRenderWritesTheKeywords(): void
44    {
45        $overlay = new Overlay(new Constant(new StringConstant('abc')), new Constant(new StringConstant('x')), new Constant(new IntegerConstant('1')), new Constant(new IntegerConstant('1')));
46        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
47        $overlay->render($out);
48        self::assertSame('OVERLAY(\'abc\' PLACING \'x\' FROM 1 FOR 1)', (new Lexical())->join($out->pieces()));
49    }
50}
51