Merge "Further expand Status unit tests"
authorMwalker <mwalker@wikimedia.org>
Mon, 25 Nov 2013 18:13:08 +0000 (18:13 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Mon, 25 Nov 2013 18:13:08 +0000 (18:13 +0000)
1  2 
tests/phpunit/includes/StatusTest.php

@@@ -5,7 -5,7 +5,7 @@@
   */
  class StatusTest extends MediaWikiTestCase {
  
 -      public function testCanConstruct(){
 +      public function testCanConstruct() {
                new Status();
                $this->assertTrue( true );
        }
         * @dataProvider provideValues
         * @covers Status::newGood
         */
 -      public function testNewGood( $value = null ){
 +      public function testNewGood( $value = null ) {
                $status = Status::newGood( $value );
                $this->assertTrue( $status->isGood() );
                $this->assertTrue( $status->isOK() );
                $this->assertEquals( $value, $status->getValue() );
        }
  
 -      public static function provideValues(){
 +      public static function provideValues() {
                return array(
                        array(),
                        array( 'foo' ),
                $status = new Status();
                $messages = $this->getMockMessages( $mockDetails );
  
 -              foreach( $messages as $message ){
 +              foreach ( $messages as $message ) {
                        $status->warning( $message );
                }
                $warnings = $status->getWarningsArray();
  
                $this->assertEquals( count( $messages ), count( $warnings ) );
 -              foreach( $messages as $key => $message ) {
 +              foreach ( $messages as $key => $message ) {
                        $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
                        $this->assertEquals( $warnings[$key], $expectedArray );
                }
                $status = new Status();
                $messages = $this->getMockMessages( $mockDetails );
  
 -              foreach( $messages as $message ){
 +              foreach ( $messages as $message ) {
                        $status->error( $message );
                }
                $errors = $status->getErrorsArray();
  
                $this->assertEquals( count( $messages ), count( $errors ) );
 -              foreach( $messages as $key => $message ) {
 +              foreach ( $messages as $key => $message ) {
                        $expectedArray = array_merge( array( $message->getKey() ), $message->getParams() );
                        $this->assertEquals( $errors[$key], $expectedArray );
                }
         * @param array $messageDetails eg. array( 'KEY' => array(/PARAMS/) )
         * @return Message[]
         */
 -      protected function getMockMessages( $messageDetails ){
 +      protected function getMockMessages( $messageDetails ) {
                $messages = array();
 -              foreach( $messageDetails as $key => $paramsArray ){
 +              foreach ( $messageDetails as $key => $paramsArray ) {
                        $messages[] = $this->getMockMessage( $key, $paramsArray );
                }
                return $messages;
        }
  
 -      public static function provideMockMessageDetails(){
 +      public static function provideMockMessageDetails() {
                return array(
                        array( array( 'key1' => array( 'foo' => 'bar' ) ) ),
                        array( array( 'key1' => array( 'foo' => 'bar' ), 'key2' => array( 'foo2' => 'bar2' ) ) ),
         * @covers Status::merge
         * @todo test merge with $overwriteValue true
         */
 -      public function testMerge(){
 +      public function testMerge() {
                $status1 = new Status();
                $status2 = new Status();
                $message1 = $this->getMockMessage( 'warn1' );
         */
        public function testCleanParams( $cleanCallback, $params, $expected ) {
                $method = new ReflectionMethod( 'Status', 'cleanParams' );
 -              $method->setAccessible(TRUE);
 +              $method->setAccessible( true );
                $status = new Status();
                $status->cleanCallback = $cleanCallback;
  
        }
  
        /**
-        * @dataProvider provideGetWikiText
+        * @dataProvider provideGetWikiTextAndHtml
         * @covers Status::getWikiText
         * @todo test long and short context messages generated through this method
         *       this can not really be done now due to use of wfMessage()->plain()
                $this->assertEquals( $expected, $status->getWikiText() );
        }
  
+       /**
+        * @dataProvider provideGetWikiTextAndHtml
+        * @covers Status::getHtml
+        * @todo test long and short context messages generated through this method
+        *       this can not really be done now due to use of $this->getWikiText using wfMessage()->plain()
+        *       It is possible to mock such methods but only if namespaces are used
+        */
+       public function testGetHtml( Status $status, $expected ) {
+               $this->assertEquals( $expected, $status->getHTML() );
+       }
        /**
         * @return array of arrays with values;
         *    0 => status object
         *    1 => expected string (with no context)
         */
-       public static function provideGetWikiText() {
+       public static function provideGetWikiTextAndHtml() {
                $testCases = array();
  
                $testCases[ 'GoodStatus' ] = array(
                return $testCases;
        }
  
-       //todo test getMessage
+       /**
+        * @dataProvider provideGetMessage
+        * @covers Status::getMessage
+        * @todo test long and short context messages generated through this method
+        */
+       public function testGetMessage( Status $status, $expectedParams = array(), $expectedKey ) {
+               $message = $status->getMessage();
+               $this->assertInstanceOf( 'Message', $message );
+               $this->assertEquals( $expectedParams, $message->getParams() );
+               $this->assertEquals( $expectedKey, $message->getKey() );
+       }
+       /**
+        * @return array of arrays with values;
+        *    0 => status object
+        *    1 => expected Message Params (with no context)
+        */
+       public static function provideGetMessage() {
+               $testCases = array();
+               $testCases[ 'GoodStatus' ] = array(
+                       new Status(),
+                       array( "Status::getMessage called for a good result, this is incorrect\n" ),
+                       'internalerror_info'
+               );
+               $status = new Status();
+               $status->ok = false;
+               $testCases[ 'GoodButNoError' ] = array(
+                       $status,
+                       array( "Status::getMessage: Invalid result object: no error text but not OK\n" ),
+                       'internalerror_info'
+               );
+               $status = new Status();
+               $status->warning( 'fooBar!' );
+               $testCases[ '1StringWarning' ] = array(
+                       $status,
+                       array(),
+                       "fooBar!"
+               );
+               //NOTE: this seems to return a string instead of a Message object...
+ //            $status = new Status();
+ //            $status->warning( 'fooBar!' );
+ //            $status->warning( 'fooBar2!' );
+ //            $testCases[ '2StringWarnings' ] = array(
+ //                    $status,
+ //                    array(),
+ //                    ''
+ //            );
+               $status = new Status();
+               $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' )  ) );
+               $testCases[ '1MessageWarning' ] = array(
+                       $status,
+                       array( 'foo', 'bar' ),
+                       "fooBar!",
+               );
+               //NOTE: this seems to return a string instead of a Message object...
+ //            $status = new Status();
+ //            $status->warning( new Message( 'fooBar!', array( 'foo', 'bar' ) ) );
+ //            $status->warning( new Message( 'fooBar2!' ) );
+ //            $testCases[ '2MessageWarnings' ] = array(
+ //                    $status,
+ //                    array(),
+ //                    "",
+ //            );
+               return $testCases;
+       }
+       /**
+        * @covers Status::replaceMessage
+        */
+       public function testReplaceMessage() {
+               $status = new Status();
+               $message = new Message( 'key1', array( 'foo1', 'bar1' ) );
+               $status->error( $message );
+               $newMessage = new Message( 'key2', array( 'foo2', 'bar2' ) );
+               $status->replaceMessage( $message, $newMessage );
+               $this->assertEquals( $newMessage, $status->errors[0]['message'] );
+       }
        //todo test getErrorMessage
-       //todo test getHTML
        //todo test getErrorMessageArray
        //todo test getStatusArray
        //todo test getErrorsByType
-       //todo test replaceMessage
  
  }