From 649c84d25154535c68d67f7a8afe5a4ad5c4ded2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Mon, 2 Mar 2026 20:31:37 -0300 Subject: [PATCH] Add unit test for Database\Structure\AddPrefixTableController MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MaurĂ­cio Meneghini Fauth --- psalm-baseline.xml | 5 - .../Structure/AddPrefixTableController.php | 8 +- .../AddPrefixTableControllerTest.php | 88 ++++ .../AddPrefixTable-testAddPrefix.html | 388 ++++++++++++++++++ 4 files changed, 479 insertions(+), 10 deletions(-) create mode 100644 tests/unit/Controllers/Database/Structure/AddPrefixTableControllerTest.php create mode 100644 tests/unit/Controllers/Database/Structure/Fixtures/AddPrefixTable-testAddPrefix.html diff --git a/psalm-baseline.xml b/psalm-baseline.xml index ef14b9ccff..4cf3027880 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -926,11 +926,6 @@ - - - - - diff --git a/src/Controllers/Database/Structure/AddPrefixTableController.php b/src/Controllers/Database/Structure/AddPrefixTableController.php index 7e6b3dae78..af3beeb661 100644 --- a/src/Controllers/Database/Structure/AddPrefixTableController.php +++ b/src/Controllers/Database/Structure/AddPrefixTableController.php @@ -15,12 +15,10 @@ use PhpMyAdmin\Routing\Route; use PhpMyAdmin\Util; #[Route('/database/structure/add-prefix-table', ['POST'])] -final class AddPrefixTableController implements InvocableController +final readonly class AddPrefixTableController implements InvocableController { - public function __construct( - private readonly DatabaseInterface $dbi, - private readonly StructureController $structureController, - ) { + public function __construct(private DatabaseInterface $dbi, private StructureController $structureController) + { } public function __invoke(ServerRequest $request): Response diff --git a/tests/unit/Controllers/Database/Structure/AddPrefixTableControllerTest.php b/tests/unit/Controllers/Database/Structure/AddPrefixTableControllerTest.php new file mode 100644 index 0000000000..59d35f4025 --- /dev/null +++ b/tests/unit/Controllers/Database/Structure/AddPrefixTableControllerTest.php @@ -0,0 +1,88 @@ +setValue(null, []); + $_SESSION['tmpval'] = []; + $_SERVER['SCRIPT_NAME'] = 'index.php'; + $_REQUEST['db'] = Current::$database = 'test_db'; + $dbiDummy = $this->createDbiDummy(); + $dbiDummy->addSelectDb('test_db'); + $dbiDummy->addResult('ALTER TABLE `table` RENAME `test_table`', true); + $dbiDummy->addSelectDb('test_db'); + $dbiDummy->addResult('SHOW TABLES FROM `test_db`;', [['test_table']], ['Tables_in_test_db']); + $dbiDummy->addResult('SELECT COUNT(*) FROM `test_db`.`test_table`', [['3']]); + $dbiDummy->addResult( + "SELECT DEFAULT_COLLATION_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = 'test_db' LIMIT 1", + [['utf8mb4_uca1400_ai_ci']], + ['DEFAULT_COLLATION_NAME'], + ); + $dbiDummy->addResult('SELECT @@storage_engine;', [['InnoDB']]); + + $request = ServerRequestFactory::create()->createServerRequest('POST', 'https://example.com/') + ->withParsedBody(['db' => 'test_db', 'add_prefix' => 'test_', 'selected' => ['table']]); + + $response = ($this->getAddPrefixTableController($dbiDummy))($request); + + $dbiDummy->assertAllSelectsConsumed(); + $dbiDummy->assertAllQueriesConsumed(); + self::assertSame("ALTER TABLE `table` RENAME `test_table`;\n", Current::$sqlQuery); + self::assertEquals(Message::success(), Current::$message); + self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode()); + self::assertStringEqualsFile( + __DIR__ . '/Fixtures/AddPrefixTable-testAddPrefix.html', + (string) $response->getBody(), + ); + } + + private function getAddPrefixTableController(DbiDummy $dbiDummy): AddPrefixTableController + { + $config = new Config(); + $dbi = $this->createDatabaseInterface($dbiDummy, $config); + $relation = new Relation($dbi, $config); + $responseRenderer = new ResponseRenderer(); + $template = new Template($config); + $structureController = new StructureController( + $responseRenderer, + $template, + $relation, + new Replication($dbi), + $dbi, + new TrackingChecker($dbi, $relation), + new PageSettings(new UserPreferences($dbi, $relation, $template, $config, new Clock()), $responseRenderer), + new DbTableExists($dbi), + $config, + ); + + return new AddPrefixTableController($dbi, $structureController); + } +} diff --git a/tests/unit/Controllers/Database/Structure/Fixtures/AddPrefixTable-testAddPrefix.html b/tests/unit/Controllers/Database/Structure/Fixtures/AddPrefixTable-testAddPrefix.html new file mode 100644 index 0000000000..cdf277d3a3 --- /dev/null +++ b/tests/unit/Controllers/Database/Structure/Fixtures/AddPrefixTable-testAddPrefix.html @@ -0,0 +1,388 @@ +
+ + + + + + +
+
+
+
+
Database structure
+
Choose which details to show in the database structure (list of tables).
+ +
+ Database structure + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + Documentation + + + + Show or hide a column displaying the charset for all tables. + + + + + + + + Restore default value + +
+ + + + Documentation + + + + Show or hide a column displaying the comments for all tables. + + + + + + + + Restore default value + +
+ + + + Documentation + + + + Show or hide a column displaying the Creation timestamp for all tables. + + + + + + + + Restore default value + +
+ + + + Documentation + + + + Show or hide a column displaying the Last update timestamp for all tables. + + + + + + + + Restore default value + +
+ + + + Documentation + + + + Show or hide a column displaying the Last check timestamp for all tables. + + + + + + + + Restore default value + +
+
+
+ +
+
+
+
+ + +
+ +
+ + +
+
Filters
+
+ +
+ +
+
+
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Table Ascending DescendingReplication + Action + + Rows + May be approximate. Click on the number to get the exact count. See FAQ 3.11. + TypeCollationSizeOverhead
+ + + test_table + + + + + + +   + + + + Browse Browse + + + + Structure Structure + + + + Search Search + + + Insert Insert + + + Empty Empty + + + + Drop Drop + + + 3 + + + InnoDB + + utf8mb4_general_ci + + + + 16.0 KiB + + + - +
+ 1 table + ReplicationSum3 + + InnoDB + + + 16.0 KiB0 B
+
+
+ With selected: + + + +
+ +
+ + + + +
+
+

+ + + Data dictionary Data dictionary + +

+ +
+ +
Create new table Create new table
+
+
+ + +
+
+ + +
+
+ +
+
+
+