packages/sql-semantics-mysql/tests/Unit/Statement/Alter/Partition/ExchangePartitionTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Statement\Alter\Partition;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10use SqlSemantics\Platform\MySql\Model\Statement\Reference\Table\DeclaredTable;
11use SqlSemantics\Platform\MySql\Semantics;
12use SqlSemantics\Platform\MySql\Statement\Alter\AlterTable;
13use SqlSemantics\Platform\MySql\Statement\Alter\Partition\ExchangePartition;
14
15#[CoversClass(ExchangePartition::class)]
16#[Medium]
17final class ExchangePartitionTest extends TestCase
18{
19    public function testDeriveCommandResolvesTheOtherTable(): void
20    {
21        $semantics = new Semantics();
22        $t = $semantics->analyze('CREATE TABLE t (a INT)');
23        $u = $semantics->analyze('CREATE TABLE u (a INT)');
24        $alter = $semantics->analyze('ALTER TABLE t EXCHANGE PARTITION p WITH TABLE u', [$t, $u]);
25
26        self::assertInstanceOf(AlterTable::class, $alter->statement);
27        self::assertEquals(new DeclaredTable($u->declarations()[0]), $alter->facts->relation($alter->statement->commands[0])->table);
28    }
29
30    public function testRenderWritesTheAction(): void
31    {
32        self::assertSame('ALTER TABLE t EXCHANGE PARTITION p WITH TABLE db.u WITHOUT VALIDATION', (new Semantics())->analyze('ALTER TABLE t EXCHANGE PARTITION p WITH TABLE db.u WITHOUT VALIDATION')->toString());
33    }
34}
35