packages/sql-semantics-mysql/tests/Unit/Statement/Utility/Show/Schema/ShowCreateViewTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Utility\Show\Schema;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Semantics;
11use SqlSemantics\Platform\MySql\Statement\Utility\Show\Schema\ShowCreateView;
12
13#[CoversClass(ShowCreateView::class)]
14#[Medium]
15final class ShowCreateViewTest extends TestCase
16{
17    public function testDeriveStatementRecordsTheRows(): void
18    {
19        $show = (new Semantics('mysql-5.7.44'))->inspect('SHOW CREATE VIEW db.v');
20        self::assertInstanceOf(ShowCreateView::class, $show->statement);
21        self::assertSame('Create View', $show->field(1)->name?->value);
22    }
23
24    public function testRenderWritesTheStatement(): void
25    {
26        self::assertSame('SHOW CREATE VIEW db.v', (new Semantics('mysql-5.7.44'))->inspect('SHOW CREATE VIEW db.v')->toString());
27    }
28
29    public function testDeriveStatementRefusesABaseTable(): void
30    {
31        $semantics = new Semantics();
32        $table = $semantics->inspect('CREATE TABLE t (a INT)');
33        $view = $semantics->inspect('CREATE VIEW v AS SELECT a FROM t', [$table]);
34
35        self::assertSame(['t is not VIEW.'], array_map(static fn ($diagnostic): string => $diagnostic->message(), $semantics->inspect('SHOW CREATE VIEW t', [$table, $view])->facts->diagnostics));
36        self::assertSame([], array_map(static fn ($diagnostic): string => $diagnostic->message(), $semantics->inspect('SHOW CREATE VIEW v', [$table, $view])->facts->diagnostics));
37    }
38}
39