packages/sql-semantics-mysql/tests/Unit/Statement/Expression/Operator/IntervalArithmeticTest.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\Type\Known;
15use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
16use SqlSemantics\Platform\MySql\Platform;
17use SqlSemantics\Platform\MySql\Statement\Expression\IntervalUnit;
18use SqlSemantics\Platform\MySql\Statement\Expression\Operator\Arithmetic;
19use SqlSemantics\Platform\MySql\Statement\Expression\Operator\ArithmeticOperator;
20use SqlSemantics\Platform\MySql\Statement\Expression\Operator\Interval;
21use SqlSemantics\Platform\MySql\Statement\Expression\Operator\IntervalArithmetic;
22use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
23use SqlSemantics\Platform\MySql\Statement\Literal\TemporalForm;
24use SqlSemantics\Platform\MySql\Statement\Literal\TemporalLiteral;
25use SqlSemantics\Platform\MySql\Statement\Type\Kind\TemporalKind;
26use SqlSemantics\Platform\MySql\Statement\Type\Temporal;
27
28#[CoversClass(IntervalArithmetic::class)]
29#[Medium]
30final class IntervalArithmeticTest extends TestCase
31{
32    public function testDeriveScalarKeepsADateForADateUnit(): void
33    {
34        $platform = new Platform();
35        $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
36        $fact = $derivation->scalar(new IntervalArithmetic(new TemporalLiteral(TemporalForm::Date, '2024-01-31'), new Interval(new NumberLiteral('1'), IntervalUnit::Month), true), $derivation->environment());
37
38        self::assertEquals([new Known(new Temporal(TemporalKind::Date)), Nullability::Nullable], [$fact->type, $fact->nullability]);
39    }
40
41    public function testRenderWritesTheSignBeforeTheInterval(): void
42    {
43        $platform = new Platform();
44        $out = new Output($platform->codec($platform->profile('mysql-8.4.7', null, ParameterStyle::Native)));
45        (new IntervalArithmetic(new NumberLiteral('1'), new Interval(new NumberLiteral('2'), IntervalUnit::Day), true))->render($out);
46
47        self::assertSame('1 - INTERVAL 2 DAY', (new Lexical())->join($out->pieces()));
48    }
49
50    public function testAMultiplicativeOperandIsAccepted(): void
51    {
52        $platform = new Platform();
53        $out = new Output($platform->codec($platform->profile('mysql-8.4.7', null, ParameterStyle::Native)));
54        (new IntervalArithmetic(new Arithmetic(ArithmeticOperator::Multiply, new NumberLiteral('1'), new NumberLiteral('2')), new Interval(new NumberLiteral('3'), IntervalUnit::Day)))->render($out);
55
56        self::assertSame('1 * 2 + INTERVAL 3 DAY', (new Lexical())->join($out->pieces()));
57    }
58
59    public function testABitOperandIsRejected(): void
60    {
61        $this->expectExceptionMessage('The operand of interval arithmetic needs a grouping to keep its place.');
62
63        new IntervalArithmetic(new Arithmetic(ArithmeticOperator::BitOr, new NumberLiteral('1'), new NumberLiteral('2')), new Interval(new NumberLiteral('3'), IntervalUnit::Day));
64    }
65}
66