packages/sql-semantics-mysql/tests/Unit/Statement/Call/ExtractTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Call;
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\Extract;
20use SqlSemantics\Platform\MySql\Statement\Expression\IntervalUnit;
21use SqlSemantics\Platform\MySql\Statement\Literal\StringLiteral;
22use SqlSemantics\Platform\MySql\Statement\Name\ColumnUse;
23
24#[CoversClass(Extract::class)]
25#[Small]
26final class ExtractTest extends TestCase
27{
28 public function testDeriveScalarAnswersAnInteger(): void
29 {
30 $platform = new Platform();
31 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
32 $derivation = new Derivation($platform->context($profile, null, [], false));
33 $fact = $derivation->scalar(new Extract(IntervalUnit::DayHour, new StringLiteral(['x'])), $derivation->environment());
34
35 self::assertEquals(new Known(TypeClass::Integer->descriptor()), $fact->type);
36 self::assertSame(Nullability::Nullable, $fact->nullability);
37 }
38
39 public function testRenderWritesTheUnit(): void
40 {
41 $platform = new Platform();
42 $profile = $platform->profile('mysql-8.4.7', null, ParameterStyle::Native);
43 $out = new Output($platform->codec($profile));
44 (new Extract(IntervalUnit::DayHour, new ColumnUse(new Name('a'))))->render($out);
45
46 self::assertSame('EXTRACT(DAY_HOUR FROM a)', (new Lexical())->join($out->pieces()));
47 }
48}
49