packages/sql-semantics-postgres/tests/Unit/Statement/Routine/Option/RoutineEstimateTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Routine\Option;
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\Platform;
16use SqlSemantics\Platform\PostgreSql\Rendering\Codec;
17use SqlSemantics\Platform\PostgreSql\Statement\Literal\IntegerConstant;
18use SqlSemantics\Platform\PostgreSql\Statement\Literal\SignedNumber;
19use SqlSemantics\Platform\PostgreSql\Statement\Routine\Option\EstimateKind;
20use SqlSemantics\Platform\PostgreSql\Statement\Routine\Option\RoutineEstimate;
21
22#[CoversClass(RoutineEstimate::class)]
23#[Small]
24final class RoutineEstimateTest extends TestCase
25{
26    public function testAlterableIsTrue(): void
27    {
28        self::assertTrue((new RoutineEstimate(EstimateKind::Rows, new SignedNumber(false, new IntegerConstant('5'))))->alterable());
29    }
30
31    public function testSettingNamesTheEstimate(): void
32    {
33        self::assertSame('rows', (new RoutineEstimate(EstimateKind::Rows, new SignedNumber(false, new IntegerConstant('5'))))->setting());
34    }
35
36    public function testDeriveClauseRecordsNothing(): void
37    {
38        $derivation = new Derivation((new Platform())->context(new LanguageProfile(GrammarRelease::PostgreSql172), null, [], true));
39        (new RoutineEstimate(EstimateKind::Cost, new SignedNumber(false, new IntegerConstant('5'))))->deriveClause($derivation, $derivation->environment());
40        self::assertSame([], $derivation->facts()->diagnostics);
41    }
42
43    public function testRenderWritesKeywordAndNumber(): void
44    {
45        $out = new Output(new Codec(GrammarRelease::PostgreSql172));
46        (new RoutineEstimate(EstimateKind::Cost, new SignedNumber(true, new IntegerConstant('5'))))->render($out);
47        self::assertSame('COST - 5', (new Lexical())->join($out->pieces()));
48    }
49}
50