66
77use PhpMyAdmin \Controllers \Table \Structure \MoveColumnsController ;
88use PhpMyAdmin \Current ;
9+ use PhpMyAdmin \Message ;
910use PhpMyAdmin \Template ;
1011use PhpMyAdmin \Tests \AbstractTestCase ;
1112use PhpMyAdmin \Tests \Stubs \ResponseRenderer as ResponseStub ;
@@ -23,8 +24,11 @@ class MoveColumnsControllerTest extends AbstractTestCase
2324 * @psalm-param list<string> $columnNames
2425 */
2526 #[DataProvider('providerForTestGenerateAlterTableSql ' )]
26- public function testGenerateAlterTableSql (string $ createStatement , array $ columnNames , string |null $ expected ): void
27- {
27+ public function testGenerateAlterTableSql (
28+ string $ createStatement ,
29+ array $ columnNames ,
30+ string |Message $ expected ,
31+ ): void {
2832 $ class = new ReflectionClass (MoveColumnsController::class);
2933 $ method = $ class ->getMethod ('generateAlterTableSql ' );
3034
@@ -38,19 +42,26 @@ public function testGenerateAlterTableSql(string $createStatement, array $column
3842 new Template (),
3943 $ dbi ,
4044 );
41- /** @var string|null $alterStatement */
45+ /** @var string|Message $alterStatement */
4246 $ alterStatement = $ method ->invoke ($ controller , $ createStatement , $ columnNames );
4347
44- $ expected = $ expected === null ? null : preg_replace ('/\r?\n/ ' , "\n" , $ expected );
45- $ alterStatement = $ alterStatement === null ? null : preg_replace ('/\r?\n/ ' , "\n" , $ alterStatement );
48+ if ($ expected instanceof Message) {
49+ self ::assertInstanceOf (Message::class, $ alterStatement );
50+ self ::assertSame ($ expected ->getMessage (), $ alterStatement ->getMessage ());
51+
52+ return ;
53+ }
54+
55+ self ::assertIsString ($ alterStatement );
56+ $ expected = preg_replace ('/\r?\n/ ' , "\n" , $ expected );
57+ $ alterStatement = preg_replace ('/\r?\n/ ' , "\n" , $ alterStatement );
4658 self ::assertSame ($ expected , $ alterStatement );
4759 }
4860
4961 /**
5062 * Data provider for testGenerateAlterTableSql
5163 *
52- * @return array<array<string[]|string|null>>
53- * @psalm-return list<array{string,list<string>,string}>
64+ * @return list<array{string,list<string>,string|Message}>
5465 */
5566 public static function providerForTestGenerateAlterTableSql (): array
5667 {
@@ -117,6 +128,56 @@ public static function providerForTestGenerateAlterTableSql(): array
117128 CHANGE `enum` `enum` enum('yes','no') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'no' FIRST
118129 SQL,
119130 ],
131+ [
132+ <<<'SQL'
133+ CREATE TABLE `test` (
134+ `id` int(11) NOT NULL,
135+ `enum` enum('yes','no') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'no',
136+ PRIMARY KEY (`id`)
137+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci
138+ SQL,
139+ ['foo ' , 'id ' ],
140+ Message::error ('The selected columns do not match the columns in the table. ' ),
141+ ],
142+ [
143+ <<<'SQL'
144+ CREATE TABLE `test` (
145+ `id` int(11) NOT NULL,
146+ `enum` enum('yes','no') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'no',
147+ PRIMARY KEY (`id`)
148+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci
149+ SQL,
150+ ['id ' , 'enum ' ],
151+ Message::error ('The selected columns are already in the correct order. ' ),
152+ ],
153+ [
154+ <<<'SQL'
155+ CREATE TABLE `test` (
156+ `"` int(11) NOT NULL,
157+ `'` enum('yes','no') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'no',
158+ PRIMARY KEY (`id`)
159+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci
160+ SQL,
161+ ['\'' , '" ' ],
162+ <<<'SQL'
163+ ALTER TABLE `test`
164+ CHANGE `'` `'` enum('yes','no') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'no' FIRST
165+ SQL,
166+ ],
167+ [
168+ <<<'SQL'
169+ CREATE TABLE `test` (
170+ `1` int(11) NOT NULL,
171+ `0` enum('yes','no') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'no',
172+ PRIMARY KEY (`id`)
173+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci
174+ SQL,
175+ ['0 ' , '1 ' ],
176+ <<<'SQL'
177+ ALTER TABLE `test`
178+ CHANGE `0` `0` enum('yes','no') CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'no' FIRST
179+ SQL,
180+ ],
120181 ];
121182 // phpcs:enable
122183 }
0 commit comments