packages/sql-semantics-mysql/tests/Unit/Statement/Call/TrimTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Call;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
11use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
12use SqlSemantics\Platform\MySql\Model\Rendering\Lexical;
13use SqlSemantics\Platform\MySql\Model\Rendering\Output;
14use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
16use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
17use SqlSemantics\Platform\MySql\Platform;
18use SqlSemantics\Platform\MySql\Rules\Call\TypeClass;
19use SqlSemantics\Platform\MySql\Statement\Call\Trim;
20use SqlSemantics\Platform\MySql\Statement\Call\TrimSide;
21use SqlSemantics\Platform\MySql\Statement\Literal\StringLiteral;
22use SqlSemantics\Platform\MySql\Statement\Name\ColumnUse;
23
24#[CoversClass(Trim::class)]
25#[Small]
26final class TrimTest extends TestCase
27{
28 public function testDeriveScalarAnswersAString(): void
29 {
30 $platform = new Platform();
31 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
32 $derivation = new Derivation($platform->context($profile, null, [], false));
33 $fact = $derivation->scalar(new Trim(new StringLiteral(['x']), TrimSide::Both, new StringLiteral(['x'])), $derivation->environment());
34
35 self::assertEquals(new Known(TypeClass::Character->descriptor()), $fact->type);
36 self::assertSame(Nullability::NotNull, $fact->nullability);
37 }
38
39 public function testRenderWritesEachForm(): void
40 {
41 $platform = new Platform();
42 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
43 $plain = new Output($platform->codec($profile));
44 (new Trim(new ColumnUse(new Name('a'))))->render($plain);
45 $side = new Output($platform->codec($profile));
46 (new Trim(new ColumnUse(new Name('a')), TrimSide::Trailing))->render($side);
47 $removed = new Output($platform->codec($profile));
48 (new Trim(new ColumnUse(new Name('a')), null, new StringLiteral(['x'])))->render($removed);
49
50 self::assertSame('TRIM(a)', (new Lexical())->join($plain->pieces()));
51 self::assertSame('TRIM(TRAILING FROM a)', (new Lexical())->join($side->pieces()));
52 self::assertSame("TRIM('x' FROM a)", (new Lexical())->join($removed->pieces()));
53 }
54}
55