packages/sql-semantics-postgres/tests/Unit/Statement/Utility/DoBlockTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Utility;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic;
11use SqlSemantics\Platform\PostgreSql\Semantics;
12use SqlSemantics\Platform\PostgreSql\Statement\Utility\DoBlock;
13
14#[CoversClass(DoBlock::class)]
15#[Medium]
16final class DoBlockTest extends TestCase
17{
18 public function testCodeAnswersTheOnlyCodeString(): void
19 {
20 $statement = (new Semantics())->inspect("DO LANGUAGE plpgsql 'BEGIN END'")->statement;
21 self::assertInstanceOf(DoBlock::class, $statement);
22 self::assertSame('BEGIN END', $statement->code()?->value);
23 }
24
25 public function testCodeAnswersNullForTwoCodeStrings(): void
26 {
27 $statement = (new Semantics())->inspect("DO 'a' 'b'")->statement;
28 self::assertInstanceOf(DoBlock::class, $statement);
29 self::assertNull($statement->code());
30 }
31
32 public function testLanguageAnswersTheOnlyLanguage(): void
33 {
34 $statement = (new Semantics())->inspect("DO 'x' LANGUAGE 'sql'")->statement;
35 self::assertInstanceOf(DoBlock::class, $statement);
36 self::assertSame('sql', $statement->language()?->name());
37 }
38
39 public function testLanguageAnswersNullWithoutALanguage(): void
40 {
41 $statement = (new Semantics())->inspect("DO 'x'")->statement;
42 self::assertInstanceOf(DoBlock::class, $statement);
43 self::assertNull($statement->language());
44 }
45
46 public function testDeriveStatementReportsMissingCodeAndRepeatedItems(): void
47 {
48 $semantics = new Semantics();
49 self::assertSame(
50 [['no inline code specified'], ['conflicting or redundant options'], ['conflicting or redundant options'], []],
51 [
52 array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect('DO LANGUAGE plpgsql')->facts->diagnostics),
53 array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect("DO 'a' 'b'")->facts->diagnostics),
54 array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect("DO 'a' LANGUAGE sql LANGUAGE plpgsql")->facts->diagnostics),
55 array_map(static fn (Diagnostic $diagnostic): string => $diagnostic->message(), $semantics->inspect("DO 'a' LANGUAGE sql")->facts->diagnostics),
56 ],
57 );
58 }
59
60 public function testRenderWritesTheItemsInOrder(): void
61 {
62 self::assertSame("DO LANGUAGE plpgsql 'BEGIN END'", (new Semantics())->inspect('DO LANGUAGE plpgsql $$BEGIN END$$')->toString());
63 }
64}
65