Merge "(bug 35693) ApiQueryImageInfo now suppresses errors when unserializing metadata"
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiTestCase.php
index a7e3954..1cc45e0 100644 (file)
@@ -5,6 +5,11 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
        public $regex = '';
        public $runDisabled = false;
 
+       /**
+        * @var Array of TestUser
+        */
+       public static $users;
+
        /**
         * @var DatabaseBase
         */
@@ -115,7 +120,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 +129,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();
        }
 
@@ -385,6 +397,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
@@ -414,6 +446,25 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                );
        }
 
+       /**
+        * Put each HTML element on its own line and then equals() the results
+        *
+        * Use for nicely formatting of PHPUnit diff output when comparing very
+        * simple HTML
+        *
+        * @since 1.20
+        *
+        * @param String $expected HTML on oneline
+        * @param String $actual HTML on oneline
+        * @param String $msg Optional message
+        */
+       protected function assertHTMLEquals( $expected, $actual, $msg='' ) {
+               $expected = str_replace( '>', ">\n", $expected );
+               $actual   = str_replace( '>', ">\n", $actual   );
+
+               $this->assertEquals( $expected, $actual, $msg );
+       }
+
        /**
         * Does an associative sort that works for objects.
         *
@@ -451,4 +502,46 @@ abstract class MediaWikiTestCase extends PHPUnit_Framework_TestCase {
                }
        }
 
+       /**
+        * Asserts that the provided variable is of the specified
+        * internal type or equals the $value argument. This is useful
+        * for testing return types of functions that return a certain
+        * type or *value* when not set or on error.
+        *
+        * @since 1.20
+        *
+        * @param string $type
+        * @param mixed $actual
+        * @param mixed $value
+        * @param string $message
+        */
+       protected function assertTypeOrValue( $type, $actual, $value = false, $message = '' ) {
+               if ( $actual === $value ) {
+                       $this->assertTrue( true, $message );
+               }
+               else {
+                       $this->assertType( $type, $actual, $message );
+               }
+       }
+
+       /**
+        * Asserts the type of the provided value. This can be either
+        * in internal type such as boolean or integer, or a class or
+        * interface the value extends or implements.
+        *
+        * @since 1.20
+        *
+        * @param string $type
+        * @param mixed $actual
+        * @param string $message
+        */
+       protected function assertType( $type, $actual, $message = '' ) {
+               if ( is_object( $actual ) ) {
+                       $this->assertInstanceOf( $type, $actual, $message );
+               }
+               else {
+                       $this->assertInternalType( $type, $actual, $message );
+               }
+       }
+
 }