packages/ztd-query-mysql/tests/Unit/Rewrite/Validation/AlterTableGuardTest.php

1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rewrite\Validation;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\TestCase;
9use ZtdQuery\Platform\MySql\Rewrite\Validation\AlterTableGuard;
10
11#[\PHPUnit\Framework\Attributes\UsesClass(\ZtdQuery\Platform\MySql\Sql\Alter\OptionList::class)]
12#[CoversClass(AlterTableGuard::class)]
13final class AlterTableGuardTest extends TestCase
14{
15    public function testHasUnsupportedAlterOperationCase1(): void
16    {
17        $operation = 'ADD COLUMN name TEXT';
18        $expected = false;
19        $statement = (new \PhpMyAdmin\SqlParser\Parser('ALTER TABLE users ' . $operation))->statements[0];
20        self::assertInstanceOf(\PhpMyAdmin\SqlParser\Statements\AlterStatement::class, $statement);
21        self::assertSame($expected, (new AlterTableGuard())->hasUnsupportedAlterOperation($statement, 'ALTER TABLE users ' . $operation));
22    }
23
24    public function testHasUnsupportedAlterOperationCase2(): void
25    {
26        $operation = 'ALTER COLUMN name SET DEFAULT 7';
27        $expected = true;
28        $statement = (new \PhpMyAdmin\SqlParser\Parser('ALTER TABLE users ' . $operation))->statements[0];
29        self::assertInstanceOf(\PhpMyAdmin\SqlParser\Statements\AlterStatement::class, $statement);
30        self::assertSame($expected, (new AlterTableGuard())->hasUnsupportedAlterOperation($statement, 'ALTER TABLE users ' . $operation));
31    }
32
33    public function testHasUnsupportedAlterOperationCase3(): void
34    {
35        $operation = 'ORDER BY id';
36        $expected = true;
37        $statement = (new \PhpMyAdmin\SqlParser\Parser('ALTER TABLE users ' . $operation))->statements[0];
38        self::assertInstanceOf(\PhpMyAdmin\SqlParser\Statements\AlterStatement::class, $statement);
39        self::assertSame($expected, (new AlterTableGuard())->hasUnsupportedAlterOperation($statement, 'ALTER TABLE users ' . $operation));
40    }
41
42    public function testHasUnsupportedAlterOperationCase4(): void
43    {
44        $operation = 'ADD INDEX idx (id)';
45        $expected = true;
46        $statement = (new \PhpMyAdmin\SqlParser\Parser('ALTER TABLE users ' . $operation))->statements[0];
47        self::assertInstanceOf(\PhpMyAdmin\SqlParser\Statements\AlterStatement::class, $statement);
48        self::assertSame($expected, (new AlterTableGuard())->hasUnsupportedAlterOperation($statement, 'ALTER TABLE users ' . $operation));
49    }
50
51    public function testRejectsOperationCase1(): void
52    {
53        $operation = 'ADD COLUMN name TEXT';
54        $expected = false;
55        $statement = (new \PhpMyAdmin\SqlParser\Parser('ALTER TABLE users ' . $operation))->statements[0];
56        self::assertInstanceOf(\PhpMyAdmin\SqlParser\Statements\AlterStatement::class, $statement);
57        self::assertNotNull($statement->altered);
58        self::assertSame($expected, (new AlterTableGuard())->rejectsOperation($statement->altered[0]));
59    }
60
61    public function testRejectsOperationCase2(): void
62    {
63        $operation = 'DROP COLUMN name';
64        $expected = false;
65        $statement = (new \PhpMyAdmin\SqlParser\Parser('ALTER TABLE users ' . $operation))->statements[0];
66        self::assertInstanceOf(\PhpMyAdmin\SqlParser\Statements\AlterStatement::class, $statement);
67        self::assertNotNull($statement->altered);
68        self::assertSame($expected, (new AlterTableGuard())->rejectsOperation($statement->altered[0]));
69    }
70
71    public function testRejectsOperationCase3(): void
72    {
73        $operation = 'ADD INDEX idx (id)';
74        $expected = true;
75        $statement = (new \PhpMyAdmin\SqlParser\Parser('ALTER TABLE users ' . $operation))->statements[0];
76        self::assertInstanceOf(\PhpMyAdmin\SqlParser\Statements\AlterStatement::class, $statement);
77        self::assertNotNull($statement->altered);
78        self::assertSame($expected, (new AlterTableGuard())->rejectsOperation($statement->altered[0]));
79    }
80
81    public function testRejectsOperationCase4(): void
82    {
83        $operation = 'DROP INDEX idx';
84        $expected = true;
85        $statement = (new \PhpMyAdmin\SqlParser\Parser('ALTER TABLE users ' . $operation))->statements[0];
86        self::assertInstanceOf(\PhpMyAdmin\SqlParser\Statements\AlterStatement::class, $statement);
87        self::assertNotNull($statement->altered);
88        self::assertSame($expected, (new AlterTableGuard())->rejectsOperation($statement->altered[0]));
89    }
90
91    public function testRejectsOperationCase5(): void
92    {
93        $operation = 'ENGINE = InnoDB';
94        $expected = true;
95        $statement = (new \PhpMyAdmin\SqlParser\Parser('ALTER TABLE users ' . $operation))->statements[0];
96        self::assertInstanceOf(\PhpMyAdmin\SqlParser\Statements\AlterStatement::class, $statement);
97        self::assertNotNull($statement->altered);
98        self::assertSame($expected, (new AlterTableGuard())->rejectsOperation($statement->altered[0]));
99    }
100
101}
102