packages/sql-semantics-mysql/tests/Unit/Statement/Expression/Operator/IntervalAdditionTest.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\Platform;
16use SqlSemantics\Platform\MySql\Statement\Expression\Comparison;
17use SqlSemantics\Platform\MySql\Statement\Expression\ComparisonOperator;
18use SqlSemantics\Platform\MySql\Statement\Expression\IntervalUnit;
19use SqlSemantics\Platform\MySql\Statement\Expression\Logical;
20use SqlSemantics\Platform\MySql\Statement\Expression\LogicalOperator;
21use SqlSemantics\Platform\MySql\Statement\Expression\Operator\Interval;
22use SqlSemantics\Platform\MySql\Statement\Expression\Operator\IntervalAddition;
23use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
24use SqlSemantics\Platform\MySql\Statement\Literal\TemporalForm;
25use SqlSemantics\Platform\MySql\Statement\Literal\TemporalLiteral;
26use SqlSemantics\Platform\MySql\Statement\Type\Kind\TemporalKind;
27use SqlSemantics\Platform\MySql\Statement\Type\Temporal;
28
29#[CoversClass(IntervalAddition::class)]
30#[Medium]
31final class IntervalAdditionTest extends TestCase
32{
33 public function testDeriveScalarMakesADateTimeForATimeUnit(): void
34 {
35 $platform = new Platform();
36 $derivation = new Derivation($platform->context($platform->profile('mysql-8.4.7', null, ParameterStyle::Native), null, [], true));
37
38 self::assertEquals(new Known(new Temporal(TemporalKind::DateTime)), $derivation->scalar(new IntervalAddition(new Interval(new NumberLiteral('1'), IntervalUnit::Hour), new TemporalLiteral(TemporalForm::Date, '2024-01-31')), $derivation->environment())->type);
39 }
40
41 public function testRenderLetsTheOperandExtendOverAComparison(): void
42 {
43 $platform = new Platform();
44 $out = new Output($platform->codec($platform->profile('mysql-8.4.7', null, ParameterStyle::Native)));
45 (new IntervalAddition(new Interval(new NumberLiteral('1'), IntervalUnit::Day), new Comparison(ComparisonOperator::Equal, new NumberLiteral('2'), new NumberLiteral('3'))))->render($out);
46
47 self::assertSame('INTERVAL 1 DAY + 2 = 3', (new Lexical())->join($out->pieces()));
48 }
49
50 public function testAConjunctionOperandIsRejected(): void
51 {
52 $this->expectExceptionMessage('The operand of a leading interval needs a grouping to keep its place.');
53
54 new IntervalAddition(new Interval(new NumberLiteral('1'), IntervalUnit::Day), new Logical(LogicalOperator::And, new NumberLiteral('2'), new NumberLiteral('3')));
55 }
56}
57