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

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Predicate;
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\Nullability;
16use SqlSemantics\Platform\MySql\Platform;
17use SqlSemantics\Platform\MySql\Statement\Expression\Operator\Arithmetic;
18use SqlSemantics\Platform\MySql\Statement\Expression\Operator\ArithmeticOperator;
19use SqlSemantics\Platform\MySql\Statement\Expression\Predicate\Between;
20use SqlSemantics\Platform\MySql\Statement\Literal\NullLiteral;
21use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
22use SqlSemantics\Platform\MySql\Statement\Variable\UserVariable;
23use SqlSemantics\Platform\MySql\Statement\Variable\VariableAssignment;
24
25#[CoversClass(Between::class)]
26#[Medium]
27final class BetweenTest extends TestCase
28{
29    public function testDeriveScalarIsNullWhenABoundCanBe(): void
30    {
31        $platform = new Platform();
32        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
33
34        self::assertSame(Nullability::Nullable, $derivation->scalar(new Between(new NumberLiteral('1'), new NullLiteral(), new NumberLiteral('2')), $derivation->environment())->nullability);
35        self::assertSame(Nullability::NotNull, $derivation->scalar(new Between(new NumberLiteral('1'), new NumberLiteral('0'), new NumberLiteral('2')), $derivation->environment())->nullability);
36    }
37
38    public function testRenderNestsARangeInTheUpperBound(): void
39    {
40        $platform = new Platform();
41        $out = new Output($platform->codec($platform->profile('mysql-8.4.7', null, ParameterStyle::Native)));
42        (new Between(new NumberLiteral('1'), new NumberLiteral('2'), new Between(new NumberLiteral('3'), new NumberLiteral('4'), new NumberLiteral('5')), true))->render($out);
43
44        self::assertSame('1 NOT BETWEEN 2 AND 3 BETWEEN 4 AND 5', (new Lexical())->join($out->pieces()));
45    }
46
47    public function testALowerBoundThatTakesTheAndIsRejected(): void
48    {
49        $this->expectExceptionMessage('The lower bound of BETWEEN needs a grouping to keep its place.');
50
51        new Between(new NumberLiteral('1'), new Arithmetic(ArithmeticOperator::Plus, new NumberLiteral('2'), new VariableAssignment(new UserVariable(new Name('v')), new NumberLiteral('3'))), new NumberLiteral('4'));
52    }
53
54    public function testAnUpperBoundRangeOnTheLowerSideIsRejected(): void
55    {
56        $this->expectExceptionMessage('The lower bound of BETWEEN needs a grouping to keep its place.');
57
58        new Between(new NumberLiteral('1'), new Between(new NumberLiteral('2'), new NumberLiteral('3'), new NumberLiteral('4')), new NumberLiteral('5'));
59    }
60}
61