packages/sql-semantics-postgres/tests/Unit/Lowering/Leaf/FlagsTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\Leaf;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Small;
9use PHPUnit\Framework\TestCase;
10use SqlParser\PostgreSql\PostgreSqlParser;
11use SqlSemantics\Platform\PostgreSql\Lowering\Leaf\Flags;
12use SqlSemantics\Platform\PostgreSql\Lowering\Lowering;
13use SqlSemantics\Platform\PostgreSql\Model\Contract\GrammarRelease;
14use SqlSemantics\Platform\PostgreSql\Model\Contract\LanguageProfile;
15use SqlSemantics\Platform\PostgreSql\Model\Lowering\Leaves;
16use SqlSemantics\Platform\PostgreSql\Platform;
17use SqlSemantics\Platform\PostgreSql\Statement\Option\AddOrDrop;
18use SqlSemantics\Platform\PostgreSql\Statement\Option\DropBehavior;
19use SqlSemantics\Platform\PostgreSql\Statement\Option\NormalForm;
20use SqlSemantics\Platform\PostgreSql\Statement\Option\XmlOption;
21
22#[CoversClass(Flags::class)]
23#[Small]
24final class FlagsTest extends TestCase
25{
26 public function testPresentTellsWhetherTheKeywordsAreWritten(): void
27 {
28 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
29 $tree = (new PostgreSqlParser('pg-17.2'))->parse('CREATE OR REPLACE FUNCTION f() RETURNS int AS \'x\'');
30 self::assertTrue($lowering->flags->present($tree->find('opt_or_replace')[0]));
31 self::assertFalse($lowering->flags->present((new PostgreSqlParser('pg-17.2'))->parse('TRUNCATE t')->find('opt_table')[0]));
32 }
33
34 public function testPresentReportsAProductionThatIsNotAFlag(): void
35 {
36 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
37 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT 1');
38 $this->expectExceptionMessage('No semantic rule is implemented for: target_el: a_expr');
39 $lowering->flags->present($tree->find('target_el')[0]);
40 }
41
42 public function testDropBehaviorLowersTheChoiceOrNothing(): void
43 {
44 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
45 $tree = (new PostgreSqlParser('pg-17.2'))->parse('DROP TABLE t CASCADE');
46 self::assertSame(DropBehavior::Cascade, $lowering->flags->dropBehavior($tree->find('opt_drop_behavior')[0]));
47 self::assertNull($lowering->flags->dropBehavior((new PostgreSqlParser('pg-17.2'))->parse('DROP TABLE t')->find('opt_drop_behavior')[0]));
48 }
49
50 public function testAddOrDropLowersTheChoice(): void
51 {
52 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
53 $tree = (new PostgreSqlParser('pg-17.2'))->parse('ALTER GROUP g DROP USER u');
54 self::assertSame(AddOrDrop::Drop, $lowering->flags->addOrDrop($tree->find('add_drop')[0]));
55 }
56
57 public function testXmlOptionLowersTheChoice(): void
58 {
59 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
60 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SET XML OPTION CONTENT');
61 self::assertSame(XmlOption::Content, $lowering->flags->xmlOption($tree->find('document_or_content')[0]));
62 }
63
64 public function testNormalFormLowersTheForm(): void
65 {
66 $lowering = new Lowering((new Platform())->productions(new LanguageProfile(GrammarRelease::PostgreSql172)), new Leaves(), GrammarRelease::PostgreSql172);
67 $tree = (new PostgreSqlParser('pg-17.2'))->parse('SELECT normalize(a, NFKC)');
68 self::assertSame(NormalForm::Nfkc, $lowering->flags->normalForm($tree->find('unicode_normal_form')[0]));
69 }
70}
71