Lots of spelling mistakes and phpdoc attributes
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiTestCase.php
index 19736ee..51fbff1 100644 (file)
@@ -81,17 +81,17 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                $needsResetDB = false;
                $logName = get_class( $this ) . '::' . $this->getName( false );
 
-               if( $this->needsDB() ) {
+               if ( $this->needsDB() ) {
                        // set up a DB connection for this test to use
 
                        self::$useTemporaryTables = !$this->getCliArg( 'use-normal-tables' );
-                       self::$reuseDB = $this->getCliArg('reuse-db');
+                       self::$reuseDB = $this->getCliArg( 'reuse-db' );
 
                        $this->db = wfGetDB( DB_MASTER );
 
                        $this->checkDbIsSupported();
 
-                       if( !self::$dbSetup ) {
+                       if ( !self::$dbSetup ) {
                                wfProfileIn( $logName . ' (clone-db)' );
 
                                // switch to a temporary clone of the database
@@ -116,7 +116,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                parent::run( $result );
                wfProfileOut( $logName );
 
-               if( $needsResetDB ) {
+               if ( $needsResetDB ) {
                        wfProfileIn( $logName . ' (reset-db)' );
                        $this->resetDB();
                        wfProfileOut( $logName . ' (reset-db)' );
@@ -128,7 +128,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         *
         * The obtained filename is enlisted to be removed upon tearDown
         *
-        * @returns string: absolute name of the temporary file
+        * @return string: absolute name of the temporary file
         */
        protected function getNewTempFile() {
                $fname = tempnam( wfTempDir(), 'MW_PHPUnit_' . get_class( $this ) . '_' );
@@ -142,7 +142,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         * The obtained directory is enlisted to be removed (recursively with all its contained
         * files) upon tearDown.
         *
-        * @returns string: absolute name of the temporary directory
+        * @return string: absolute name of the temporary directory
         */
        protected function getNewTempDirectory() {
                // Starting of with a temporary /file/.
@@ -188,7 +188,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
                if ( $this->needsDB() && $this->db ) {
                        // Clean up open transactions
-                       while( $this->db->trxLevel() > 0 ) {
+                       while ( $this->db->trxLevel() > 0 ) {
                                $this->db->rollback();
                        }
 
@@ -213,7 +213,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
                if ( $this->needsDB() && $this->db ) {
                        // Clean up open transactions
-                       while( $this->db->trxLevel() > 0 ) {
+                       while ( $this->db->trxLevel() > 0 ) {
                                $this->db->rollback();
                        }
 
@@ -276,7 +276,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        protected function setMwGlobals( $pairs, $value = null ) {
 
                // Normalize (string, value) to an array
-               if( is_string( $pairs ) ) {
+               if ( is_string( $pairs ) ) {
                        $pairs = array( $pairs => $value );
                }
 
@@ -356,8 +356,8 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                        # Insert 0 user to prevent FK violations
                        # Anonymous user
                        $this->db->insert( 'user', array(
-                               'user_id'               => 0,
-                               'user_name'     => 'Anonymous' ), __METHOD__, array( 'IGNORE' ) );
+                               'user_id' => 0,
+                               'user_name' => 'Anonymous' ), __METHOD__, array( 'IGNORE' ) );
 
                        # Insert 0 page to prevent FK violations
                        # Blank page
@@ -471,20 +471,24 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         * Empty all tables so they can be repopulated for tests
         */
        private function resetDB() {
-               if( $this->db ) {
-                       if ( $this->db->getType() == 'oracle' )  {
+               if ( $this->db ) {
+                       if ( $this->db->getType() == 'oracle' ) {
                                if ( self::$useTemporaryTables ) {
                                        wfGetLB()->closeAll();
                                        $this->db = wfGetDB( DB_MASTER );
                                } else {
-                                       foreach( $this->tablesUsed as $tbl ) {
-                                               if( $tbl == 'interwiki') continue;
-                                               $this->db->query( 'TRUNCATE TABLE '.$this->db->tableName($tbl), __METHOD__ );
+                                       foreach ( $this->tablesUsed as $tbl ) {
+                                               if ( $tbl == 'interwiki' ) {
+                                                       continue;
+                                               }
+                                               $this->db->query( 'TRUNCATE TABLE ' . $this->db->tableName( $tbl ), __METHOD__ );
                                        }
                                }
                        } else {
-                               foreach( $this->tablesUsed as $tbl ) {
-                                       if( $tbl == 'interwiki' || $tbl == 'user' ) continue;
+                               foreach ( $this->tablesUsed as $tbl ) {
+                                       if ( $tbl == 'interwiki' || $tbl == 'user' ) {
+                                               continue;
+                                       }
                                        $this->db->delete( $tbl, '*', __METHOD__ );
                                }
                        }
@@ -500,9 +504,9 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                );
 
                if ( method_exists( $this->suite, $func ) ) {
-                       return call_user_func_array( array( $this->suite, $func ), $args);
+                       return call_user_func_array( array( $this->suite, $func ), $args );
                } elseif ( isset( $compatibility[$func] ) ) {
-                       return call_user_func_array( array( $this, $compatibility[$func] ), $args);
+                       return call_user_func_array( array( $this, $compatibility[$func] ), $args );
                } else {
                        throw new MWException( "Called non-existant $func method on "
                                . get_class( $this ) );
@@ -543,14 +547,14 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        }
 
        protected function checkDbIsSupported() {
-               if( !in_array( $this->db->getType(), $this->supportedDBs ) ) {
+               if ( !in_array( $this->db->getType(), $this->supportedDBs ) ) {
                        throw new MWException( $this->db->getType() . " is not currently supported for unit testing." );
                }
        }
 
        public function getCliArg( $offset ) {
 
-               if( isset( MediaWikiPHPUnitCommand::$additionalOptions[$offset] ) ) {
+               if ( isset( MediaWikiPHPUnitCommand::$additionalOptions[$offset] ) ) {
                        return MediaWikiPHPUnitCommand::$additionalOptions[$offset];
                }
 
@@ -595,7 +599,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        protected function assertSelect( $table, $fields, $condition, array $expectedRows ) {
                if ( !$this->needsDB() ) {
                        throw new MWException( 'When testing database state, the test cases\'s needDB()' .
-                               ' method should return true. Use @group Database or $this->tablesUsed.');
+                               ' method should return true. Use @group Database or $this->tablesUsed.' );
                }
 
                $db = wfGetDB( DB_SLAVE );
@@ -634,7 +638,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         */
        protected function arrayWrap( array $elements ) {
                return array_map(
-                       function( $element ) {
+                       function ( $element ) {
                                return array( $element );
                        },
                        $elements
@@ -682,9 +686,9 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         * @param String $actual HTML on oneline
         * @param String $msg Optional message
         */
-       protected function assertHTMLEquals( $expected, $actual, $msg='' ) {
+       protected function assertHTMLEquals( $expected, $actual, $msg = '' ) {
                $expected = str_replace( '>', ">\n", $expected );
-               $actual   = str_replace( '>', ">\n", $actual   );
+               $actual = str_replace( '>', ">\n", $actual );
 
                $this->assertEquals( $expected, $actual, $msg );
        }
@@ -699,7 +703,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        protected function objectAssociativeSort( array &$array ) {
                uasort(
                        $array,
-                       function( $a, $b ) {
+                       function ( $a, $b ) {
                                return serialize( $a ) > serialize( $b ) ? 1 : -1;
                        }
                );
@@ -742,8 +746,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        protected function assertTypeOrValue( $type, $actual, $value = false, $message = '' ) {
                if ( $actual === $value ) {
                        $this->assertTrue( true, $message );
-               }
-               else {
+               } else {
                        $this->assertType( $type, $actual, $message );
                }
        }
@@ -762,8 +765,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        protected function assertType( $type, $actual, $message = '' ) {
                if ( class_exists( $type ) || interface_exists( $type ) ) {
                        $this->assertInstanceOf( $type, $actual, $message );
-               }
-               else {
+               } else {
                        $this->assertInternalType( $type, $actual, $message );
                }
        }
@@ -816,7 +818,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
                $namespaces = array_diff( $namespaces, array(
                        NS_FILE, NS_CATEGORY, NS_MEDIAWIKI, NS_USER // don't mess with magic namespaces
-               ));
+               ) );
 
                $talk = array_filter( $namespaces, function ( $ns ) {
                        return MWNamespace::isTalk( $ns );
@@ -829,7 +831,8 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                // check default content model of each namespace
                foreach ( $namespaces as $ns ) {
                        if ( !isset( $wgNamespaceContentModels[$ns] ) ||
-                               $wgNamespaceContentModels[$ns] === CONTENT_MODEL_WIKITEXT ) {
+                               $wgNamespaceContentModels[$ns] === CONTENT_MODEL_WIKITEXT
+                       ) {
 
                                $wikitextNS = $ns;
                                return $wikitextNS;
@@ -857,11 +860,51 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                $haveDiff3 = $wgDiff3 && file_exists( $wgDiff3 );
                wfRestoreWarnings();
 
-               if( !$haveDiff3 ) {
+               if ( !$haveDiff3 ) {
                        $this->markTestSkipped( "Skip test, since diff3 is not configured" );
                }
        }
 
+       /**
+        * Check whether we have the 'gzip' commandline utility, will skip
+        * the test whenever "gzip -V" fails.
+        *
+        * Result is cached at the process level.
+        *
+        * @return bool
+        *
+        * @since 1.21
+        */
+       protected function checkHasGzip() {
+               static $haveGzip;
+
+               if ( $haveGzip === null ) {
+                       $retval = null;
+                       wfShellExec( 'gzip -V', $retval );
+                       $haveGzip = ( $retval === 0 );
+               }
+
+               if ( !$haveGzip ) {
+                       $this->markTestSkipped( "Skip test, requires the gzip utility in PATH" );
+               }
+
+               return $haveGzip;
+       }
+
+       /**
+        * Check if $extName is a loaded PHP extension, will skip the
+        * test whenever it is not loaded.
+        *
+        * @since 1.21
+        */
+       protected function checkPHPExtension( $extName ) {
+               $loaded = extension_loaded( $extName );
+               if ( !$loaded ) {
+                       $this->markTestSkipped( "PHP extension '$extName' is not loaded, skipping." );
+               }
+               return $loaded;
+       }
+
        /**
         * Asserts that an exception of the specified type occurs when running
         * the provided code.
@@ -877,8 +920,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
                try {
                        call_user_func( $code );
-               }
-               catch ( Exception $pokemons ) {
+               } catch ( Exception $pokemons ) {
                        // Gotta Catch 'Em All!
                }