Refactor MediaWikiIntegrationTestCase::resetDB() for readability
authorThiemo Kreuz <thiemo.kreuz@wikimedia.de>
Fri, 13 Sep 2019 09:47:00 +0000 (11:47 +0200)
committerThiemo Kreuz (WMDE) <thiemo.kreuz@wikimedia.de>
Thu, 19 Sep 2019 15:39:55 +0000 (15:39 +0000)
This was inspired by I1b2a6eb. As far as I can tell this patch does not
change anything in terms of behavior. My main motivation was to get rid
of the (possibly confusing) duplications, and turn them into a loop.

The special case where TestUserRegistry::clear() is executed appears like
it might not do the exact same as before, but I'm pretty sure it does.
Before, the special case was executed whenever any of the user tables was
present in the $tablesUsed array originally passed to the method. The
same still happens.

Additional changes:
* Strict type hints in the method signature.

Change-Id: I7f292095c0dac99b9cc0b0aa5ce10703f24f8bba

tests/phpunit/MediaWikiIntegrationTestCase.php

index ecd5c05..8a188bf 100644 (file)
@@ -1821,27 +1821,30 @@ abstract class MediaWikiIntegrationTestCase extends PHPUnit\Framework\TestCase {
        /**
         * Empty all tables so they can be repopulated for tests
         *
-        * @param Database $db|null Database to reset
-        * @param array $tablesUsed Tables to reset
+        * @param IDatabase $db|null Database to reset
+        * @param string[] $tablesUsed Tables to reset
         */
-       private function resetDB( $db, $tablesUsed ) {
+       private function resetDB( IDatabase $db = null, array $tablesUsed ) {
                if ( $db ) {
-                       $userTables = [ 'user', 'user_groups', 'user_properties', 'actor' ];
-                       $pageTables = [
-                               'page', 'revision', 'ip_changes', 'revision_comment_temp', 'comment', 'archive',
-                               'revision_actor_temp', 'slots', 'content', 'content_models', 'slot_roles',
-                               'change_tag',
-                       ];
-                       $loggingTables = [
-                               'logging', 'log_search', 'change_tag',
+                       // some groups of tables are connected such that if any is used, all should be cleared
+                       $extraTables = [
+                               'user' => [ 'user', 'user_groups', 'user_properties', 'actor' ],
+                               'page' => [ 'page', 'revision', 'ip_changes', 'revision_comment_temp', 'comment', 'archive',
+                                       'revision_actor_temp', 'slots', 'content', 'content_models', 'slot_roles',
+                                       'change_tag' ],
+                               'logging' => [ 'logging', 'log_search', 'change_tag' ],
                        ];
-                       $coreDBDataTables = array_merge( $userTables, $pageTables );
+                       $coreDBDataTables = array_merge( $extraTables['user'], $extraTables['page'] );
 
-                       // some groups of tables are connected such that if any is used, all should be cleared
-                       $extraTables = [];
-                       if ( array_intersect( $tablesUsed, $userTables ) ) {
-                               $extraTables[] = $userTables;
+                       foreach ( $extraTables as $i => $group ) {
+                               if ( !array_intersect( $tablesUsed, $group ) ) {
+                                       unset( $extraTables[$i] );
+                               }
+                       }
+                       $extraTables = array_values( $extraTables );
+                       $tablesUsed = array_unique( array_merge( $tablesUsed, ...$extraTables ) );
 
+                       if ( in_array( 'user', $tablesUsed ) ) {
                                TestUserRegistry::clear();
 
                                // Reset $wgUser, which is probably 127.0.0.1, as its loaded data is probably not valid
@@ -1850,15 +1853,6 @@ abstract class MediaWikiIntegrationTestCase extends PHPUnit\Framework\TestCase {
                                global $wgUser;
                                $wgUser->clearInstanceCache( $wgUser->mFrom );
                        }
-                       if ( array_intersect( $tablesUsed, $pageTables ) ) {
-                               $extraTables[] = $pageTables;
-                       }
-                       if ( array_intersect( $tablesUsed, $loggingTables ) ) {
-                               $extraTables[] = $loggingTables;
-                       }
-                       if ( $extraTables !== [] ) {
-                               $tablesUsed = array_unique( array_merge( $tablesUsed, ...$extraTables ) );
-                       }
 
                        // Postgres uses mwuser/pagecontent
                        // instead of user/text. But Postgres does not remap the