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

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression;
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\Type\Known;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
16use SqlSemantics\Platform\MySql\Platform;
17use SqlSemantics\Platform\MySql\Statement\Expression\Logical;
18use SqlSemantics\Platform\MySql\Statement\Expression\LogicalOperator;
19use SqlSemantics\Platform\MySql\Statement\Literal\NullLiteral;
20use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
21use SqlSemantics\Platform\MySql\Statement\Type\Integral;
22use SqlSemantics\Platform\MySql\Statement\Type\Kind\IntegralKind;
23
24#[CoversClass(Logical::class)]
25#[Medium]
26final class LogicalTest extends TestCase
27{
28    public function testDeriveScalarIsAnIntegerThatIsNullWhenAnOperandIs(): void
29    {
30        $platform = new Platform();
31        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
32        $and = new Logical(LogicalOperator::And, new NumberLiteral('1'), new NumberLiteral('2'));
33        $or = new Logical(LogicalOperator::Or, new NumberLiteral('1'), new NullLiteral());
34
35        self::assertEquals(new Known(new Integral(IntegralKind::BigInt)), $derivation->scalar($and, $derivation->environment())->type);
36        self::assertSame(Nullability::NotNull, $derivation->scalar($and, $derivation->environment())->nullability);
37        self::assertSame(Nullability::Nullable, $derivation->scalar($or, $derivation->environment())->nullability);
38    }
39
40    public function testRenderWritesTheKeywordAndKeepsTheLeftAssociation(): void
41    {
42        $platform = new Platform();
43        $out = new Output($platform->codec($platform->profile('mysql-8.4.7', null, ParameterStyle::Native)));
44        (new Logical(LogicalOperator::Or, new Logical(LogicalOperator::Or, new NumberLiteral('1'), new NumberLiteral('2')), new Logical(LogicalOperator::And, new NumberLiteral('3'), new NumberLiteral('4'))))->render($out);
45
46        self::assertSame('1 OR 2 OR 3 AND 4', (new Lexical())->join($out->pieces()));
47    }
48
49    public function testAWeakerLeftOperandIsRejected(): void
50    {
51        $this->expectExceptionMessage('The left operand of AND needs a grouping to keep its place.');
52
53        new Logical(LogicalOperator::And, new Logical(LogicalOperator::Xor, new NumberLiteral('1'), new NumberLiteral('2')), new NumberLiteral('3'));
54    }
55
56    public function testAnEqualRightOperandIsRejected(): void
57    {
58        $this->expectExceptionMessage('The right operand of OR needs a grouping to keep its place.');
59
60        new Logical(LogicalOperator::Or, new NumberLiteral('1'), new Logical(LogicalOperator::Or, new NumberLiteral('2'), new NumberLiteral('3')));
61    }
62}
63