packages/sql-semantics-postgres/tests/Unit/Statement/Literal/StringConstantTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Literal;
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\Platform;
16use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
17use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
18
19#[CoversClass(StringConstant::class)]
20#[Small]
21final class StringConstantTest extends TestCase
22{
23    public function testDeriveClauseRecordsNothing(): void
24    {
25        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
26        (new StringConstant('a'))->deriveClause($derivation, $derivation->environment());
27        self::assertSame([], $derivation->facts()->diagnostics);
28    }
29
30    public function testRenderDoublesQuotes(): void
31    {
32        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
33        (new StringConstant("it's\\n"))->render($out);
34        self::assertSame("'it''s\\n'", (new Lexical())->join($out->pieces()));
35    }
36
37    public function testRejectsAZeroByte(): void
38    {
39        $this->expectExceptionMessage('A PostgreSQL string constant holds no zero byte.');
40        new StringConstant("a\0");
41    }
42}
43