packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Text/SubstringTest.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\Substring;
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(Substring::class)]
28#[Small]
29final class SubstringTest extends TestCase
30{
31 public function testRejectsNeitherStartNorCount(): void
32 {
33 $this->expectExceptionMessage('SUBSTRING takes FROM, FOR or both.');
34 new Substring(new NullLiteral());
35 }
36
37 public function testRejectsCountFirstWithoutBoth(): void
38 {
39 $this->expectExceptionMessage('FOR is written before FROM only when both are written.');
40 new Substring(new NullLiteral(), new Constant(new IntegerConstant('1')), null, true);
41 }
42
43 public function testOutputNameIsSubstring(): void
44 {
45 self::assertSame('substring', (new Substring(new NullLiteral(), new Constant(new IntegerConstant('1'))))->outputName()->value);
46 }
47
48 public function testDeriveScalarTypesTheCountOnlyFormAsAnIntegerCall(): void
49 {
50 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
51 $fact = $derivation->scalar(new Substring(new Constant(new StringConstant('abc')), null, new Constant(new StringConstant('2'))), $derivation->environment());
52 self::assertEquals(new ScalarFact(new Known(Builtin::Text), Nullability::NotNull), $fact);
53 }
54
55 public function testRenderKeepsTheOrderWritten(): void
56 {
57 $substring = new Substring(new Constant(new StringConstant('abc')), new Constant(new IntegerConstant('1')), new Constant(new IntegerConstant('1')), true);
58 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
59 $substring->render($out);
60 self::assertSame('SUBSTRING(\'abc\' FOR 1 FROM 1)', (new Lexical())->join($out->pieces()));
61 }
62}
63