packages/sql-semantics-mysql/tests/Unit/Statement/Expression/Operator/UnaryTest.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\Collated;
18use SqlSemantics\Platform\MySql\Statement\Expression\Operator\Concatenation;
19use SqlSemantics\Platform\MySql\Statement\Expression\Operator\Unary;
20use SqlSemantics\Platform\MySql\Statement\Expression\Operator\UnaryOperator;
21use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
22use SqlSemantics\Platform\MySql\Statement\Literal\StringLiteral;
23use SqlSemantics\Platform\MySql\Statement\Type\Character;
24use SqlSemantics\Platform\MySql\Statement\Type\Floating;
25use SqlSemantics\Platform\MySql\Statement\Type\Integral;
26use SqlSemantics\Platform\MySql\Statement\Type\Kind\CharacterKind;
27use SqlSemantics\Platform\MySql\Statement\Type\Kind\FloatingKind;
28use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
29use SqlSemantics\Platform\MySql\Statement\Type\Kind\NumericModifier;
30
31#[CoversClass(Unary::class)]
32#[Medium]
33final class UnaryTest extends TestCase
34{
35    public function testDeriveScalarTypesEachOperator(): void
36    {
37        $platform = new Platform();
38        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
39        $plus = $derivation->scalar(new Unary(UnaryOperator::Plus, new StringLiteral(['7'])), $derivation->environment());
40        $minus = $derivation->scalar(new Unary(UnaryOperator::Minus, new StringLiteral(['7'])), $derivation->environment());
41        $invert = $derivation->scalar(new Unary(UnaryOperator::Invert, new StringLiteral(['7'])), $derivation->environment());
42        $not = $derivation->scalar(new Unary(UnaryOperator::Not, new StringLiteral(['7'])), $derivation->environment());
43
44        self::assertEquals([new Known(new Character(CharacterKind::VarChar)), new Known(new Floating(FloatingKind::Double)), new Known(new Integral(IntegralKind::BigInt, null, [NumericModifier::Unsigned])), new Known(new Integral(IntegralKind::BigInt))], [$plus->type, $minus->type, $invert->type, $not->type]);
45    }
46
47    public function testRenderNegatesACollatedOperandWithoutParentheses(): void
48    {
49        $platform = new Platform();
50        $out = new Output($platform->codec($platform->profile('mysql-8.4.7', null, ParameterStyle::Native)));
51        (new Unary(UnaryOperator::Minus, new Unary(UnaryOperator::Minus, new Collated(new NumberLiteral('1'), new Name('utf8mb4_bin')))))->render($out);
52
53        self::assertSame('- - 1 COLLATE utf8mb4_bin', (new Lexical())->join($out->pieces()));
54    }
55
56    public function testAConcatenationOperandIsRejected(): void
57    {
58        $this->expectExceptionMessage('The operand of a prefix operator needs a grouping to keep its place.');
59
60        new Unary(UnaryOperator::Minus, new Concatenation(new NumberLiteral('1'), new NumberLiteral('2')));
61    }
62}
63