packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Text/SimilarSubstringTest.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\Expression\Operator\PatternMatch;
21use SqlSemantics\Platform\PostgreSql\Statement\Expression\Operator\PatternOperator;
22use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Text\SimilarSubstring;
23use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
25use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
26use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
27
28#[CoversClass(SimilarSubstring::class)]
29#[Small]
30final class SimilarSubstringTest extends TestCase
31{
32    public function testOutputNameIsSubstring(): void
33    {
34        self::assertSame('substring', (new SimilarSubstring(new NullLiteral(), new NullLiteral(), new NullLiteral()))->outputName()->value);
35    }
36
37    public function testDeriveScalarIsText(): void
38    {
39        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
40        $fact = $derivation->scalar(new SimilarSubstring(new Constant(new StringConstant('abc')), new Constant(new StringConstant('a')), new Constant(new StringConstant('#'))), $derivation->environment());
41        self::assertEquals(new ScalarFact(new Known(Builtin::Text), Nullability::NotNull), $fact);
42    }
43
44    public function testRenderWritesTheKeywords(): void
45    {
46        $substring = new SimilarSubstring(new Constant(new StringConstant('abc')), new Constant(new StringConstant('a')), new Constant(new StringConstant('#')));
47        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
48        $substring->render($out);
49        self::assertSame('SUBSTRING(\'abc\' SIMILAR \'a\' ESCAPE \'#\')', (new Lexical())->join($out->pieces()));
50    }
51
52    public function testRejectsAStringThatWouldLoseItsPlaceBeforeSimilar(): void
53    {
54        $this->expectExceptionMessage('The string needs parentheses to keep its place before SIMILAR.');
55        new SimilarSubstring(new PatternMatch(new NullLiteral(), PatternOperator::Like, false, new NullLiteral()), new NullLiteral(), new NullLiteral());
56    }
57
58    public function testRejectsAPatternThatWouldTakeTheEscape(): void
59    {
60        $this->expectExceptionMessage('A pattern ending in a pattern match without ESCAPE would take the escape; group it.');
61        new SimilarSubstring(new NullLiteral(), new PatternMatch(new NullLiteral(), PatternOperator::Like, false, new NullLiteral()), new NullLiteral());
62    }
63}
64