packages/sql-semantics-postgres/tests/Unit/Lowering/Utility/CommandRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Utility;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Lowering\Utility\CommandRule;
11use SqlSemantics\Platform\PostgreSql\Semantics;
12use SqlSemantics\Platform\PostgreSql\Statement\Utility\Call;
13use SqlSemantics\Platform\PostgreSql\Statement\Utility\DoBlock;
14use SqlSemantics\Platform\PostgreSql\Statement\Utility\DoLanguage;
15use SqlSemantics\Platform\PostgreSql\Statement\Utility\Notify;
16
17#[CoversClass(CommandRule::class)]
18#[Medium]
19final class CommandRuleTest extends TestCase
20{
21    public function testStatementLowersEachCommand(): void
22    {
23        $semantics = new Semantics();
24        self::assertSame(
25            ['NOTIFY c', 'LISTEN c', 'UNLISTEN c', 'UNLISTEN *', "LOAD 'x'"],
26            [$semantics->inspect('NOTIFY c')->toString(), $semantics->inspect('LISTEN c')->toString(), $semantics->inspect('UNLISTEN c')->toString(), $semantics->inspect('UNLISTEN *')->toString(), $semantics->inspect("LOAD 'x'")->toString()],
27        );
28    }
29
30    public function testCallLowersTheProcedureCall(): void
31    {
32        $statement = (new Semantics())->inspect('CALL s.p(VARIADIC ARRAY[1])')->statement;
33        self::assertInstanceOf(Call::class, $statement);
34        self::assertSame('p', $statement->call->name->last()->value);
35    }
36
37    public function testItemsKeepsTheOrderWritten(): void
38    {
39        $statement = (new Semantics())->inspect("DO LANGUAGE sql 'a' LANGUAGE plpgsql")->statement;
40        self::assertInstanceOf(DoBlock::class, $statement);
41        self::assertSame([DoLanguage::class, \SqlSemantics\Platform\PostgreSql\Statement\Literal\StringConstant::class, DoLanguage::class], array_map(static fn (object $item): string => $item::class, $statement->items));
42    }
43
44    public function testPayloadIsNullWithoutAPayload(): void
45    {
46        $with = (new Semantics())->inspect("NOTIFY c, 'p'")->statement;
47        $without = (new Semantics())->inspect('NOTIFY c')->statement;
48        self::assertInstanceOf(Notify::class, $with);
49        self::assertInstanceOf(Notify::class, $without);
50        self::assertSame(['p', null], [$with->payload?->value, $without->payload]);
51    }
52}
53