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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
<?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;
use const PHP_VERSION_ID;
/**
* @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');
if (PHP_VERSION_ID < 80100) {
$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
,
],
// with foreign key
[
<<<'SQL'
CREATE TABLE orders (
order_id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
order_date DATE NOT NULL,
amount DECIMAL(10, 2) NOT NULL,
status VARCHAR(50) DEFAULT 'pending',
CONSTRAINT fk_user
FOREIGN KEY (user_id)
REFERENCES users(user_id)
ON DELETE CASCADE
ON UPDATE CASCADE
)
SQL
,
['order_id','user_id','order_date','status','amount'],
<<<'SQL'
ALTER TABLE `orders`
CHANGE `status` `status` varchar(50) DEFAULT 'pending' AFTER `order_date`
SQL
,
],
// With non-primary index
[
<<<'SQL'
CREATE TABLE `test_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`val` int(11) DEFAULT NULL,
`note` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `val_index` (`val`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
SQL
,
['id', 'note', 'val'],
<<<'SQL'
ALTER TABLE `test_table`
CHANGE `note` `note` varchar(100) DEFAULT NULL AFTER `id`
SQL
,
],
];
}
}
|