packages/sql-semantics-postgres/tests/Unit/Statement/Invocation/Text/TrimTest.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\Dependent;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
19use SqlSemantics\Platform\PostgreSql\Platform;
20use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
21use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Text\Trim;
22use SqlSemantics\Platform\PostgreSql\Statement\Invocation\Text\TrimSide;
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(Trim::class)]
29#[Small]
30final class TrimTest extends TestCase
31{
32 public function testRejectsNoString(): void
33 {
34 $this->expectExceptionMessage('TRIM takes at least one string.');
35 new Trim(TrimSide::Both, null, []);
36 }
37
38 public function testOutputNameIsTheFunctionCalled(): void
39 {
40 self::assertSame('ltrim', (new Trim(TrimSide::Leading, null, [new NullLiteral()]))->outputName()->value);
41 }
42
43 public function testDeriveScalarPassesTheCharactersLast(): void
44 {
45 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
46 $fact = $derivation->scalar(new Trim(TrimSide::Trailing, new Constant(new StringConstant('x')), [new Constant(new StringConstant('axx'))]), $derivation->environment());
47 self::assertEquals(new ScalarFact(new Known(Builtin::Text), Nullability::NotNull), $fact);
48 }
49
50 public function testDeriveScalarDependsForTooManyStrings(): void
51 {
52 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
53 $fact = $derivation->scalar(new Trim(TrimSide::Both, new Constant(new StringConstant('x')), [new Constant(new StringConstant('a')), new Constant(new StringConstant('b'))]), $derivation->environment());
54 self::assertInstanceOf(Dependent::class, $fact->type);
55 }
56
57 public function testRenderLeavesTheDefaultSideUnwritten(): void
58 {
59 $trim = new Trim(TrimSide::Both, new Constant(new StringConstant('x')), [new Constant(new StringConstant('xax'))]);
60 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
61 $trim->render($out);
62 (new Trim(TrimSide::Leading, null, [new Constant(new StringConstant('xax'))]))->render($out);
63 self::assertSame('TRIM(\'x\' FROM \'xax\') TRIM(LEADING \'xax\')', (new Lexical())->join($out->pieces()));
64 }
65}
66