-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit test Table\IndexRenameController class
Adds unit test for preview SQL button when renaming an index and using an old MySQL server. Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
- Loading branch information
1 parent
7652f01
commit a850e44
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
test/classes/Controllers/Table/IndexRenameControllerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpMyAdmin\Tests\Controllers\Table; | ||
|
||
use PhpMyAdmin\Controllers\Table\IndexRenameController; | ||
use PhpMyAdmin\DatabaseInterface; | ||
use PhpMyAdmin\Index; | ||
use PhpMyAdmin\Table\Indexes; | ||
use PhpMyAdmin\Template; | ||
use PhpMyAdmin\Tests\AbstractTestCase; | ||
use PhpMyAdmin\Tests\Stubs\DbiDummy; | ||
use PhpMyAdmin\Tests\Stubs\ResponseRenderer; | ||
use ReflectionProperty; | ||
|
||
/** @covers \PhpMyAdmin\Controllers\Table\IndexRenameController */ | ||
final class IndexRenameControllerTest extends AbstractTestCase | ||
{ | ||
public function testPreviewSqlWithOldStatement(): void | ||
{ | ||
$indexRegistry = new ReflectionProperty(Index::class, 'registry'); | ||
$indexRegistry->setAccessible(true); | ||
$indexRegistry->setValue(null, []); | ||
|
||
$GLOBALS['cfg']['Server'] = $GLOBALS['cfg']['Servers'][1]; | ||
$GLOBALS['cfg']['Server']['DisableIS'] = true; | ||
|
||
$GLOBALS['db'] = 'test_db'; | ||
$GLOBALS['table'] = 'test_table'; | ||
$_POST['db'] = 'test_db'; | ||
$_POST['table'] = 'test_table'; | ||
$_POST['old_index'] = 'old_name'; | ||
$_POST['index'] = ['Key_name' => 'new_name']; | ||
$_POST['do_save_data'] = '1'; | ||
$_POST['preview_sql'] = '1'; | ||
|
||
$dbiDummy = new DbiDummy(); | ||
$dbiDummy->addSelectDb('test_db'); | ||
$dbiDummy->addResult('SHOW TABLES LIKE \'test_table\';', [['test_table']]); | ||
$dbiDummy->addResult( | ||
'SHOW INDEXES FROM `test_db`.`test_table`', | ||
[ | ||
['test_table', '0', 'PRIMARY', 'id', 'BTREE'], | ||
['test_table', '1', 'old_name', 'name', 'BTREE'], | ||
], | ||
['Table', 'Non_unique', 'Key_name', 'Column_name', 'Index_type'] | ||
); | ||
|
||
$dbi = DatabaseInterface::load($dbiDummy); | ||
$dbi->setVersion(['@@version' => '5.5.0']); | ||
$GLOBALS['dbi'] = $dbi; | ||
|
||
$expected = <<<'HTML' | ||
<div class="preview_sql"> | ||
<code class="sql" dir="ltr"><pre> | ||
ALTER TABLE `test_db`.`test_table` DROP INDEX `old_name`, ADD INDEX `new_name` (`name`) USING BTREE; | ||
</pre></code> | ||
</div> | ||
HTML; | ||
|
||
$responseRenderer = new ResponseRenderer(); | ||
$template = new Template(); | ||
$controller = new IndexRenameController( | ||
$responseRenderer, | ||
$template, | ||
'test_db', | ||
'test_table', | ||
$dbi, | ||
new Indexes($responseRenderer, $template, $dbi) | ||
); | ||
$controller(); | ||
|
||
self::assertSame(['sql_data' => $expected], $responseRenderer->getJSONResult()); | ||
|
||
$this->assertAllSelectsConsumed(); | ||
$this->assertAllQueriesConsumed(); | ||
|
||
$indexRegistry->setValue(null, []); | ||
} | ||
} |