Use __DIR__ instead of dirname( __FILE__ )
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiTestCase.php
index c96eba0..49c2a70 100644 (file)
@@ -115,7 +115,7 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        }
 
        protected function tearDown() {
-               // Cleaning up temoporary files
+               // Cleaning up temporary files
                foreach ( $this->tmpfiles as $fname ) {
                        if ( is_file( $fname ) || ( is_link( $fname ) ) ) {
                                unlink( $fname );
@@ -124,6 +124,13 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                        }
                }
 
+               // clean up open transactions
+               if( $this->needsDB() && $this->db ) {
+                       while( $this->db->trxLevel() > 0 ) {
+                               $this->db->rollback();
+                       }
+               }
+
                parent::tearDown();
        }
 
@@ -326,10 +333,6 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
 
        }
 
-       public static function disableInterwikis( $prefix, &$data ) {
-               return false;
-       }
-
        /**
         * Don't throw a warning if $function is deprecated and called later
         *
@@ -348,6 +351,8 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         * the values given in the order of the columns in the $fields parameter.
         * Note that the rows are sorted by the columns given in $fields.
         *
+        * @since 1.20
+        *
         * @param $table String|Array the table(s) to query
         * @param $fields String|Array the columns to include in the result (and to sort by)
         * @param $condition String|Array "where" condition(s)
@@ -387,6 +392,26 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                $this->assertFalse( $r, "found extra row (after #$i)" );
        }
 
+       /**
+        * Utility method taking an array of elements and wrapping
+        * each element in it's own array. Useful for data providers
+        * that only return a single argument.
+        *
+        * @since 1.20
+        *
+        * @param array $elements
+        *
+        * @return array
+        */
+       protected function arrayWrap( array $elements ) {
+               return array_map(
+                       function( $element ) {
+                               return array( $element );
+                       },
+                       $elements
+               );
+       }
+
        /**
         * Assert that two arrays are equal. By default this means that both arrays need to hold
         * the same set of values. Using additional arguments, order and associated key can also
@@ -401,8 +426,8 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
         */
        protected function assertArrayEquals( array $expected, array $actual, $ordered = false, $named = false ) {
                if ( !$ordered ) {
-                       asort( $expected );
-                       asort( $actual );
+                       $this->objectAssociativeSort( $expected );
+                       $this->objectAssociativeSort( $actual );
                }
 
                if ( !$named ) {
@@ -416,19 +441,41 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                );
        }
 
+       /**
+        * Does an associative sort that works for objects.
+        *
+        * @since 1.20
+        *
+        * @param array $array
+        */
+       protected function objectAssociativeSort( array &$array ) {
+               uasort(
+                       $array,
+                       function( $a, $b ) {
+                               return serialize( $a ) > serialize( $b ) ? 1 : -1;
+                       }
+               );
+       }
+
        /**
         * Utility function for eliminating all string keys from an array.
         * Useful to turn a database result row as returned by fetchRow() into
         * a pure indexed array.
         *
-        * @static
+        * @since 1.20
+        *
         * @param $r mixed the array to remove string keys from.
         */
        protected static function stripStringKeys( &$r ) {
-               if ( !is_array( $r ) ) return;
+               if ( !is_array( $r ) ) {
+                       return;
+               }
 
                foreach ( $r as $k => $v ) {
-                       if ( is_string( $k ) ) unset( $r[$k] );
+                       if ( is_string( $k ) ) {
+                               unset( $r[$k] );
+                       }
                }
        }
+
 }