packages/sql-semantics-mysql/tests/Unit/Statement/Call/Temporal/TimestampCallTest.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\TimestampCall;
20use SqlSemantics\Platform\MySql\Statement\Call\Temporal\TimestampOperation;
21use SqlSemantics\Platform\MySql\Statement\Expression\IntervalUnit;
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\Name\ColumnUse;
26
27#[CoversClass(TimestampCall::class)]
28#[Small]
29final class TimestampCallTest extends TestCase
30{
31 public function testDeriveScalarTypesAnAdditionAsDateArithmetic(): void
32 {
33 $platform = new Platform();
34 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
35 $derivation = new Derivation($platform->context($profile, null, [], false));
36 $fact = $derivation->scalar(new TimestampCall(TimestampOperation::Add, IntervalUnit::Day, new NumberLiteral('1'), new TemporalLiteral(TemporalForm::Date, '2024-01-01')), $derivation->environment());
37
38 self::assertEquals(new Known(TypeClass::Date->descriptor()), $fact->type);
39 }
40
41 public function testDeriveScalarTypesADifferenceAsAnInteger(): void
42 {
43 $platform = new Platform();
44 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
45 $derivation = new Derivation($platform->context($profile, null, [], false));
46 $fact = $derivation->scalar(new TimestampCall(TimestampOperation::Difference, IntervalUnit::Day, new TemporalLiteral(TemporalForm::Date, '2024-01-01'), new TemporalLiteral(TemporalForm::Date, '2024-01-01')), $derivation->environment());
47
48 self::assertEquals(new Known(TypeClass::Integer->descriptor()), $fact->type);
49 self::assertSame(Nullability::Nullable, $fact->nullability);
50 }
51
52 public function testRenderWritesTheUnitFirst(): void
53 {
54 $platform = new Platform();
55 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
56 $out = new Output($platform->codec($profile));
57 (new TimestampCall(TimestampOperation::Difference, IntervalUnit::Week, new ColumnUse(new Name('a')), new ColumnUse(new Name('a'))))->render($out);
58
59 self::assertSame('TIMESTAMPDIFF(WEEK, a, a)', (new Lexical())->join($out->pieces()));
60 }
61}
62