packages/sql-semantics-mysql/tests/Unit/Statement/Expression/Operator/CollatedTest.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\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\Platform;
17use SqlSemantics\Platform\MySql\Statement\Expression\Operator\BinaryCast;
18use SqlSemantics\Platform\MySql\Statement\Expression\Operator\Collated;
19use SqlSemantics\Platform\MySql\Statement\Literal\StringLiteral;
20use SqlSemantics\Platform\MySql\Statement\Type\Character;
21use SqlSemantics\Platform\MySql\Statement\Type\Kind\CharacterKind;
22
23#[CoversClass(Collated::class)]
24#[Medium]
25final class CollatedTest extends TestCase
26{
27    public function testDeriveScalarHasTheTypeOfTheOperand(): void
28    {
29        $platform = new Platform();
30        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
31
32        self::assertEquals(new Known(new Character(CharacterKind::VarChar)), $derivation->scalar(new Collated(new StringLiteral(['a']), new Name('utf8mb4_bin')), $derivation->environment())->type);
33    }
34
35    public function testRenderChainsCollationsFromTheLeft(): void
36    {
37        $platform = new Platform();
38        $out = new Output($platform->codec($platform->profile('mysql-8.4.7', null, ParameterStyle::Native)));
39        (new Collated(new Collated(new StringLiteral(['a']), new Name('utf8mb4_bin')), new Name('utf8mb4_0900_ai_ci')))->render($out);
40
41        self::assertSame("'a' COLLATE utf8mb4_bin COLLATE utf8mb4_0900_ai_ci", (new Lexical())->join($out->pieces()));
42    }
43
44    public function testABinaryCastOperandIsRejected(): void
45    {
46        $this->expectExceptionMessage('The operand of COLLATE needs a grouping to keep its place.');
47
48        new Collated(new BinaryCast(new StringLiteral(['a'])), new Name('utf8mb4_bin'));
49    }
50}
51