packages/sql-semantics-mysql/tests/Unit/Statement/Call/Temporal/DateArithmeticTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Call\Temporal;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
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\Known;
16use SqlSemantics\Platform\MySql\Model\Statement\Type\Nullability;
17use SqlSemantics\Platform\MySql\Platform;
18use SqlSemantics\Platform\MySql\Rules\Call\TypeClass;
19use SqlSemantics\Platform\MySql\Statement\Call\Temporal\DateArithmetic;
20use SqlSemantics\Platform\MySql\Statement\Expression\IntervalUnit;
21use SqlSemantics\Platform\MySql\Statement\Literal\NumberLiteral;
22use SqlSemantics\Platform\MySql\Statement\Literal\TemporalForm;
23use SqlSemantics\Platform\MySql\Statement\Literal\TemporalLiteral;
24use SqlSemantics\Platform\MySql\Statement\Name\ColumnUse;
25
26#[CoversClass(DateArithmetic::class)]
27#[Small]
28final class DateArithmeticTest extends TestCase
29{
30 public function testDeriveScalarFollowsTheDateAndTheUnit(): void
31 {
32 $platform = new Platform();
33 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
34 $derivation = new Derivation($platform->context($profile, null, [], false));
35 $fact = $derivation->scalar(new DateArithmetic(true, new TemporalLiteral(TemporalForm::Date, '2024-01-01'), new NumberLiteral('1'), IntervalUnit::Second), $derivation->environment());
36
37 self::assertEquals(new Known(TypeClass::DateTime->descriptor()), $fact->type);
38 self::assertSame(Nullability::Nullable, $fact->nullability);
39 }
40
41 public function testRenderWritesDateSub(): void
42 {
43 $platform = new Platform();
44 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
45 $out = new Output($platform->codec($profile));
46 (new DateArithmetic(true, new ColumnUse(new Name('a')), new NumberLiteral('1'), IntervalUnit::Second))->render($out);
47
48 self::assertSame('DATE_SUB(a, INTERVAL 1 SECOND)', (new Lexical())->join($out->pieces()));
49 }
50}
51