packages/sql-semantics-mysql/tests/Unit/Lowering/TableDefinition/View/ViewRuleTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Lowering\TableDefinition\View;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Lowering\Lowering;
11use SqlSemantics\Platform\MySql\Lowering\TableDefinition\View\ViewRule;
12use SqlSemantics\Platform\MySql\Model\Contract\ParameterStyle;
13use SqlSemantics\Platform\MySql\Model\Lowering\Leaves;
14use SqlSemantics\Platform\MySql\Platform;
15use SqlSemantics\Platform\MySql\Statement\View\ViewAlgorithm;
16use SqlSemantics\Platform\MySql\Statement\View\ViewCheckOption;
17
18#[CoversClass(ViewRule::class)]
19#[Medium]
20final class ViewRuleTest extends TestCase
21{
22    public function testCreateLowersAViewTail(): void
23    {
24        $platform = new Platform();
25        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
26        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
27        $node = $platform->parser($profile)->parse('CREATE OR REPLACE VIEW v AS SELECT 1 WITH LOCAL CHECK OPTION')->find('view_tail')[0];
28        $prefix = $platform->parser($profile)->parse('CREATE OR REPLACE VIEW v AS SELECT 1')->find('view_replace_or_algorithm')[0];
29        $view = (new ViewRule($lowering))->create($node, $prefix, null);
30
31        self::assertTrue($view->orReplace);
32        self::assertSame(ViewCheckOption::Local, $view->definition->check);
33    }
34
35    public function testPrefixLowersReplaceAndAlgorithm(): void
36    {
37        $platform = new Platform();
38        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
39        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
40        $node = $platform->parser($profile)->parse('CREATE OR REPLACE ALGORITHM = MERGE VIEW v AS SELECT 1')->find('view_replace_or_algorithm')[0];
41
42        self::assertSame([true, ViewAlgorithm::Merge], (new ViewRule($lowering))->prefix($node));
43    }
44
45    public function testReplaceConfirmsOrReplace(): void
46    {
47        $platform = new Platform();
48        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
49        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
50        $node = $platform->parser($profile)->parse('CREATE OR REPLACE VIEW v AS SELECT 1')->find('view_replace')[0];
51
52        self::assertTrue((new ViewRule($lowering))->replace($node));
53    }
54
55    public function testAlgorithmLowersTheAlgorithm(): void
56    {
57        $platform = new Platform();
58        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
59        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
60        $node = $platform->parser($profile)->parse('CREATE ALGORITHM = UNDEFINED VIEW v AS SELECT 1')->find('view_algorithm')[0];
61
62        self::assertSame(ViewAlgorithm::Undefined, (new ViewRule($lowering))->algorithm($node));
63    }
64
65    public function testDefinitionLowersTheTail(): void
66    {
67        $platform = new Platform();
68        $profile = $platform->profile('5.7.44', null, ParameterStyle::Native);
69        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
70        $node = $platform->parser($profile)->parse('CREATE VIEW v (a) AS SELECT 1')->find('view_tail')[0];
71
72        self::assertSame('a', (new ViewRule($lowering))->definition($node, null, null)->columns[0]->value ?? null);
73    }
74
75    public function testColumnsLowersALegacyColumnList(): void
76    {
77        $platform = new Platform();
78        $profile = $platform->profile('5.6.51', null, ParameterStyle::Native);
79        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
80        $node = $platform->parser($profile)->parse('CREATE VIEW v (a, b) AS SELECT 1, 2')->find('view_list_opt')[0];
81
82        self::assertCount(2, (new ViewRule($lowering))->columns($node) ?? []);
83    }
84
85    public function testDerivedLowersAnAbsentColumnList(): void
86    {
87        $platform = new Platform();
88        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
89        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
90        $node = $platform->parser($profile)->parse('CREATE VIEW v AS SELECT 1')->find('opt_derived_column_list')[0];
91
92        self::assertNull((new ViewRule($lowering))->derived($node));
93    }
94
95    public function testAlterLowersAlterView(): void
96    {
97        $platform = new Platform();
98        $profile = $platform->profile('8.4.7', null, ParameterStyle::Native);
99        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
100        $node = $platform->parser($profile)->parse('ALTER VIEW v AS SELECT 1')->find('alter_view_stmt')[0];
101
102        self::assertSame('v', (new ViewRule($lowering))->alter($lowering->form($node))->definition->name->name->value);
103    }
104
105    public function testDropLowersDropView(): void
106    {
107        $platform = new Platform();
108        $profile = $platform->profile('5.7.44', null, ParameterStyle::Native);
109        $lowering = new Lowering($platform->productions($profile), new Leaves(), $profile);
110        $node = $platform->parser($profile)->parse('DROP VIEW IF EXISTS v')->find('drop')[0];
111
112        self::assertTrue((new ViewRule($lowering))->drop($lowering->form($node))->ifExists);
113    }
114}
115