Fix phpcs warnings
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiResultTest.php
index f2fcb4a..f894f87 100644 (file)
@@ -106,6 +106,16 @@ class ApiResultTest extends MediaWikiTestCase {
                                'Expected exception'
                        );
                }
+               try {
+                       ApiResult::setValue( $arr, null, $fh );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( InvalidArgumentException $ex ) {
+                       $this->assertSame(
+                               'Cannot add resource(stream) to ApiResult',
+                               $ex->getMessage(),
+                               'Expected exception'
+                       );
+               }
                try {
                        $obj->file = $fh;
                        ApiResult::setValue( $arr, 'sub', $obj );
@@ -117,6 +127,17 @@ class ApiResultTest extends MediaWikiTestCase {
                                'Expected exception'
                        );
                }
+               try {
+                       $obj->file = $fh;
+                       ApiResult::setValue( $arr, null, $obj );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( InvalidArgumentException $ex ) {
+                       $this->assertSame(
+                               'Cannot add resource(stream) to ApiResult',
+                               $ex->getMessage(),
+                               'Expected exception'
+                       );
+               }
                fclose( $fh );
 
                try {
@@ -129,6 +150,16 @@ class ApiResultTest extends MediaWikiTestCase {
                                'Expected exception'
                        );
                }
+               try {
+                       ApiResult::setValue( $arr, null, INF );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( InvalidArgumentException $ex ) {
+                       $this->assertSame(
+                               'Cannot add non-finite floats to ApiResult',
+                               $ex->getMessage(),
+                               'Expected exception'
+                       );
+               }
                try {
                        ApiResult::setValue( $arr, 'nan', NAN );
                        $this->fail( 'Expected exception not thrown' );
@@ -139,6 +170,16 @@ class ApiResultTest extends MediaWikiTestCase {
                                'Expected exception'
                        );
                }
+               try {
+                       ApiResult::setValue( $arr, null, NAN );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( InvalidArgumentException $ex ) {
+                       $this->assertSame(
+                               'Cannot add non-finite floats to ApiResult',
+                               $ex->getMessage(),
+                               'Expected exception'
+                       );
+               }
 
                $arr = array();
                $result2 = new ApiResult( 8388608 );
@@ -155,10 +196,14 @@ class ApiResultTest extends MediaWikiTestCase {
                ApiResult::setValue( $arr, 'foo', "foo\x80bar" );
                ApiResult::setValue( $arr, 'bar', "a\xcc\x81" );
                ApiResult::setValue( $arr, 'baz', 74 );
+               ApiResult::setValue( $arr, null, "foo\x80bar" );
+               ApiResult::setValue( $arr, null, "a\xcc\x81" );
                $this->assertSame( array(
                        'foo' => "foo\xef\xbf\xbdbar",
                        'bar' => "\xc3\xa1",
                        'baz' => 74,
+                       0 => "foo\xef\xbf\xbdbar",
+                       1 => "\xc3\xa1",
                ), $arr );
        }
 
@@ -288,6 +333,16 @@ class ApiResultTest extends MediaWikiTestCase {
                                'Expected exception'
                        );
                }
+               try {
+                       $result->addValue( null, null, $fh );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( InvalidArgumentException $ex ) {
+                       $this->assertSame(
+                               'Cannot add resource(stream) to ApiResult',
+                               $ex->getMessage(),
+                               'Expected exception'
+                       );
+               }
                try {
                        $obj->file = $fh;
                        $result->addValue( null, 'sub', $obj );
@@ -299,6 +354,17 @@ class ApiResultTest extends MediaWikiTestCase {
                                'Expected exception'
                        );
                }
+               try {
+                       $obj->file = $fh;
+                       $result->addValue( null, null, $obj );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( InvalidArgumentException $ex ) {
+                       $this->assertSame(
+                               'Cannot add resource(stream) to ApiResult',
+                               $ex->getMessage(),
+                               'Expected exception'
+                       );
+               }
                fclose( $fh );
 
                try {
@@ -311,6 +377,16 @@ class ApiResultTest extends MediaWikiTestCase {
                                'Expected exception'
                        );
                }
+               try {
+                       $result->addValue( null, null, INF );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( InvalidArgumentException $ex ) {
+                       $this->assertSame(
+                               'Cannot add non-finite floats to ApiResult',
+                               $ex->getMessage(),
+                               'Expected exception'
+                       );
+               }
                try {
                        $result->addValue( null, 'nan', NAN );
                        $this->fail( 'Expected exception not thrown' );
@@ -321,6 +397,16 @@ class ApiResultTest extends MediaWikiTestCase {
                                'Expected exception'
                        );
                }
+               try {
+                       $result->addValue( null, null, NAN );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( InvalidArgumentException $ex ) {
+                       $this->assertSame(
+                               'Cannot add non-finite floats to ApiResult',
+                               $ex->getMessage(),
+                               'Expected exception'
+                       );
+               }
 
                $result->reset();
                $result->addParsedLimit( 'foo', 12 );
@@ -374,10 +460,14 @@ class ApiResultTest extends MediaWikiTestCase {
                $result->addValue( null, 'foo', "foo\x80bar" );
                $result->addValue( null, 'bar', "a\xcc\x81" );
                $result->addValue( null, 'baz', 74 );
+               $result->addValue( null, null, "foo\x80bar" );
+               $result->addValue( null, null, "a\xcc\x81" );
                $this->assertSame( array(
                        'foo' => "foo\xef\xbf\xbdbar",
                        'bar' => "\xc3\xa1",
                        'baz' => 74,
+                       0 => "foo\xef\xbf\xbdbar",
+                       1 => "\xc3\xa1",
                        ApiResult::META_TYPE => 'assoc',
                ), $result->getResultData() );
        }
@@ -781,8 +871,12 @@ class ApiResultTest extends MediaWikiTestCase {
                                array( 'Types' => array( 'AssocAsObject' => true ) ),
                                (object)array(
                                        'defaultArray' => array( 'b', 'c', 'a', ApiResult::META_TYPE => 'array' ),
-                                       'defaultAssoc' => (object)array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult::META_TYPE => 'assoc' ),
-                                       'defaultAssoc2' => (object)array( 2 => 'a', 3 => 'b', 0 => 'c', ApiResult::META_TYPE => 'assoc' ),
+                                       'defaultAssoc' => (object)array( 'x' => 'a',
+                                               1 => 'b', 0 => 'c', ApiResult::META_TYPE => 'assoc'
+                                       ),
+                                       'defaultAssoc2' => (object)array( 2 => 'a', 3 => 'b',
+                                               0 => 'c', ApiResult::META_TYPE => 'assoc'
+                                       ),
                                        'array' => array( 'a', 'c', 'b', ApiResult::META_TYPE => 'array' ),
                                        'BCarray' => array( 'a', 'c', 'b', ApiResult::META_TYPE => 'array' ),
                                        'BCassoc' => (object)array( 'a', 'b', 'c', ApiResult::META_TYPE => 'assoc' ),
@@ -870,8 +964,12 @@ class ApiResultTest extends MediaWikiTestCase {
                                array( 'Types' => array( 'ArmorKVP' => 'name', 'AssocAsObject' => true ) ),
                                (object)array(
                                        'defaultArray' => array( 'b', 'c', 'a', ApiResult::META_TYPE => 'array' ),
-                                       'defaultAssoc' => (object)array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult::META_TYPE => 'assoc' ),
-                                       'defaultAssoc2' => (object)array( 2 => 'a', 3 => 'b', 0 => 'c', ApiResult::META_TYPE => 'assoc' ),
+                                       'defaultAssoc' => (object)array( 'x' => 'a', 1 => 'b',
+                                               0 => 'c', ApiResult::META_TYPE => 'assoc'
+                                       ),
+                                       'defaultAssoc2' => (object)array( 2 => 'a', 3 => 'b',
+                                               0 => 'c', ApiResult::META_TYPE => 'assoc'
+                                       ),
                                        'array' => array( 'a', 'c', 'b', ApiResult::META_TYPE => 'array' ),
                                        'BCarray' => array( 'a', 'c', 'b', ApiResult::META_TYPE => 'array' ),
                                        'BCassoc' => (object)array( 'a', 'b', 'c', ApiResult::META_TYPE => 'assoc' ),
@@ -1034,6 +1132,76 @@ class ApiResultTest extends MediaWikiTestCase {
                $data[ApiResult::META_CONTENT] = 'bar';
        }
 
+       /**
+        * @covers ApiResult
+        */
+       public function testAddMetadataToResultVars() {
+               $arr = array(
+                       'a' => "foo",
+                       'b' => false,
+                       'c' => 10,
+                       'sequential_numeric_keys' => array( 'a', 'b', 'c' ),
+                       'non_sequential_numeric_keys' => array( 'a', 'b', 4 => 'c' ),
+                       'string_keys' => array(
+                               'one' => 1,
+                               'two' => 2
+                       ),
+                       'object_sequential_keys' => (object)array( 'a', 'b', 'c' ),
+                       '_type' => "should be overwritten in result",
+               );
+               $this->assertSame( array(
+                       ApiResult::META_TYPE => 'kvp',
+                       ApiResult::META_KVP_KEY_NAME => 'key',
+                       ApiResult::META_PRESERVE_KEYS => array(
+                               'a', 'b', 'c',
+                               'sequential_numeric_keys', 'non_sequential_numeric_keys',
+                               'string_keys', 'object_sequential_keys'
+                       ),
+                       ApiResult::META_BC_BOOLS => array( 'b' ),
+                       ApiResult::META_INDEXED_TAG_NAME => 'var',
+                       'a' => "foo",
+                       'b' => false,
+                       'c' => 10,
+                       'sequential_numeric_keys' => array(
+                               ApiResult::META_TYPE => 'array',
+                               ApiResult::META_BC_BOOLS => array(),
+                               ApiResult::META_INDEXED_TAG_NAME => 'value',
+                               0 => 'a',
+                               1 => 'b',
+                               2 => 'c',
+                       ),
+                       'non_sequential_numeric_keys' => array(
+                               ApiResult::META_TYPE => 'kvp',
+                               ApiResult::META_KVP_KEY_NAME => 'key',
+                               ApiResult::META_PRESERVE_KEYS => array( 0, 1, 4 ),
+                               ApiResult::META_BC_BOOLS => array(),
+                               ApiResult::META_INDEXED_TAG_NAME => 'var',
+                               0 => 'a',
+                               1 => 'b',
+                               4 => 'c',
+                       ),
+                       'string_keys' => array(
+                               ApiResult::META_TYPE => 'kvp',
+                               ApiResult::META_KVP_KEY_NAME => 'key',
+                               ApiResult::META_PRESERVE_KEYS => array( 'one', 'two' ),
+                               ApiResult::META_BC_BOOLS => array(),
+                               ApiResult::META_INDEXED_TAG_NAME => 'var',
+                               'one' => 1,
+                               'two' => 2,
+                       ),
+                       'object_sequential_keys' => array(
+                               ApiResult::META_TYPE => 'kvp',
+                               ApiResult::META_KVP_KEY_NAME => 'key',
+                               ApiResult::META_PRESERVE_KEYS => array( 0, 1, 2 ),
+                               ApiResult::META_BC_BOOLS => array(),
+                               ApiResult::META_INDEXED_TAG_NAME => 'var',
+                               0 => 'a',
+                               1 => 'b',
+                               2 => 'c',
+                       ),
+               ), ApiResult::addMetadataToResultVars( $arr ) );
+       }
+
        /**
         * @covers ApiResult
         */
@@ -1043,7 +1211,8 @@ class ApiResultTest extends MediaWikiTestCase {
                        if ( preg_match( '/Use of ApiResult::\S+ was deprecated in MediaWiki \d+.\d+\./', $errstr ) ) {
                                return true;
                        }
-                       if ( preg_match( '/Use of ApiMain to ApiResult::__construct was deprecated in MediaWiki \d+.\d+\./', $errstr ) ) {
+                       if ( preg_match( '/Use of ApiMain to ApiResult::__construct ' .
+                               'was deprecated in MediaWiki \d+.\d+\./', $errstr ) ) {
                                return true;
                        }
                        return false;
@@ -1361,7 +1530,8 @@ class ApiResultTest extends MediaWikiTestCase {
 
                $result = new ApiResult( 8388608 );
                $result->setMainForContinuation( $main );
-               $result->beginContinuation( '||mock2', array_slice( $allModules, 0, 2 ), array( 'mock1', 'mock2' ) );
+               $result->beginContinuation( '||mock2', array_slice( $allModules, 0, 2 ),
+                       array( 'mock1', 'mock2' ) );
                try {
                        $result->setContinueParam( $allModules[1], 'm2continue', 1 );
                        $this->fail( 'Expected exception not thrown' );
@@ -1377,7 +1547,8 @@ class ApiResultTest extends MediaWikiTestCase {
                        $this->fail( 'Expected exception not thrown' );
                } catch ( UnexpectedValueException $ex ) {
                        $this->assertSame(
-                               'Module \'mocklist\' called ApiContinuationManager::addContinueParam but was not passed to ApiContinuationManager::__construct',
+                               'Module \'mocklist\' called ApiContinuationManager::addContinueParam ' .
+                                       'but was not passed to ApiContinuationManager::__construct',
                                $ex->getMessage(),
                                'Expected exception'
                        );
@@ -1405,13 +1576,14 @@ class ApiResultTest extends MediaWikiTestCase {
 
                try {
                        $arr = array();
-                       ApiResult::setValue( $arr, 'foo',  new ApiResultTestSerializableObject(
+                       ApiResult::setValue( $arr, 'foo', new ApiResultTestSerializableObject(
                                new ApiResultTestStringifiableObject()
                        ) );
                        $this->fail( 'Expected exception not thrown' );
                } catch ( UnexpectedValueException $ex ) {
                        $this->assertSame(
-                               'ApiResultTestSerializableObject::serializeForApiResult() returned an object of class ApiResultTestStringifiableObject',
+                               'ApiResultTestSerializableObject::serializeForApiResult() ' .
+                                       'returned an object of class ApiResultTestStringifiableObject',
                                $ex->getMessage(),
                                'Expected exception'
                        );
@@ -1419,18 +1591,19 @@ class ApiResultTest extends MediaWikiTestCase {
 
                try {
                        $arr = array();
-                       ApiResult::setValue( $arr, 'foo',  new ApiResultTestSerializableObject( NAN ) );
+                       ApiResult::setValue( $arr, 'foo', new ApiResultTestSerializableObject( NAN ) );
                        $this->fail( 'Expected exception not thrown' );
                } catch ( UnexpectedValueException $ex ) {
                        $this->assertSame(
-                               'ApiResultTestSerializableObject::serializeForApiResult() returned an invalid value: Cannot add non-finite floats to ApiResult',
+                               'ApiResultTestSerializableObject::serializeForApiResult() ' .
+                                       'returned an invalid value: Cannot add non-finite floats to ApiResult',
                                $ex->getMessage(),
                                'Expected exception'
                        );
                }
 
                $arr = array();
-               ApiResult::setValue( $arr, 'foo',  new ApiResultTestSerializableObject(
+               ApiResult::setValue( $arr, 'foo', new ApiResultTestSerializableObject(
                        array(
                                'one' => new ApiResultTestStringifiableObject( '1' ),
                                'two' => new ApiResultTestSerializableObject( 2 ),