tests: Throw when tests run that "need a db" but miss Database group
authoraddshore <addshorewiki@gmail.com>
Tue, 11 Sep 2018 17:16:26 +0000 (18:16 +0100)
committerJforrester <jforrester@wikimedia.org>
Tue, 11 Sep 2018 19:04:05 +0000 (19:04 +0000)
Depends-On: I5cfea2dc4e56ed4639126e35c0f8aa370852f056
Depends-On: Ic472eb073290793c432812ae8b47ab2b41e74a26
Change-Id: I4cdc8130032340726c5d18d795cd2d6b6b58b307

tests/phpunit/MediaWikiTestCase.php

index 0778ab9..de01b47 100644 (file)
@@ -387,6 +387,12 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
                }
                $this->overrideMwServices();
 
+               if ( $this->needsDB() && !$this->isTestInDatabaseGroup() ) {
+                       throw new Exception(
+                               get_class( $this ) . ' apparently needsDB but is not in the Database group'
+                       );
+               }
+
                $needsResetDB = false;
                if ( !self::$dbSetup || $this->needsDB() ) {
                        // set up a DB connection for this test to use
@@ -1062,18 +1068,18 @@ abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
         */
        public function needsDB() {
                // If the test says it uses database tables, it needs the database
-               if ( $this->tablesUsed ) {
-                       return true;
-               }
+               return $this->tablesUsed || $this->isTestInDatabaseGroup();
+       }
 
+       /**
+        * @return bool
+        * @since 1.32
+        */
+       protected function isTestInDatabaseGroup() {
                // If the test class says it belongs to the Database group, it needs the database.
                // NOTE: This ONLY checks for the group in the class level doc comment.
                $rc = new ReflectionClass( $this );
-               if ( preg_match( '/@group +Database/im', $rc->getDocComment() ) ) {
-                       return true;
-               }
-
-               return false;
+               return (bool)preg_match( '/@group +Database/im', $rc->getDocComment() );
        }
 
        /**