packages/sql-semantics-mysql/tests/Unit/Statement/Expression/Operator/ConcatenationTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Operator;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Mode;
11use SqlSemantics\Platform\MySql\Model\Construction\Derivation;
12use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
13use SqlSemantics\Platform\MySql\Model\Rendering\Lexical;
14use SqlSemantics\Platform\MySql\Model\Rendering\Output;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Known;
16use SqlSemantics\Platform\MySql\Platform;
17use SqlSemantics\Platform\MySql\Statement\Expression\Operator\Concatenation;
18use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
19use SqlSemantics\Platform\MySql\Statement\Literal\Radix;
20use SqlSemantics\Platform\MySql\Statement\Literal\RadixLiteral;
21use SqlSemantics\Platform\MySql\Statement\Literal\StringLiteral;
22use SqlSemantics\Platform\MySql\Statement\Type\Binary;
23use SqlSemantics\Platform\MySql\Statement\Type\Character;
24use SqlSemantics\Platform\MySql\Statement\Type\Kind\BinaryKind;
25use SqlSemantics\Platform\MySql\Statement\Type\Kind\CharacterKind;
26
27#[CoversClass(Concatenation::class)]
28#[Medium]
29final class ConcatenationTest extends TestCase
30{
31    public function testDeriveScalarIsABinaryStringWhenAnOperandIs(): void
32    {
33        $platform = new Platform();
34        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', Mode::fromString('PIPES_AS_CONCAT'), ParameterStyle::Native), null, [], true));
35        $text = $derivation->scalar(new Concatenation(new StringLiteral(['a']), new NumberLiteral('1')), $derivation->environment());
36        $bytes = $derivation->scalar(new Concatenation(new StringLiteral(['a']), new RadixLiteral(Radix::Hexadecimal, '41')), $derivation->environment());
37
38        self::assertEquals([new Known(new Character(CharacterKind::VarChar)), new Known(new Binary(BinaryKind::VarBinary))], [$text->type, $bytes->type]);
39    }
40
41    public function testDeriveScalarRejectsTheOperatorWithoutPipesAsConcat(): void
42    {
43        $platform = new Platform();
44        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
45
46        $this->expectExceptionMessage('The concatenation operator || exists only under PIPES_AS_CONCAT.');
47
48        $derivation->scalar(new Concatenation(new NumberLiteral('1'), new NumberLiteral('2')), $derivation->environment());
49    }
50
51    public function testRenderAssociatesToTheLeft(): void
52    {
53        $platform = new Platform();
54        $out = new Output($platform->codec($platform->profile('mysql-8.4.7', Mode::fromString('PIPES_AS_CONCAT'), ParameterStyle::Native)));
55        (new Concatenation(new Concatenation(new NumberLiteral('1'), new NumberLiteral('2')), new NumberLiteral('3')))->render($out);
56
57        self::assertSame('1 || 2 || 3', (new Lexical())->join($out->pieces()));
58    }
59
60    public function testAConcatenationOnTheRightIsRejected(): void
61    {
62        $this->expectExceptionMessage('The right operand of || needs a grouping to keep its place.');
63
64        new Concatenation(new NumberLiteral('1'), new Concatenation(new NumberLiteral('2'), new NumberLiteral('3')));
65    }
66}
67