packages/sql-semantics-postgres/tests/Unit/Statement/Expression/Operator/UnaryOperationTest.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\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\Identifier\Name;
16use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
17use SqlSemantics\Platform\PostgreSql\Platform;
18use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
19use SqlSemantics\Platform\PostgreSql\Statement\Expression\BinaryOperation;
20use SqlSemantics\Platform\PostgreSql\Statement\Expression\Operator\UnaryOperation;
21use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
22use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
23use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
24use SqlSemantics\Platform\PostgreSql\Statement\Literal\NumericConstant;
25use SqlSemantics\Platform\PostgreSql\Statement\Name\OperatorName;
26use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
27
28#[CoversClass(UnaryOperation::class)]
29#[Small]
30final class UnaryOperationTest extends TestCase
31{
32    public function testDeriveScalarTypesANegativeConstantByItsValue(): void
33    {
34        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
35        $fact = $derivation->scalar(new UnaryOperation(new OperatorName(new Name('-')), new Constant(new NumericConstant('2147483648'))), $derivation->environment());
36        self::assertEquals(new Known(Builtin::Int4), $fact->type);
37    }
38
39    public function testDeriveScalarKeepsTheTypeOfANegatedNumber(): void
40    {
41        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
42        $sum = new BinaryOperation(new OperatorName(new Name('^')), new Constant(new NumericConstant('1.5')), new Constant(new IntegerConstant('2')));
43        $fact = $derivation->scalar(new UnaryOperation(new OperatorName(new Name('@')), $sum), $derivation->environment());
44        self::assertEquals(new Known(Builtin::Numeric), $fact->type);
45    }
46
47    public function testRenderWritesTheOperatorFirst(): void
48    {
49        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
50        (new UnaryOperation(new OperatorName(new Name('-')), new UnaryOperation(new OperatorName(new Name('-')), new Constant(new IntegerConstant('1')))))->render($out);
51        self::assertSame('- - 1', (new Lexical())->join($out->pieces()));
52    }
53
54    public function testRejectsAnOperandThatBindsWeaker(): void
55    {
56        $product = new BinaryOperation(new OperatorName(new Name('*')), new Constant(new IntegerConstant('1')), new Constant(new IntegerConstant('2')));
57        $this->expectExceptionMessage('The operand needs parentheses to keep its place.');
58        new UnaryOperation(new OperatorName(new Name('-')), $product);
59    }
60
61    public function testRejectsAnOperatorThatCannotBePrefix(): void
62    {
63        $this->expectExceptionMessage('This operator cannot be written before an operand without OPERATOR(...).');
64        new UnaryOperation(new OperatorName(new Name('=')), new NullLiteral());
65    }
66}
67