packages/sql-semantics-postgres/tests/Unit/Statement/Expression/BinaryOperationTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression;
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\Identifier\QualifiedName;
17use SqlSemantics\Platform\PostgreSql\Model\Statement\Reference\Missing\UndeclaredRoutine;
18use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Dependent;
19use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
20use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Nullability;
21use SqlSemantics\Platform\PostgreSql\Platform;
22use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
23use SqlSemantics\Platform\PostgreSql\Statement\Expression\BinaryOperation;
24use SqlSemantics\Platform\PostgreSql\Statement\Expression\Grouped;
25use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
26use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
27use SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral;
28use SqlSemantics\Platform\PostgreSql\Statement\Literal\NumericConstant;
29use SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant;
30use SqlSemantics\Platform\PostgreSql\Statement\Name\OperatorName;
31use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
32
33#[CoversClass(BinaryOperation::class)]
34#[Small]
35final class BinaryOperationTest extends TestCase
36{
37    public function testOutputNameIsNone(): void
38    {
39        self::assertNull((new BinaryOperation(new OperatorName(new Name('=')), new Constant(new IntegerConstant('1')), new Constant(new IntegerConstant('2'))))->outputName());
40    }
41
42    public function testDeriveScalarIsBooleanForCatalogComparisons(): void
43    {
44        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
45        $fact = $derivation->scalar(new BinaryOperation(new OperatorName(new Name('<')), new Constant(new IntegerConstant('1')), new Constant(new StringConstant('2'))), $derivation->environment());
46        self::assertEquals(new Known(Builtin::Bool), $fact->type);
47        self::assertSame(Nullability::NotNull, $fact->nullability);
48    }
49
50    public function testDeriveScalarIsNullableWhenAnOperandIsNull(): void
51    {
52        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
53        $fact = $derivation->scalar(new BinaryOperation(new OperatorName(new Name('=')), new Constant(new IntegerConstant('1')), new NullLiteral()), $derivation->environment());
54        self::assertEquals(new Known(Builtin::Bool), $fact->type);
55        self::assertSame(Nullability::Nullable, $fact->nullability);
56    }
57
58    public function testDeriveScalarPromotesAnIntegerAndANumeric(): void
59    {
60        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
61        $fact = $derivation->scalar(new BinaryOperation(new OperatorName(new Name('*')), new Constant(new IntegerConstant('2')), new Constant(new NumericConstant('1.5'))), $derivation->environment());
62        self::assertEquals(new Known(Builtin::Numeric), $fact->type);
63    }
64
65    public function testDeriveScalarDependsOnAnOperatorOutsideTheCatalog(): void
66    {
67        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
68        $fact = $derivation->scalar(new BinaryOperation(new OperatorName(new Name('<->')), new Constant(new IntegerConstant('1')), new Constant(new IntegerConstant('2'))), $derivation->environment());
69        self::assertEquals(new Dependent([new UndeclaredRoutine(new QualifiedName(new Name('<->')))]), $fact->type);
70    }
71
72    public function testRenderWritesTheOperatorBetweenTheOperands(): void
73    {
74        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
75        (new BinaryOperation(new OperatorName(new Name('+')), new Constant(new IntegerConstant('1')), new BinaryOperation(new OperatorName(new Name('*')), new Constant(new IntegerConstant('2')), new Constant(new IntegerConstant('3')))))->render($out);
76        self::assertSame('1 + 2 * 3', (new Lexical())->join($out->pieces()));
77    }
78
79    public function testRejectsALeftOperandThatBindsWeaker(): void
80    {
81        $sum = new BinaryOperation(new OperatorName(new Name('+')), new Constant(new IntegerConstant('1')), new Constant(new IntegerConstant('2')));
82        $this->expectExceptionMessage('The left operand needs parentheses to keep its place.');
83        new BinaryOperation(new OperatorName(new Name('*')), $sum, new Constant(new IntegerConstant('3')));
84    }
85
86    public function testRejectsARightOperandOfTheSameLevel(): void
87    {
88        $difference = new BinaryOperation(new OperatorName(new Name('-')), new Constant(new IntegerConstant('1')), new Constant(new IntegerConstant('2')));
89        $this->expectExceptionMessage('The right operand needs parentheses to keep its place.');
90        new BinaryOperation(new OperatorName(new Name('-')), new Constant(new IntegerConstant('3')), $difference);
91    }
92
93    public function testAcceptsAGroupedOperand(): void
94    {
95        $sum = new BinaryOperation(new OperatorName(new Name('+')), new Constant(new IntegerConstant('1')), new Constant(new IntegerConstant('2')));
96        self::assertInstanceOf(Grouped::class, (new BinaryOperation(new OperatorName(new Name('*')), new Grouped($sum), new Constant(new IntegerConstant('3'))))->left);
97    }
98
99    public function testRejectsAQualifiedOperatorWithoutTheOperatorSyntax(): void
100    {
101        $this->expectExceptionMessage('An expression writes a qualified operator with OPERATOR(...).');
102        new BinaryOperation(new OperatorName(new Name('+'), [new Name('pg_catalog')]), new Constant(new IntegerConstant('1')), new Constant(new IntegerConstant('2')));
103    }
104}
105