packages/sql-semantics-postgres/tests/Unit/Statement/Expression/Step/SliceTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Expression\Step;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Construction\Derivation;
11use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
12use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
13use SqlSemantics\Platform\PostgreSql\Model\Rendering\Lexical;
14use SqlSemantics\Platform\PostgreSql\Model\Rendering\Output;
15use SqlSemantics\Platform\PostgreSql\Model\Statement\Type\Known;
16use SqlSemantics\Platform\PostgreSql\Platform;
17use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
18use SqlSemantics\Platform\PostgreSql\Statement\Expression\Step\Slice;
19use SqlSemantics\Platform\PostgreSql\Statement\Literal\Constant;
20use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
21use SqlSemantics\Platform\PostgreSql\Statement\Type\Descriptor\Builtin;
22
23#[CoversClass(Slice::class)]
24#[Small]
25final class SliceTest extends TestCase
26{
27 public function testFieldIsNone(): void
28 {
29 self::assertNull((new Slice(null, null))->field());
30 }
31
32 public function testDeriveClauseDerivesTheWrittenBounds(): void
33 {
34 $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
35 $upper = new Constant(new IntegerConstant('2'));
36 (new Slice(null, $upper))->deriveClause($derivation, $derivation->environment());
37 self::assertEquals(new Known(Builtin::Int4), $derivation->facts()->scalar($upper)->type);
38 }
39
40 public function testRenderWritesTheBounds(): void
41 {
42 $out = new Output(new Codec(GrammarRelease::PostgreSql172));
43 (new Slice(new Constant(new IntegerConstant('1')), null))->render($out);
44 self::assertSame('[1 :]', (new Lexical())->join($out->pieces()));
45 }
46}
47