packages/sql-semantics-mysql/tests/Unit/Statement/Utility/Show/Schema/ShowCreateTableTest.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\ShowCreateTable;
12
13#[CoversClass(ShowCreateTable::class)]
14#[Medium]
15final class ShowCreateTableTest extends TestCase
16{
17 public function testDeriveStatementLeavesTheShapeOpen(): void
18 {
19 $show = (new Semantics())->inspect('SHOW CREATE TABLE t');
20 self::assertInstanceOf(ShowCreateTable::class, $show->statement);
21 $missing = (new Semantics())->inspect('SHOW CREATE TABLE t', []);
22 self::assertSame('Relation t does not exist.', $missing->facts->diagnostics[0]->message());
23 self::assertFalse($show->shape()?->complete());
24 }
25
26 public function testRenderWritesTheStatement(): void
27 {
28 self::assertSame('SHOW CREATE TABLE t', (new Semantics())->inspect('SHOW CREATE TABLE t')->toString());
29 }
30
31 public function testDeriveStatementAnswersTheColumnsOfTheDeclaredKind(): void
32 {
33 $semantics = new Semantics();
34 $table = $semantics->inspect('CREATE TABLE t (a INT)');
35 $view = $semantics->inspect('CREATE VIEW v AS SELECT a FROM t', [$table]);
36
37 self::assertSame(['Table', 'Create Table'], array_map(static fn ($slot): ?string => $slot->name?->value, $semantics->inspect('SHOW CREATE TABLE t', [$table, $view])->shape()->slots ?? []));
38 self::assertSame(['View', 'Create View', 'character_set_client', 'collation_connection'], array_map(static fn ($slot): ?string => $slot->name?->value, $semantics->inspect('SHOW CREATE TABLE v', [$table, $view])->shape()->slots ?? []));
39 }
40}
41