1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
<?php
declare(strict_types=1);
namespace PhpMyAdmin\Tests\Controllers\Table\Structure;
use PhpMyAdmin\Controllers\Table\Structure\MoveColumnsController;
use PhpMyAdmin\Template;
use PhpMyAdmin\Tests\AbstractTestCase;
use PhpMyAdmin\Tests\Stubs\ResponseRenderer as ResponseStub;
use ReflectionClass;
use function preg_replace;
/**
* @covers \PhpMyAdmin\Controllers\Table\Structure\MoveColumnsController
*/
#[\PHPUnit\Framework\Attributes\CoversClass(\PhpMyAdmin\Controllers\Table\Structure\MoveColumnsController::class)]
class MoveColumnsControllerTest extends AbstractTestCase
{
/**
* @param array<int,string> $columnNames
* @psalm-param list<string> $columnNames
*
* @dataProvider providerForTestGenerateAlterTableSql
*/
#[\PHPUnit\Framework\Attributes\DataProvider('providerForTestGenerateAlterTableSql')]
public function testGenerateAlterTableSql(string $createStatement, array $columnNames, ?string $expected): void
{
$class = new ReflectionClass(MoveColumnsController::class);
$method = $class->getMethod('generateAlterTableSql');
$method->setAccessible(true);
$controller = new MoveColumnsController(
new ResponseStub(),
new Template(),
'test-db',
'test',
$this->dbi
);
/** @var string|null $alterStatement */
$alterStatement = $method->invoke($controller, $createStatement, $columnNames);
$expected = $expected === null ? null : preg_replace('/\r?\n/', "\n", $expected);
$alterStatement = $alterStatement === null ? null : preg_replace('/\r?\n/', "\n", $alterStatement);
self::assertSame($expected, $alterStatement);
}
/**
* Data provider for testGenerateAlterTableSql
*
* @return array<array<string[]|string|null>>
* @psalm-return list<array{string,list<string>,string}>
*/
public static function providerForTestGenerateAlterTableSql(): array
{
return [
// MariaDB / column CHECK constraint
[
<<<'SQL'
CREATE TABLE `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) DEFAULT NULL,
`data` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL CHECK (json_valid(`json`)),
PRIMARY KEY (`id`)
)
SQL
,
['id', 'data', 'name'],
<<<'SQL'
ALTER TABLE `test`
CHANGE `data` `data` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL CHECK (json_valid(`json`)) AFTER `id`
SQL
,
],
// MariaDB / text column with uuid() default
[
<<<'SQL'
CREATE TABLE `test` (
`Id` int(11) NOT NULL,
`First` text NOT NULL DEFAULT uuid(),
`Second` text NOT NULL DEFAULT uuid()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
SQL
,
['Id', 'Second', 'First'],
<<<'SQL'
ALTER TABLE `test`
CHANGE `Second` `Second` text NOT NULL DEFAULT uuid() AFTER `Id`
SQL
,
],
// MySQL 8.0.13 text column with uuid() default
[
<<<'SQL'
CREATE TABLE `test` (
`Id` int(11) NOT NULL,
`First` text COLLATE utf8mb4_general_ci NOT NULL DEFAULT (uuid()),
`Second` text COLLATE utf8mb4_general_ci NOT NULL DEFAULT (uuid())
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
SQL
,
['Id', 'Second', 'First'],
<<<'SQL'
ALTER TABLE `test`
CHANGE `Second` `Second` text COLLATE utf8mb4_general_ci NOT NULL DEFAULT (uuid()) AFTER `Id`
SQL
,
],
// enum with default
[
<<<'SQL'
CREATE TABLE `test` (
`id` int(11) NOT NULL,
`enum` enum('yes','no') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'no',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci
SQL
,
['enum', 'id'],
<<<'SQL'
ALTER TABLE `test`
CHANGE `enum` `enum` enum('yes','no') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'no' FIRST
SQL
,
],
];
}
}
|