7 class ApiResultTest
extends MediaWikiTestCase
{
12 public function testStaticDataMethods() {
15 ApiResult
::setValue( $arr, 'setValue', '1' );
17 ApiResult
::setValue( $arr, null, 'unnamed 1' );
18 ApiResult
::setValue( $arr, null, 'unnamed 2' );
20 ApiResult
::setValue( $arr, 'deleteValue', '2' );
21 ApiResult
::unsetValue( $arr, 'deleteValue' );
23 ApiResult
::setContentValue( $arr, 'setContentValue', '3' );
25 $this->assertSame( array(
29 ApiResult
::META_CONTENT
=> 'setContentValue',
30 'setContentValue' => '3',
34 ApiResult
::setValue( $arr, 'setValue', '99' );
35 $this->fail( 'Expected exception not thrown' );
36 } catch ( RuntimeException
$ex ) {
38 'Attempting to add element setValue=99, existing value is 1',
45 ApiResult
::setContentValue( $arr, 'setContentValue2', '99' );
46 $this->fail( 'Expected exception not thrown' );
47 } catch ( RuntimeException
$ex ) {
49 'Attempting to set content element as setContentValue2 when setContentValue ' .
50 'is already set as the content element',
56 ApiResult
::setValue( $arr, 'setValue', '99', ApiResult
::OVERRIDE
);
57 $this->assertSame( '99', $arr['setValue'] );
59 ApiResult
::setContentValue( $arr, 'setContentValue2', '99', ApiResult
::OVERRIDE
);
60 $this->assertSame( 'setContentValue2', $arr[ApiResult
::META_CONTENT
] );
62 $arr = array( 'foo' => 1, 'bar' => 1 );
63 ApiResult
::setValue( $arr, 'top', '2', ApiResult
::ADD_ON_TOP
);
64 ApiResult
::setValue( $arr, null, '2', ApiResult
::ADD_ON_TOP
);
65 ApiResult
::setValue( $arr, 'bottom', '2' );
66 ApiResult
::setValue( $arr, 'foo', '2', ApiResult
::OVERRIDE
);
67 ApiResult
::setValue( $arr, 'bar', '2', ApiResult
::OVERRIDE | ApiResult
::ADD_ON_TOP
);
68 $this->assertSame( array( 0, 'top', 'foo', 'bar', 'bottom' ), array_keys( $arr ) );
71 ApiResult
::setValue( $arr, 'sub', array( 'foo' => 1 ) );
72 ApiResult
::setValue( $arr, 'sub', array( 'bar' => 1 ) );
73 $this->assertSame( array( 'sub' => array( 'foo' => 1, 'bar' => 1 ) ), $arr );
76 ApiResult
::setValue( $arr, 'sub', array( 'foo' => 2, 'baz' => 2 ) );
77 $this->fail( 'Expected exception not thrown' );
78 } catch ( RuntimeException
$ex ) {
80 'Conflicting keys (foo) when attempting to merge element sub',
87 $title = Title
::newFromText( "MediaWiki:Foobar" );
91 ApiResult
::setValue( $arr, 'title', $title );
92 ApiResult
::setValue( $arr, 'obj', $obj );
93 $this->assertSame( array(
94 'title' => (string)$title,
95 'obj' => array( 'foo' => 1, 'bar' => 2, ApiResult
::META_TYPE
=> 'assoc' ),
100 ApiResult
::setValue( $arr, 'file', $fh );
101 $this->fail( 'Expected exception not thrown' );
102 } catch ( InvalidArgumentException
$ex ) {
104 'Cannot add resource(stream) to ApiResult',
111 ApiResult
::setValue( $arr, 'sub', $obj );
112 $this->fail( 'Expected exception not thrown' );
113 } catch ( InvalidArgumentException
$ex ) {
115 'Cannot add resource(stream) to ApiResult',
123 ApiResult
::setValue( $arr, 'inf', INF
);
124 $this->fail( 'Expected exception not thrown' );
125 } catch ( InvalidArgumentException
$ex ) {
127 'Cannot add non-finite floats to ApiResult',
133 ApiResult
::setValue( $arr, 'nan', NAN
);
134 $this->fail( 'Expected exception not thrown' );
135 } catch ( InvalidArgumentException
$ex ) {
137 'Cannot add non-finite floats to ApiResult',
144 $result2 = new ApiResult( 8388608 );
145 $result2->addValue( null, 'foo', 'bar' );
146 ApiResult
::setValue( $arr, 'baz', $result2 );
147 $this->assertSame( array( 'baz' => array( 'foo' => 'bar' ) ), $arr );
150 ApiResult
::setValue( $arr, 'foo', "foo\x80bar" );
151 ApiResult
::setValue( $arr, 'bar', "a\xcc\x81" );
152 ApiResult
::setValue( $arr, 'baz', 74 );
153 $this->assertSame( array(
154 'foo' => "foo\xef\xbf\xbdbar",
163 public function testInstanceDataMethods() {
164 $result = new ApiResult( 8388608 );
166 $result->addValue( null, 'setValue', '1' );
168 $result->addValue( null, null, 'unnamed 1' );
169 $result->addValue( null, null, 'unnamed 2' );
171 $result->addValue( null, 'deleteValue', '2' );
172 $result->removeValue( null, 'deleteValue' );
174 $result->addValue( array( 'a', 'b' ), 'deleteValue', '3' );
175 $result->removeValue( array( 'a', 'b', 'deleteValue' ), null, '3' );
177 $result->addContentValue( null, 'setContentValue', '3' );
179 $this->assertSame( array(
183 'a' => array( 'b' => array() ),
184 'setContentValue' => '3',
185 ApiResult
::META_CONTENT
=> 'setContentValue',
186 ), $result->getResultData() );
187 $this->assertSame( 20, $result->getSize() );
190 $result->addValue( null, 'setValue', '99' );
191 $this->fail( 'Expected exception not thrown' );
192 } catch ( RuntimeException
$ex ) {
194 'Attempting to add element setValue=99, existing value is 1',
201 $result->addContentValue( null, 'setContentValue2', '99' );
202 $this->fail( 'Expected exception not thrown' );
203 } catch ( RuntimeException
$ex ) {
205 'Attempting to set content element as setContentValue2 when setContentValue ' .
206 'is already set as the content element',
212 $result->addValue( null, 'setValue', '99', ApiResult
::OVERRIDE
);
213 $this->assertSame( '99', $result->getResultData( array( 'setValue' ) ) );
215 $result->addContentValue( null, 'setContentValue2', '99', ApiResult
::OVERRIDE
);
216 $this->assertSame( 'setContentValue2',
217 $result->getResultData( array( ApiResult
::META_CONTENT
) ) );
220 $this->assertSame( array(), $result->getResultData() );
221 $this->assertSame( 0, $result->getSize() );
223 $result->addValue( null, 'foo', 1 );
224 $result->addValue( null, 'bar', 1 );
225 $result->addValue( null, 'top', '2', ApiResult
::ADD_ON_TOP
);
226 $result->addValue( null, null, '2', ApiResult
::ADD_ON_TOP
);
227 $result->addValue( null, 'bottom', '2' );
228 $result->addValue( null, 'foo', '2', ApiResult
::OVERRIDE
);
229 $result->addValue( null, 'bar', '2', ApiResult
::OVERRIDE | ApiResult
::ADD_ON_TOP
);
230 $this->assertSame( array( 0, 'top', 'foo', 'bar', 'bottom' ),
231 array_keys( $result->getResultData() ) );
234 $result->addValue( null, 'foo', array( 'bar' => 1 ) );
235 $result->addValue( array( 'foo', 'top' ), 'x', 2, ApiResult
::ADD_ON_TOP
);
236 $result->addValue( array( 'foo', 'bottom' ), 'x', 2 );
237 $this->assertSame( array( 'top', 'bar', 'bottom' ),
238 array_keys( $result->getResultData( array( 'foo' ) ) ) );
241 $result->addValue( null, 'sub', array( 'foo' => 1 ) );
242 $result->addValue( null, 'sub', array( 'bar' => 1 ) );
243 $this->assertSame( array( 'sub' => array( 'foo' => 1, 'bar' => 1 ) ),
244 $result->getResultData() );
247 $result->addValue( null, 'sub', array( 'foo' => 2, 'baz' => 2 ) );
248 $this->fail( 'Expected exception not thrown' );
249 } catch ( RuntimeException
$ex ) {
251 'Conflicting keys (foo) when attempting to merge element sub',
258 $title = Title
::newFromText( "MediaWiki:Foobar" );
262 $result->addValue( null, 'title', $title );
263 $result->addValue( null, 'obj', $obj );
264 $this->assertSame( array(
265 'title' => (string)$title,
266 'obj' => array( 'foo' => 1, 'bar' => 2, ApiResult
::META_TYPE
=> 'assoc' ),
267 ), $result->getResultData() );
271 $result->addValue( null, 'file', $fh );
272 $this->fail( 'Expected exception not thrown' );
273 } catch ( InvalidArgumentException
$ex ) {
275 'Cannot add resource(stream) to ApiResult',
282 $result->addValue( null, 'sub', $obj );
283 $this->fail( 'Expected exception not thrown' );
284 } catch ( InvalidArgumentException
$ex ) {
286 'Cannot add resource(stream) to ApiResult',
294 $result->addValue( null, 'inf', INF
);
295 $this->fail( 'Expected exception not thrown' );
296 } catch ( InvalidArgumentException
$ex ) {
298 'Cannot add non-finite floats to ApiResult',
304 $result->addValue( null, 'nan', NAN
);
305 $this->fail( 'Expected exception not thrown' );
306 } catch ( InvalidArgumentException
$ex ) {
308 'Cannot add non-finite floats to ApiResult',
315 $result->addParsedLimit( 'foo', 12 );
316 $this->assertSame( array( 'limits' => array( 'foo' => 12 ) ), $result->getResultData() );
317 $result->addParsedLimit( 'foo', 13 );
318 $this->assertSame( array( 'limits' => array( 'foo' => 13 ) ), $result->getResultData() );
319 $this->assertSame( null, $result->getResultData( array( 'foo', 'bar', 'baz' ) ) );
320 $this->assertSame( 13, $result->getResultData( array( 'limits', 'foo' ) ) );
322 $result->getResultData( array( 'limits', 'foo', 'bar' ) );
323 $this->fail( 'Expected exception not thrown' );
324 } catch ( InvalidArgumentException
$ex ) {
326 'Path limits.foo is not an array',
332 $result = new ApiResult( 10 );
333 $formatter = new ApiErrorFormatter( $result, Language
::factory( 'en' ), 'none', false );
334 $result->setErrorFormatter( $formatter );
335 $this->assertFalse( $result->addValue( null, 'foo', '12345678901' ) );
336 $this->assertTrue( $result->addValue( null, 'foo', '12345678901', ApiResult
::NO_SIZE_CHECK
) );
337 $this->assertSame( 0, $result->getSize() );
339 $this->assertTrue( $result->addValue( null, 'foo', '1234567890' ) );
340 $this->assertFalse( $result->addValue( null, 'foo', '1' ) );
341 $result->removeValue( null, 'foo' );
342 $this->assertTrue( $result->addValue( null, 'foo', '1' ) );
344 $result = new ApiResult( 8388608 );
345 $result2 = new ApiResult( 8388608 );
346 $result2->addValue( null, 'foo', 'bar' );
347 $result->addValue( null, 'baz', $result2 );
348 $this->assertSame( array( 'baz' => array( 'foo' => 'bar' ) ), $result->getResultData() );
350 $result = new ApiResult( 8388608 );
351 $result->addValue( null, 'foo', "foo\x80bar" );
352 $result->addValue( null, 'bar', "a\xcc\x81" );
353 $result->addValue( null, 'baz', 74 );
354 $this->assertSame( array(
355 'foo' => "foo\xef\xbf\xbdbar",
358 ), $result->getResultData() );
364 public function testMetadata() {
365 $arr = array( 'foo' => array( 'bar' => array() ) );
366 $result = new ApiResult( 8388608 );
367 $result->addValue( null, 'foo', array( 'bar' => array() ) );
372 ApiResult
::META_INDEXED_TAG_NAME
=> 'ritn',
373 ApiResult
::META_TYPE
=> 'default',
375 ApiResult
::META_INDEXED_TAG_NAME
=> 'ritn',
376 ApiResult
::META_TYPE
=> 'default',
378 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
379 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
380 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar' ),
381 ApiResult
::META_TYPE
=> 'array',
384 ApiResult
::setSubelementsList( $arr, 'foo' );
385 ApiResult
::setSubelementsList( $arr, array( 'bar', 'baz' ) );
386 ApiResult
::unsetSubelementsList( $arr, 'baz' );
387 ApiResult
::setIndexedTagNameRecursive( $arr, 'ritn' );
388 ApiResult
::setIndexedTagName( $arr, 'itn' );
389 ApiResult
::setPreserveKeysList( $arr, 'foo' );
390 ApiResult
::setPreserveKeysList( $arr, array( 'bar', 'baz' ) );
391 ApiResult
::unsetPreserveKeysList( $arr, 'baz' );
392 ApiResult
::setArrayTypeRecursive( $arr, 'default' );
393 ApiResult
::setArrayType( $arr, 'array' );
394 $this->assertSame( $expect, $arr );
396 $result->addSubelementsList( null, 'foo' );
397 $result->addSubelementsList( null, array( 'bar', 'baz' ) );
398 $result->removeSubelementsList( null, 'baz' );
399 $result->addIndexedTagNameRecursive( null, 'ritn' );
400 $result->addIndexedTagName( null, 'itn' );
401 $result->addPreserveKeysList( null, 'foo' );
402 $result->addPreserveKeysList( null, array( 'bar', 'baz' ) );
403 $result->removePreserveKeysList( null, 'baz' );
404 $result->addArrayTypeRecursive( null, 'default' );
405 $result->addArrayType( null, 'array' );
406 $this->assertSame( $expect, $result->getResultData() );
408 $arr = array( 'foo' => array( 'bar' => array() ) );
412 ApiResult
::META_TYPE
=> 'kvp',
413 ApiResult
::META_KVP_KEY_NAME
=> 'key',
415 ApiResult
::META_TYPE
=> 'kvp',
416 ApiResult
::META_KVP_KEY_NAME
=> 'key',
418 ApiResult
::META_TYPE
=> 'BCkvp',
419 ApiResult
::META_KVP_KEY_NAME
=> 'bc',
421 ApiResult
::setArrayTypeRecursive( $arr, 'kvp', 'key' );
422 ApiResult
::setArrayType( $arr, 'BCkvp', 'bc' );
423 $this->assertSame( $expect, $arr );
429 public function testUtilityFunctions() {
432 'bar' => array( '_dummy' => 'foobaz' ),
433 'bar2' => (object)array( '_dummy' => 'foobaz' ),
435 '_dummy' => 'foobaz',
437 'foo2' => (object)array(
438 'bar' => array( '_dummy' => 'foobaz' ),
439 'bar2' => (object)array( '_dummy' => 'foobaz' ),
441 '_dummy' => 'foobaz',
443 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
444 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
445 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar', '_dummy2', 0 ),
446 ApiResult
::META_TYPE
=> 'array',
447 '_dummy' => 'foobaz',
448 '_dummy2' => 'foobaz!',
450 $this->assertEquals( array(
453 'bar2' => (object)array(),
456 'foo2' => (object)array(
458 'bar2' => (object)array(),
461 '_dummy2' => 'foobaz!',
462 ), ApiResult
::stripMetadata( $arr ), 'ApiResult::stripMetadata' );
465 $data = ApiResult
::stripMetadataNonRecursive( $arr, $metadata );
466 $this->assertEquals( array(
468 'bar' => array( '_dummy' => 'foobaz' ),
469 'bar2' => (object)array( '_dummy' => 'foobaz' ),
471 '_dummy' => 'foobaz',
473 'foo2' => (object)array(
474 'bar' => array( '_dummy' => 'foobaz' ),
475 'bar2' => (object)array( '_dummy' => 'foobaz' ),
477 '_dummy' => 'foobaz',
479 '_dummy2' => 'foobaz!',
480 ), $data, 'ApiResult::stripMetadataNonRecursive ($data)' );
481 $this->assertEquals( array(
482 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
483 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
484 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar', '_dummy2', 0 ),
485 ApiResult
::META_TYPE
=> 'array',
486 '_dummy' => 'foobaz',
487 ), $metadata, 'ApiResult::stripMetadataNonRecursive ($metadata)' );
490 $data = ApiResult
::stripMetadataNonRecursive( (object)$arr, $metadata );
491 $this->assertEquals( (object)array(
493 'bar' => array( '_dummy' => 'foobaz' ),
494 'bar2' => (object)array( '_dummy' => 'foobaz' ),
496 '_dummy' => 'foobaz',
498 'foo2' => (object)array(
499 'bar' => array( '_dummy' => 'foobaz' ),
500 'bar2' => (object)array( '_dummy' => 'foobaz' ),
502 '_dummy' => 'foobaz',
504 '_dummy2' => 'foobaz!',
505 ), $data, 'ApiResult::stripMetadataNonRecursive on object ($data)' );
506 $this->assertEquals( array(
507 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
508 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
509 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar', '_dummy2', 0 ),
510 ApiResult
::META_TYPE
=> 'array',
511 '_dummy' => 'foobaz',
512 ), $metadata, 'ApiResult::stripMetadataNonRecursive on object ($metadata)' );
517 * @dataProvider provideTransformations
518 * @param string $label
519 * @param array $input
520 * @param array $transforms
521 * @param array|Exception $expect
523 public function testTransformations( $label, $input, $transforms, $expect ) {
524 $result = new ApiResult( false );
525 $result->addValue( null, 'test', $input );
527 if ( $expect instanceof Exception
) {
529 $output = $result->getResultData( 'test', $transforms );
530 $this->fail( 'Expected exception not thrown', $label );
531 } catch ( Exception
$ex ) {
532 $this->assertEquals( $ex, $expect, $label );
535 $output = $result->getResultData( 'test', $transforms );
536 $this->assertEquals( $expect, $output, $label );
540 public function provideTransformations() {
541 $kvp = function ( $keyKey, $key, $valKey, $value ) {
545 ApiResult
::META_PRESERVE_KEYS
=> array( $keyKey ),
546 ApiResult
::META_CONTENT
=> $valKey,
547 ApiResult
::META_TYPE
=> 'assoc',
551 'defaultArray' => array( 2 => 'a', 0 => 'b', 1 => 'c' ),
552 'defaultAssoc' => array( 'x' => 'a', 1 => 'b', 0 => 'c' ),
553 'defaultAssoc2' => array( 2 => 'a', 3 => 'b', 0 => 'c' ),
554 'array' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'array' ),
555 'BCarray' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'BCarray' ),
556 'BCassoc' => array( 'a', 'b', 'c', ApiResult
::META_TYPE
=> 'BCassoc' ),
557 'assoc' => array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
558 'kvp' => array( 'x' => 'a', 'y' => 'b', 'z' => array( 'c' ), ApiResult
::META_TYPE
=> 'kvp' ),
559 'BCkvp' => array( 'x' => 'a', 'y' => 'b',
560 ApiResult
::META_TYPE
=> 'BCkvp',
561 ApiResult
::META_KVP_KEY_NAME
=> 'key',
563 'emptyDefault' => array( '_dummy' => 1 ),
564 'emptyAssoc' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'assoc' ),
566 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy' ),
570 'bar' => array( '_dummy' => 'foobaz' ),
572 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
573 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
574 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar', '_dummy2', 0 ),
575 ApiResult
::META_TYPE
=> 'array',
578 '_dummy' => 'foobaz',
580 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
581 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
582 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar', '_dummy2', 0 ),
583 ApiResult
::META_TYPE
=> 'array',
584 '_dummy' => 'foobaz',
585 '_dummy2' => 'foobaz!',
596 ApiResult
::META_BC_BOOLS
=> array( 0, 'true', 'false' ),
598 array( 'BC' => array() ),
603 ApiResult
::META_BC_BOOLS
=> array( 0, 'true', 'false' ),
607 'BC: META_BC_SUBELEMENTS',
611 ApiResult
::META_BC_SUBELEMENTS
=> array( 'bc' ),
613 array( 'BC' => array() ),
617 ApiResult
::META_CONTENT
=> '*',
618 ApiResult
::META_TYPE
=> 'assoc',
621 ApiResult
::META_BC_SUBELEMENTS
=> array( 'bc' ),
628 ApiResult
::META_CONTENT
=> 'content',
630 array( 'BC' => array() ),
633 ApiResult
::META_CONTENT
=> '*',
639 'foo' => 'foo value',
640 'bar' => 'bar value',
641 '_baz' => 'baz value',
642 ApiResult
::META_TYPE
=> 'BCkvp',
643 ApiResult
::META_KVP_KEY_NAME
=> 'key',
644 ApiResult
::META_PRESERVE_KEYS
=> array( '_baz' ),
646 array( 'BC' => array() ),
648 $kvp( 'key', 'foo', '*', 'foo value' ),
649 $kvp( 'key', 'bar', '*', 'bar value' ),
650 $kvp( 'key', '_baz', '*', 'baz value' ),
651 ApiResult
::META_TYPE
=> 'array',
652 ApiResult
::META_KVP_KEY_NAME
=> 'key',
653 ApiResult
::META_PRESERVE_KEYS
=> array( '_baz' ),
659 ApiResult
::META_TYPE
=> 'BCarray',
661 array( 'BC' => array() ),
663 ApiResult
::META_TYPE
=> 'default',
669 ApiResult
::META_TYPE
=> 'BCassoc',
671 array( 'BC' => array() ),
673 ApiResult
::META_TYPE
=> 'default',
677 'BC: BCkvp exception',
679 ApiResult
::META_TYPE
=> 'BCkvp',
681 array( 'BC' => array() ),
682 new UnexpectedValueException(
683 'Type "BCkvp" used without setting ApiResult::META_KVP_KEY_NAME metadata item'
687 'BC: nobool, no*, nosub',
691 'content' => 'content',
692 ApiResult
::META_CONTENT
=> 'content',
694 ApiResult
::META_BC_SUBELEMENTS
=> array( 'bc' ),
695 'BCarray' => array( ApiResult
::META_TYPE
=> 'BCarray' ),
696 'BCassoc' => array( ApiResult
::META_TYPE
=> 'BCassoc' ),
698 'foo' => 'foo value',
699 'bar' => 'bar value',
700 '_baz' => 'baz value',
701 ApiResult
::META_TYPE
=> 'BCkvp',
702 ApiResult
::META_KVP_KEY_NAME
=> 'key',
703 ApiResult
::META_PRESERVE_KEYS
=> array( '_baz' ),
706 array( 'BC' => array( 'nobool', 'no*', 'nosub' ) ),
710 'content' => 'content',
712 'BCarray' => array( ApiResult
::META_TYPE
=> 'default' ),
713 'BCassoc' => array( ApiResult
::META_TYPE
=> 'default' ),
715 $kvp( 'key', 'foo', '*', 'foo value' ),
716 $kvp( 'key', 'bar', '*', 'bar value' ),
717 $kvp( 'key', '_baz', '*', 'baz value' ),
718 ApiResult
::META_TYPE
=> 'array',
719 ApiResult
::META_KVP_KEY_NAME
=> 'key',
720 ApiResult
::META_PRESERVE_KEYS
=> array( '_baz' ),
722 ApiResult
::META_CONTENT
=> 'content',
723 ApiResult
::META_BC_SUBELEMENTS
=> array( 'bc' ),
728 'Types: Normal transform',
730 array( 'Types' => array() ),
732 'defaultArray' => array( 'b', 'c', 'a', ApiResult
::META_TYPE
=> 'array' ),
733 'defaultAssoc' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
734 'defaultAssoc2' => array( 2 => 'a', 3 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
735 'array' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
736 'BCarray' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
737 'BCassoc' => array( 'a', 'b', 'c', ApiResult
::META_TYPE
=> 'assoc' ),
738 'assoc' => array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
739 'kvp' => array( 'x' => 'a', 'y' => 'b',
740 'z' => array( 'c', ApiResult
::META_TYPE
=> 'array' ),
741 ApiResult
::META_TYPE
=> 'assoc'
743 'BCkvp' => array( 'x' => 'a', 'y' => 'b',
744 ApiResult
::META_TYPE
=> 'assoc',
745 ApiResult
::META_KVP_KEY_NAME
=> 'key',
747 'emptyDefault' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'array' ),
748 'emptyAssoc' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'assoc' ),
750 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy' ),
751 ApiResult
::META_TYPE
=> 'assoc',
755 'Types: AssocAsObject',
757 array( 'Types' => array( 'AssocAsObject' => true ) ),
759 'defaultArray' => array( 'b', 'c', 'a', ApiResult
::META_TYPE
=> 'array' ),
760 'defaultAssoc' => (object)array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
761 'defaultAssoc2' => (object)array( 2 => 'a', 3 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
762 'array' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
763 'BCarray' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
764 'BCassoc' => (object)array( 'a', 'b', 'c', ApiResult
::META_TYPE
=> 'assoc' ),
765 'assoc' => (object)array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
766 'kvp' => (object)array( 'x' => 'a', 'y' => 'b',
767 'z' => array( 'c', ApiResult
::META_TYPE
=> 'array' ),
768 ApiResult
::META_TYPE
=> 'assoc'
770 'BCkvp' => (object)array( 'x' => 'a', 'y' => 'b',
771 ApiResult
::META_TYPE
=> 'assoc',
772 ApiResult
::META_KVP_KEY_NAME
=> 'key',
774 'emptyDefault' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'array' ),
775 'emptyAssoc' => (object)array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'assoc' ),
777 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy' ),
778 ApiResult
::META_TYPE
=> 'assoc',
784 array( 'Types' => array( 'ArmorKVP' => 'name' ) ),
786 'defaultArray' => array( 'b', 'c', 'a', ApiResult
::META_TYPE
=> 'array' ),
787 'defaultAssoc' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
788 'defaultAssoc2' => array( 2 => 'a', 3 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
789 'array' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
790 'BCarray' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
791 'BCassoc' => array( 'a', 'b', 'c', ApiResult
::META_TYPE
=> 'assoc' ),
792 'assoc' => array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
794 $kvp( 'name', 'x', 'value', 'a' ),
795 $kvp( 'name', 'y', 'value', 'b' ),
796 $kvp( 'name', 'z', 'value', array( 'c', ApiResult
::META_TYPE
=> 'array' ) ),
797 ApiResult
::META_TYPE
=> 'array'
800 $kvp( 'key', 'x', 'value', 'a' ),
801 $kvp( 'key', 'y', 'value', 'b' ),
802 ApiResult
::META_TYPE
=> 'array',
803 ApiResult
::META_KVP_KEY_NAME
=> 'key',
805 'emptyDefault' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'array' ),
806 'emptyAssoc' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'assoc' ),
808 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy' ),
809 ApiResult
::META_TYPE
=> 'assoc',
813 'Types: ArmorKVP + BC',
815 array( 'BC' => array(), 'Types' => array( 'ArmorKVP' => 'name' ) ),
817 'defaultArray' => array( 'b', 'c', 'a', ApiResult
::META_TYPE
=> 'array' ),
818 'defaultAssoc' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
819 'defaultAssoc2' => array( 2 => 'a', 3 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
820 'array' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
821 'BCarray' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
822 'BCassoc' => array( 'a', 'b', 'c', ApiResult
::META_TYPE
=> 'array' ),
823 'assoc' => array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
825 $kvp( 'name', 'x', '*', 'a' ),
826 $kvp( 'name', 'y', '*', 'b' ),
827 $kvp( 'name', 'z', '*', array( 'c', ApiResult
::META_TYPE
=> 'array' ) ),
828 ApiResult
::META_TYPE
=> 'array'
831 $kvp( 'key', 'x', '*', 'a' ),
832 $kvp( 'key', 'y', '*', 'b' ),
833 ApiResult
::META_TYPE
=> 'array',
834 ApiResult
::META_KVP_KEY_NAME
=> 'key',
836 'emptyDefault' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'array' ),
837 'emptyAssoc' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'assoc' ),
839 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy' ),
840 ApiResult
::META_TYPE
=> 'assoc',
844 'Types: ArmorKVP + AssocAsObject',
846 array( 'Types' => array( 'ArmorKVP' => 'name', 'AssocAsObject' => true ) ),
848 'defaultArray' => array( 'b', 'c', 'a', ApiResult
::META_TYPE
=> 'array' ),
849 'defaultAssoc' => (object)array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
850 'defaultAssoc2' => (object)array( 2 => 'a', 3 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
851 'array' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
852 'BCarray' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
853 'BCassoc' => (object)array( 'a', 'b', 'c', ApiResult
::META_TYPE
=> 'assoc' ),
854 'assoc' => (object)array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
856 (object)$kvp( 'name', 'x', 'value', 'a' ),
857 (object)$kvp( 'name', 'y', 'value', 'b' ),
858 (object)$kvp( 'name', 'z', 'value', array( 'c', ApiResult
::META_TYPE
=> 'array' ) ),
859 ApiResult
::META_TYPE
=> 'array'
862 (object)$kvp( 'key', 'x', 'value', 'a' ),
863 (object)$kvp( 'key', 'y', 'value', 'b' ),
864 ApiResult
::META_TYPE
=> 'array',
865 ApiResult
::META_KVP_KEY_NAME
=> 'key',
867 'emptyDefault' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'array' ),
868 'emptyAssoc' => (object)array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'assoc' ),
870 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy' ),
871 ApiResult
::META_TYPE
=> 'assoc',
875 'Types: BCkvp exception',
877 ApiResult
::META_TYPE
=> 'BCkvp',
879 array( 'Types' => array() ),
880 new UnexpectedValueException(
881 'Type "BCkvp" used without setting ApiResult::META_KVP_KEY_NAME metadata item'
886 'Strip: With ArmorKVP + AssocAsObject transforms',
888 array( 'Types' => array( 'ArmorKVP' => 'name', 'AssocAsObject' => true ), 'Strip' => 'all' ),
890 'defaultArray' => array( 'b', 'c', 'a' ),
891 'defaultAssoc' => (object)array( 'x' => 'a', 1 => 'b', 0 => 'c' ),
892 'defaultAssoc2' => (object)array( 2 => 'a', 3 => 'b', 0 => 'c' ),
893 'array' => array( 'a', 'c', 'b' ),
894 'BCarray' => array( 'a', 'c', 'b' ),
895 'BCassoc' => (object)array( 'a', 'b', 'c' ),
896 'assoc' => (object)array( 2 => 'a', 0 => 'b', 1 => 'c' ),
898 (object)array( 'name' => 'x', 'value' => 'a' ),
899 (object)array( 'name' => 'y', 'value' => 'b' ),
900 (object)array( 'name' => 'z', 'value' => array( 'c' ) ),
903 (object)array( 'key' => 'x', 'value' => 'a' ),
904 (object)array( 'key' => 'y', 'value' => 'b' ),
906 'emptyDefault' => array(),
907 'emptyAssoc' => (object)array(),
915 array( 'Strip' => 'all' ),
922 '_dummy2' => 'foobaz!',
928 array( 'Strip' => 'base' ),
931 'bar' => array( '_dummy' => 'foobaz' ),
933 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
934 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
935 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar', '_dummy2', 0 ),
936 ApiResult
::META_TYPE
=> 'array',
939 '_dummy' => 'foobaz',
941 '_dummy2' => 'foobaz!',
947 array( 'Strip' => 'bc' ),
952 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
953 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
957 '_dummy2' => 'foobaz!',
958 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
959 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
971 ApiResult
::META_CONTENT
=> 'foo',
972 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy2', '_dummy3' ),
975 'Custom' => array( $this, 'customTransform' ),
983 'baz' => array( 'a', 'b' ),
984 '_dummy2' => '_DUMMY2',
985 '_dummy3' => '_DUMMY3',
986 ApiResult
::META_CONTENT
=> 'bar',
994 * Custom transformer for testTransformations
995 * @param array &$data
996 * @param array &$metadata
998 public function customTransform( &$data, &$metadata ) {
1000 if ( isset( $metadata['_added'] ) ) {
1001 $metadata[ApiResult
::META_TYPE
] = 'array';
1005 foreach ( $data as $k => $v ) {
1006 $data[$k] = strtoupper( $k );
1008 $data['baz'] = array( '_added' => 1, 'z' => 'b', 'y' => 'a' );
1009 $metadata[ApiResult
::META_PRESERVE_KEYS
][0] = '_dummy';
1010 $data[ApiResult
::META_CONTENT
] = 'bar';
1016 public function testDeprecatedFunctions() {
1017 // Ignore ApiResult deprecation warnings during this test
1018 set_error_handler( function ( $errno, $errstr ) use ( &$warnings ) {
1019 if ( preg_match( '/Use of ApiResult::\S+ was deprecated in MediaWiki \d+.\d+\./', $errstr ) ) {
1024 $reset = new ScopedCallback( 'restore_error_handler' );
1026 $context = new DerivativeContext( RequestContext
::getMain() );
1027 $context->setConfig( new HashConfig( array(
1028 'APIModules' => array(),
1029 'APIFormatModules' => array(),
1030 'APIMaxResultSize' => 42,
1032 $main = new ApiMain( $context );
1033 $result = TestingAccessWrapper
::newFromObject( new ApiResult( $main ) );
1034 $this->assertSame( 42, $result->maxSize
);
1035 $this->assertSame( $main->getErrorFormatter(), $result->errorFormatter
);
1036 $this->assertSame( $main, $result->mainForContinuation
);
1038 $result = new ApiResult( 8388608 );
1040 $result->addContentValue( null, 'test', 'content' );
1041 $result->addContentValue( array( 'foo', 'bar' ), 'test', 'content' );
1042 $result->addIndexedTagName( null, 'itn' );
1043 $result->addSubelementsList( null, array( 'sub' ) );
1044 $this->assertSame( array(
1051 ), $result->getData() );
1052 $result->setRawMode();
1053 $this->assertSame( array(
1060 '_element' => 'itn',
1061 '_subelements' => array( 'sub' ),
1062 ), $result->getData() );
1065 ApiResult
::setContent( $arr, 'value' );
1066 ApiResult
::setContent( $arr, 'value2', 'foobar' );
1067 $this->assertSame( array(
1068 ApiResult
::META_CONTENT
=> 'content',
1069 'content' => 'value',
1071 ApiResult
::META_CONTENT
=> 'content',
1072 'content' => 'value2',
1076 $result = new ApiResult( 3 );
1077 $formatter = new ApiErrorFormatter_BackCompat( $result );
1078 $result->setErrorFormatter( $formatter );
1079 $result->disableSizeCheck();
1080 $this->assertTrue( $result->addValue( null, 'foo', '1234567890' ) );
1081 $result->enableSizeCheck();
1082 $this->assertSame( 0, $result->getSize() );
1083 $this->assertFalse( $result->addValue( null, 'foo', '1234567890' ) );
1085 $arr = array( 'foo' => array( 'bar' => 1 ) );
1086 $result->setIndexedTagName_recursive( $arr, 'itn' );
1087 $this->assertSame( array(
1090 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn'
1094 $status = Status
::newGood();
1095 $status->fatal( 'parentheses', '1' );
1096 $status->fatal( 'parentheses', '2' );
1097 $status->warning( 'parentheses', '3' );
1098 $status->warning( 'parentheses', '4' );
1099 $this->assertSame( array(
1102 'message' => 'parentheses',
1105 ApiResult
::META_INDEXED_TAG_NAME
=> 'param',
1110 'message' => 'parentheses',
1113 ApiResult
::META_INDEXED_TAG_NAME
=> 'param',
1116 ApiResult
::META_INDEXED_TAG_NAME
=> 'error',
1117 ), $result->convertStatusToArray( $status, 'error' ) );
1118 $this->assertSame( array(
1120 'type' => 'warning',
1121 'message' => 'parentheses',
1124 ApiResult
::META_INDEXED_TAG_NAME
=> 'param',
1128 'type' => 'warning',
1129 'message' => 'parentheses',
1132 ApiResult
::META_INDEXED_TAG_NAME
=> 'param',
1135 ApiResult
::META_INDEXED_TAG_NAME
=> 'warning',
1136 ), $result->convertStatusToArray( $status, 'warning' ) );
1142 public function testDeprecatedContinuation() {
1143 // Ignore ApiResult deprecation warnings during this test
1144 set_error_handler( function ( $errno, $errstr ) use ( &$warnings ) {
1145 if ( preg_match( '/Use of ApiResult::\S+ was deprecated in MediaWiki \d+.\d+\./', $errstr ) ) {
1151 $reset = new ScopedCallback( 'restore_error_handler' );
1152 $allModules = array(
1153 new MockApiQueryBase( 'mock1' ),
1154 new MockApiQueryBase( 'mock2' ),
1155 new MockApiQueryBase( 'mocklist' ),
1157 $generator = new MockApiQueryBase( 'generator' );
1159 $main = new ApiMain( RequestContext
::getMain() );
1160 $result = new ApiResult( 8388608 );
1161 $result->setMainForContinuation( $main );
1162 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1163 $this->assertSame( array( false, $allModules ), $ret );
1164 $result->setContinueParam( $allModules[0], 'm1continue', array( 1, 2 ) );
1165 $result->setContinueParam( $allModules[2], 'mlcontinue', 2 );
1166 $result->setGeneratorContinueParam( $generator, 'gcontinue', 3 );
1167 $result->endContinuation( 'raw' );
1168 $result->endContinuation( 'standard' );
1169 $this->assertSame( array(
1171 'm1continue' => '1|2',
1172 'continue' => '||mock2',
1173 ), $result->getResultData( 'continue' ) );
1174 $this->assertSame( null, $result->getResultData( 'batchcomplete' ) );
1175 $this->assertSame( array(
1176 'mock1' => array( 'm1continue' => '1|2' ),
1177 'mocklist' => array( 'mlcontinue' => 2 ),
1178 'generator' => array( 'gcontinue' => 3 ),
1179 ), $result->getResultData( 'query-continue' ) );
1180 $main->setContinuationManager( null );
1182 $result = new ApiResult( 8388608 );
1183 $result->setMainForContinuation( $main );
1184 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1185 $this->assertSame( array( false, $allModules ), $ret );
1186 $result->setContinueParam( $allModules[0], 'm1continue', array( 1, 2 ) );
1187 $result->setGeneratorContinueParam( $generator, 'gcontinue', array( 3, 4 ) );
1188 $result->endContinuation( 'raw' );
1189 $result->endContinuation( 'standard' );
1190 $this->assertSame( array(
1191 'm1continue' => '1|2',
1192 'continue' => '||mock2|mocklist',
1193 ), $result->getResultData( 'continue' ) );
1194 $this->assertSame( null, $result->getResultData( 'batchcomplete' ) );
1195 $this->assertSame( array(
1196 'mock1' => array( 'm1continue' => '1|2' ),
1197 'generator' => array( 'gcontinue' => '3|4' ),
1198 ), $result->getResultData( 'query-continue' ) );
1199 $main->setContinuationManager( null );
1201 $result = new ApiResult( 8388608 );
1202 $result->setMainForContinuation( $main );
1203 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1204 $this->assertSame( array( false, $allModules ), $ret );
1205 $result->setContinueParam( $allModules[2], 'mlcontinue', 2 );
1206 $result->setGeneratorContinueParam( $generator, 'gcontinue', 3 );
1207 $result->endContinuation( 'raw' );
1208 $result->endContinuation( 'standard' );
1209 $this->assertSame( array(
1212 'continue' => 'gcontinue||',
1213 ), $result->getResultData( 'continue' ) );
1214 $this->assertSame( '', $result->getResultData( 'batchcomplete' ) );
1215 $this->assertSame( array(
1216 'mocklist' => array( 'mlcontinue' => 2 ),
1217 'generator' => array( 'gcontinue' => 3 ),
1218 ), $result->getResultData( 'query-continue' ) );
1219 $main->setContinuationManager( null );
1221 $result = new ApiResult( 8388608 );
1222 $result->setMainForContinuation( $main );
1223 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1224 $this->assertSame( array( false, $allModules ), $ret );
1225 $result->setGeneratorContinueParam( $generator, 'gcontinue', 3 );
1226 $result->endContinuation( 'raw' );
1227 $result->endContinuation( 'standard' );
1228 $this->assertSame( array(
1230 'continue' => 'gcontinue||mocklist',
1231 ), $result->getResultData( 'continue' ) );
1232 $this->assertSame( '', $result->getResultData( 'batchcomplete' ) );
1233 $this->assertSame( array(
1234 'generator' => array( 'gcontinue' => 3 ),
1235 ), $result->getResultData( 'query-continue' ) );
1236 $main->setContinuationManager( null );
1238 $result = new ApiResult( 8388608 );
1239 $result->setMainForContinuation( $main );
1240 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1241 $this->assertSame( array( false, $allModules ), $ret );
1242 $result->setContinueParam( $allModules[0], 'm1continue', array( 1, 2 ) );
1243 $result->setContinueParam( $allModules[2], 'mlcontinue', 2 );
1244 $result->endContinuation( 'raw' );
1245 $result->endContinuation( 'standard' );
1246 $this->assertSame( array(
1248 'm1continue' => '1|2',
1249 'continue' => '||mock2',
1250 ), $result->getResultData( 'continue' ) );
1251 $this->assertSame( null, $result->getResultData( 'batchcomplete' ) );
1252 $this->assertSame( array(
1253 'mock1' => array( 'm1continue' => '1|2' ),
1254 'mocklist' => array( 'mlcontinue' => 2 ),
1255 ), $result->getResultData( 'query-continue' ) );
1256 $main->setContinuationManager( null );
1258 $result = new ApiResult( 8388608 );
1259 $result->setMainForContinuation( $main );
1260 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1261 $this->assertSame( array( false, $allModules ), $ret );
1262 $result->setContinueParam( $allModules[0], 'm1continue', array( 1, 2 ) );
1263 $result->endContinuation( 'raw' );
1264 $result->endContinuation( 'standard' );
1265 $this->assertSame( array(
1266 'm1continue' => '1|2',
1267 'continue' => '||mock2|mocklist',
1268 ), $result->getResultData( 'continue' ) );
1269 $this->assertSame( null, $result->getResultData( 'batchcomplete' ) );
1270 $this->assertSame( array(
1271 'mock1' => array( 'm1continue' => '1|2' ),
1272 ), $result->getResultData( 'query-continue' ) );
1273 $main->setContinuationManager( null );
1275 $result = new ApiResult( 8388608 );
1276 $result->setMainForContinuation( $main );
1277 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1278 $this->assertSame( array( false, $allModules ), $ret );
1279 $result->setContinueParam( $allModules[2], 'mlcontinue', 2 );
1280 $result->endContinuation( 'raw' );
1281 $result->endContinuation( 'standard' );
1282 $this->assertSame( array(
1284 'continue' => '-||mock1|mock2',
1285 ), $result->getResultData( 'continue' ) );
1286 $this->assertSame( '', $result->getResultData( 'batchcomplete' ) );
1287 $this->assertSame( array(
1288 'mocklist' => array( 'mlcontinue' => 2 ),
1289 ), $result->getResultData( 'query-continue' ) );
1290 $main->setContinuationManager( null );
1292 $result = new ApiResult( 8388608 );
1293 $result->setMainForContinuation( $main );
1294 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1295 $this->assertSame( array( false, $allModules ), $ret );
1296 $result->endContinuation( 'raw' );
1297 $result->endContinuation( 'standard' );
1298 $this->assertSame( null, $result->getResultData( 'continue' ) );
1299 $this->assertSame( '', $result->getResultData( 'batchcomplete' ) );
1300 $this->assertSame( null, $result->getResultData( 'query-continue' ) );
1301 $main->setContinuationManager( null );
1303 $result = new ApiResult( 8388608 );
1304 $result->setMainForContinuation( $main );
1305 $ret = $result->beginContinuation( '||mock2', $allModules, array( 'mock1', 'mock2' ) );
1307 array( false, array_values( array_diff_key( $allModules, array( 1 => 1 ) ) ) ),
1310 $main->setContinuationManager( null );
1312 $result = new ApiResult( 8388608 );
1313 $result->setMainForContinuation( $main );
1314 $ret = $result->beginContinuation( '-||', $allModules, array( 'mock1', 'mock2' ) );
1316 array( true, array_values( array_diff_key( $allModules, array( 0 => 0, 1 => 1 ) ) ) ),
1319 $main->setContinuationManager( null );
1321 $result = new ApiResult( 8388608 );
1322 $result->setMainForContinuation( $main );
1324 $result->beginContinuation( 'foo', $allModules, array( 'mock1', 'mock2' ) );
1325 $this->fail( 'Expected exception not thrown' );
1326 } catch ( UsageException
$ex ) {
1328 'Invalid continue param. You should pass the original value returned by the previous query',
1330 'Expected exception'
1333 $main->setContinuationManager( null );
1335 $result = new ApiResult( 8388608 );
1336 $result->setMainForContinuation( $main );
1337 $result->beginContinuation( '||mock2', array_slice( $allModules, 0, 2 ), array( 'mock1', 'mock2' ) );
1339 $result->setContinueParam( $allModules[1], 'm2continue', 1 );
1340 $this->fail( 'Expected exception not thrown' );
1341 } catch ( UnexpectedValueException
$ex ) {
1343 'Module \'mock2\' was not supposed to have been executed, but it was executed anyway',
1345 'Expected exception'
1349 $result->setContinueParam( $allModules[2], 'mlcontinue', 1 );
1350 $this->fail( 'Expected exception not thrown' );
1351 } catch ( UnexpectedValueException
$ex ) {
1353 'Module \'mocklist\' called ApiContinuationManager::addContinueParam but was not passed to ApiContinuationManager::__construct',
1355 'Expected exception'
1358 $main->setContinuationManager( null );
1362 public function testObjectSerialization() {
1364 ApiResult
::setValue( $arr, 'foo', (object)array( 'a' => 1, 'b' => 2 ) );
1365 $this->assertSame( array(
1368 ApiResult
::META_TYPE
=> 'assoc',
1372 ApiResult
::setValue( $arr, 'foo', new ApiResultTestStringifiableObject() );
1373 $this->assertSame( 'Ok', $arr['foo'] );
1376 ApiResult
::setValue( $arr, 'foo', new ApiResultTestSerializableObject( 'Ok' ) );
1377 $this->assertSame( 'Ok', $arr['foo'] );
1381 ApiResult
::setValue( $arr, 'foo', new ApiResultTestSerializableObject(
1382 new ApiResultTestStringifiableObject()
1384 $this->fail( 'Expected exception not thrown' );
1385 } catch ( UnexpectedValueException
$ex ) {
1387 'ApiResultTestSerializableObject::serializeForApiResult() returned an object of class ApiResultTestStringifiableObject',
1389 'Expected exception'
1395 ApiResult
::setValue( $arr, 'foo', new ApiResultTestSerializableObject( NAN
) );
1396 $this->fail( 'Expected exception not thrown' );
1397 } catch ( UnexpectedValueException
$ex ) {
1399 'ApiResultTestSerializableObject::serializeForApiResult() returned an invalid value: Cannot add non-finite floats to ApiResult',
1401 'Expected exception'
1406 ApiResult
::setValue( $arr, 'foo', new ApiResultTestSerializableObject(
1408 'one' => new ApiResultTestStringifiableObject( '1' ),
1409 'two' => new ApiResultTestSerializableObject( 2 ),
1412 $this->assertSame( array(
1420 class ApiResultTestStringifiableObject
{
1423 public function __construct( $ret = 'Ok' ) {
1427 public function __toString() {
1432 class ApiResultTestSerializableObject
{
1435 public function __construct( $ret ) {
1439 public function __toString() {
1443 public function serializeForApiResult() {