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(
149 ApiResult
::META_TYPE
=> 'assoc',
155 ApiResult
::setValue( $arr, 'foo', "foo\x80bar" );
156 ApiResult
::setValue( $arr, 'bar', "a\xcc\x81" );
157 ApiResult
::setValue( $arr, 'baz', 74 );
158 $this->assertSame( array(
159 'foo' => "foo\xef\xbf\xbdbar",
168 public function testInstanceDataMethods() {
169 $result = new ApiResult( 8388608 );
171 $result->addValue( null, 'setValue', '1' );
173 $result->addValue( null, null, 'unnamed 1' );
174 $result->addValue( null, null, 'unnamed 2' );
176 $result->addValue( null, 'deleteValue', '2' );
177 $result->removeValue( null, 'deleteValue' );
179 $result->addValue( array( 'a', 'b' ), 'deleteValue', '3' );
180 $result->removeValue( array( 'a', 'b', 'deleteValue' ), null, '3' );
182 $result->addContentValue( null, 'setContentValue', '3' );
184 $this->assertSame( array(
188 'a' => array( 'b' => array() ),
189 'setContentValue' => '3',
190 ApiResult
::META_TYPE
=> 'assoc',
191 ApiResult
::META_CONTENT
=> 'setContentValue',
192 ), $result->getResultData() );
193 $this->assertSame( 20, $result->getSize() );
196 $result->addValue( null, 'setValue', '99' );
197 $this->fail( 'Expected exception not thrown' );
198 } catch ( RuntimeException
$ex ) {
200 'Attempting to add element setValue=99, existing value is 1',
207 $result->addContentValue( null, 'setContentValue2', '99' );
208 $this->fail( 'Expected exception not thrown' );
209 } catch ( RuntimeException
$ex ) {
211 'Attempting to set content element as setContentValue2 when setContentValue ' .
212 'is already set as the content element',
218 $result->addValue( null, 'setValue', '99', ApiResult
::OVERRIDE
);
219 $this->assertSame( '99', $result->getResultData( array( 'setValue' ) ) );
221 $result->addContentValue( null, 'setContentValue2', '99', ApiResult
::OVERRIDE
);
222 $this->assertSame( 'setContentValue2',
223 $result->getResultData( array( ApiResult
::META_CONTENT
) ) );
226 $this->assertSame( array(
227 ApiResult
::META_TYPE
=> 'assoc',
228 ), $result->getResultData() );
229 $this->assertSame( 0, $result->getSize() );
231 $result->addValue( null, 'foo', 1 );
232 $result->addValue( null, 'bar', 1 );
233 $result->addValue( null, 'top', '2', ApiResult
::ADD_ON_TOP
);
234 $result->addValue( null, null, '2', ApiResult
::ADD_ON_TOP
);
235 $result->addValue( null, 'bottom', '2' );
236 $result->addValue( null, 'foo', '2', ApiResult
::OVERRIDE
);
237 $result->addValue( null, 'bar', '2', ApiResult
::OVERRIDE | ApiResult
::ADD_ON_TOP
);
238 $this->assertSame( array( 0, 'top', 'foo', 'bar', 'bottom', ApiResult
::META_TYPE
),
239 array_keys( $result->getResultData() ) );
242 $result->addValue( null, 'foo', array( 'bar' => 1 ) );
243 $result->addValue( array( 'foo', 'top' ), 'x', 2, ApiResult
::ADD_ON_TOP
);
244 $result->addValue( array( 'foo', 'bottom' ), 'x', 2 );
245 $this->assertSame( array( 'top', 'bar', 'bottom' ),
246 array_keys( $result->getResultData( array( 'foo' ) ) ) );
249 $result->addValue( null, 'sub', array( 'foo' => 1 ) );
250 $result->addValue( null, 'sub', array( 'bar' => 1 ) );
251 $this->assertSame( array(
252 'sub' => array( 'foo' => 1, 'bar' => 1 ),
253 ApiResult
::META_TYPE
=> 'assoc',
254 ), $result->getResultData() );
257 $result->addValue( null, 'sub', array( 'foo' => 2, 'baz' => 2 ) );
258 $this->fail( 'Expected exception not thrown' );
259 } catch ( RuntimeException
$ex ) {
261 'Conflicting keys (foo) when attempting to merge element sub',
268 $title = Title
::newFromText( "MediaWiki:Foobar" );
272 $result->addValue( null, 'title', $title );
273 $result->addValue( null, 'obj', $obj );
274 $this->assertSame( array(
275 'title' => (string)$title,
276 'obj' => array( 'foo' => 1, 'bar' => 2, ApiResult
::META_TYPE
=> 'assoc' ),
277 ApiResult
::META_TYPE
=> 'assoc',
278 ), $result->getResultData() );
282 $result->addValue( null, 'file', $fh );
283 $this->fail( 'Expected exception not thrown' );
284 } catch ( InvalidArgumentException
$ex ) {
286 'Cannot add resource(stream) to ApiResult',
293 $result->addValue( null, 'sub', $obj );
294 $this->fail( 'Expected exception not thrown' );
295 } catch ( InvalidArgumentException
$ex ) {
297 'Cannot add resource(stream) to ApiResult',
305 $result->addValue( null, 'inf', INF
);
306 $this->fail( 'Expected exception not thrown' );
307 } catch ( InvalidArgumentException
$ex ) {
309 'Cannot add non-finite floats to ApiResult',
315 $result->addValue( null, 'nan', NAN
);
316 $this->fail( 'Expected exception not thrown' );
317 } catch ( InvalidArgumentException
$ex ) {
319 'Cannot add non-finite floats to ApiResult',
326 $result->addParsedLimit( 'foo', 12 );
327 $this->assertSame( array(
328 'limits' => array( 'foo' => 12 ),
329 ApiResult
::META_TYPE
=> 'assoc',
330 ), $result->getResultData() );
331 $result->addParsedLimit( 'foo', 13 );
332 $this->assertSame( array(
333 'limits' => array( 'foo' => 13 ),
334 ApiResult
::META_TYPE
=> 'assoc',
335 ), $result->getResultData() );
336 $this->assertSame( null, $result->getResultData( array( 'foo', 'bar', 'baz' ) ) );
337 $this->assertSame( 13, $result->getResultData( array( 'limits', 'foo' ) ) );
339 $result->getResultData( array( 'limits', 'foo', 'bar' ) );
340 $this->fail( 'Expected exception not thrown' );
341 } catch ( InvalidArgumentException
$ex ) {
343 'Path limits.foo is not an array',
349 $result = new ApiResult( 10 );
350 $formatter = new ApiErrorFormatter( $result, Language
::factory( 'en' ), 'none', false );
351 $result->setErrorFormatter( $formatter );
352 $this->assertFalse( $result->addValue( null, 'foo', '12345678901' ) );
353 $this->assertTrue( $result->addValue( null, 'foo', '12345678901', ApiResult
::NO_SIZE_CHECK
) );
354 $this->assertSame( 0, $result->getSize() );
356 $this->assertTrue( $result->addValue( null, 'foo', '1234567890' ) );
357 $this->assertFalse( $result->addValue( null, 'foo', '1' ) );
358 $result->removeValue( null, 'foo' );
359 $this->assertTrue( $result->addValue( null, 'foo', '1' ) );
361 $result = new ApiResult( 8388608 );
362 $result2 = new ApiResult( 8388608 );
363 $result2->addValue( null, 'foo', 'bar' );
364 $result->addValue( null, 'baz', $result2 );
365 $this->assertSame( array(
368 ApiResult
::META_TYPE
=> 'assoc',
370 ApiResult
::META_TYPE
=> 'assoc',
371 ), $result->getResultData() );
373 $result = new ApiResult( 8388608 );
374 $result->addValue( null, 'foo', "foo\x80bar" );
375 $result->addValue( null, 'bar', "a\xcc\x81" );
376 $result->addValue( null, 'baz', 74 );
377 $this->assertSame( array(
378 'foo' => "foo\xef\xbf\xbdbar",
381 ApiResult
::META_TYPE
=> 'assoc',
382 ), $result->getResultData() );
388 public function testMetadata() {
389 $arr = array( 'foo' => array( 'bar' => array() ) );
390 $result = new ApiResult( 8388608 );
391 $result->addValue( null, 'foo', array( 'bar' => array() ) );
396 ApiResult
::META_INDEXED_TAG_NAME
=> 'ritn',
397 ApiResult
::META_TYPE
=> 'default',
399 ApiResult
::META_INDEXED_TAG_NAME
=> 'ritn',
400 ApiResult
::META_TYPE
=> 'default',
402 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
403 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
404 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar' ),
405 ApiResult
::META_TYPE
=> 'array',
408 ApiResult
::setSubelementsList( $arr, 'foo' );
409 ApiResult
::setSubelementsList( $arr, array( 'bar', 'baz' ) );
410 ApiResult
::unsetSubelementsList( $arr, 'baz' );
411 ApiResult
::setIndexedTagNameRecursive( $arr, 'ritn' );
412 ApiResult
::setIndexedTagName( $arr, 'itn' );
413 ApiResult
::setPreserveKeysList( $arr, 'foo' );
414 ApiResult
::setPreserveKeysList( $arr, array( 'bar', 'baz' ) );
415 ApiResult
::unsetPreserveKeysList( $arr, 'baz' );
416 ApiResult
::setArrayTypeRecursive( $arr, 'default' );
417 ApiResult
::setArrayType( $arr, 'array' );
418 $this->assertSame( $expect, $arr );
420 $result->addSubelementsList( null, 'foo' );
421 $result->addSubelementsList( null, array( 'bar', 'baz' ) );
422 $result->removeSubelementsList( null, 'baz' );
423 $result->addIndexedTagNameRecursive( null, 'ritn' );
424 $result->addIndexedTagName( null, 'itn' );
425 $result->addPreserveKeysList( null, 'foo' );
426 $result->addPreserveKeysList( null, array( 'bar', 'baz' ) );
427 $result->removePreserveKeysList( null, 'baz' );
428 $result->addArrayTypeRecursive( null, 'default' );
429 $result->addArrayType( null, 'array' );
430 $this->assertEquals( $expect, $result->getResultData() );
432 $arr = array( 'foo' => array( 'bar' => array() ) );
436 ApiResult
::META_TYPE
=> 'kvp',
437 ApiResult
::META_KVP_KEY_NAME
=> 'key',
439 ApiResult
::META_TYPE
=> 'kvp',
440 ApiResult
::META_KVP_KEY_NAME
=> 'key',
442 ApiResult
::META_TYPE
=> 'BCkvp',
443 ApiResult
::META_KVP_KEY_NAME
=> 'bc',
445 ApiResult
::setArrayTypeRecursive( $arr, 'kvp', 'key' );
446 ApiResult
::setArrayType( $arr, 'BCkvp', 'bc' );
447 $this->assertSame( $expect, $arr );
453 public function testUtilityFunctions() {
456 'bar' => array( '_dummy' => 'foobaz' ),
457 'bar2' => (object)array( '_dummy' => 'foobaz' ),
459 '_dummy' => 'foobaz',
461 'foo2' => (object)array(
462 'bar' => array( '_dummy' => 'foobaz' ),
463 'bar2' => (object)array( '_dummy' => 'foobaz' ),
465 '_dummy' => 'foobaz',
467 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
468 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
469 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar', '_dummy2', 0 ),
470 ApiResult
::META_TYPE
=> 'array',
471 '_dummy' => 'foobaz',
472 '_dummy2' => 'foobaz!',
474 $this->assertEquals( array(
477 'bar2' => (object)array(),
480 'foo2' => (object)array(
482 'bar2' => (object)array(),
485 '_dummy2' => 'foobaz!',
486 ), ApiResult
::stripMetadata( $arr ), 'ApiResult::stripMetadata' );
489 $data = ApiResult
::stripMetadataNonRecursive( $arr, $metadata );
490 $this->assertEquals( array(
492 'bar' => array( '_dummy' => 'foobaz' ),
493 'bar2' => (object)array( '_dummy' => 'foobaz' ),
495 '_dummy' => 'foobaz',
497 'foo2' => (object)array(
498 'bar' => array( '_dummy' => 'foobaz' ),
499 'bar2' => (object)array( '_dummy' => 'foobaz' ),
501 '_dummy' => 'foobaz',
503 '_dummy2' => 'foobaz!',
504 ), $data, 'ApiResult::stripMetadataNonRecursive ($data)' );
505 $this->assertEquals( array(
506 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
507 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
508 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar', '_dummy2', 0 ),
509 ApiResult
::META_TYPE
=> 'array',
510 '_dummy' => 'foobaz',
511 ), $metadata, 'ApiResult::stripMetadataNonRecursive ($metadata)' );
514 $data = ApiResult
::stripMetadataNonRecursive( (object)$arr, $metadata );
515 $this->assertEquals( (object)array(
517 'bar' => array( '_dummy' => 'foobaz' ),
518 'bar2' => (object)array( '_dummy' => 'foobaz' ),
520 '_dummy' => 'foobaz',
522 'foo2' => (object)array(
523 'bar' => array( '_dummy' => 'foobaz' ),
524 'bar2' => (object)array( '_dummy' => 'foobaz' ),
526 '_dummy' => 'foobaz',
528 '_dummy2' => 'foobaz!',
529 ), $data, 'ApiResult::stripMetadataNonRecursive on object ($data)' );
530 $this->assertEquals( array(
531 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
532 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
533 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar', '_dummy2', 0 ),
534 ApiResult
::META_TYPE
=> 'array',
535 '_dummy' => 'foobaz',
536 ), $metadata, 'ApiResult::stripMetadataNonRecursive on object ($metadata)' );
541 * @dataProvider provideTransformations
542 * @param string $label
543 * @param array $input
544 * @param array $transforms
545 * @param array|Exception $expect
547 public function testTransformations( $label, $input, $transforms, $expect ) {
548 $result = new ApiResult( false );
549 $result->addValue( null, 'test', $input );
551 if ( $expect instanceof Exception
) {
553 $output = $result->getResultData( 'test', $transforms );
554 $this->fail( 'Expected exception not thrown', $label );
555 } catch ( Exception
$ex ) {
556 $this->assertEquals( $ex, $expect, $label );
559 $output = $result->getResultData( 'test', $transforms );
560 $this->assertEquals( $expect, $output, $label );
564 public function provideTransformations() {
565 $kvp = function ( $keyKey, $key, $valKey, $value ) {
569 ApiResult
::META_PRESERVE_KEYS
=> array( $keyKey ),
570 ApiResult
::META_CONTENT
=> $valKey,
571 ApiResult
::META_TYPE
=> 'assoc',
575 'defaultArray' => array( 2 => 'a', 0 => 'b', 1 => 'c' ),
576 'defaultAssoc' => array( 'x' => 'a', 1 => 'b', 0 => 'c' ),
577 'defaultAssoc2' => array( 2 => 'a', 3 => 'b', 0 => 'c' ),
578 'array' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'array' ),
579 'BCarray' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'BCarray' ),
580 'BCassoc' => array( 'a', 'b', 'c', ApiResult
::META_TYPE
=> 'BCassoc' ),
581 'assoc' => array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
582 'kvp' => array( 'x' => 'a', 'y' => 'b', 'z' => array( 'c' ), ApiResult
::META_TYPE
=> 'kvp' ),
583 'BCkvp' => array( 'x' => 'a', 'y' => 'b',
584 ApiResult
::META_TYPE
=> 'BCkvp',
585 ApiResult
::META_KVP_KEY_NAME
=> 'key',
587 'emptyDefault' => array( '_dummy' => 1 ),
588 'emptyAssoc' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'assoc' ),
590 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy' ),
594 'bar' => array( '_dummy' => 'foobaz' ),
596 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
597 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
598 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar', '_dummy2', 0 ),
599 ApiResult
::META_TYPE
=> 'array',
602 '_dummy' => 'foobaz',
604 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
605 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
606 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar', '_dummy2', 0 ),
607 ApiResult
::META_TYPE
=> 'array',
608 '_dummy' => 'foobaz',
609 '_dummy2' => 'foobaz!',
620 ApiResult
::META_BC_BOOLS
=> array( 0, 'true', 'false' ),
622 array( 'BC' => array() ),
627 ApiResult
::META_BC_BOOLS
=> array( 0, 'true', 'false' ),
631 'BC: META_BC_SUBELEMENTS',
635 ApiResult
::META_BC_SUBELEMENTS
=> array( 'bc' ),
637 array( 'BC' => array() ),
641 ApiResult
::META_CONTENT
=> '*',
642 ApiResult
::META_TYPE
=> 'assoc',
645 ApiResult
::META_BC_SUBELEMENTS
=> array( 'bc' ),
652 ApiResult
::META_CONTENT
=> 'content',
654 array( 'BC' => array() ),
657 ApiResult
::META_CONTENT
=> '*',
663 'foo' => 'foo value',
664 'bar' => 'bar value',
665 '_baz' => 'baz value',
666 ApiResult
::META_TYPE
=> 'BCkvp',
667 ApiResult
::META_KVP_KEY_NAME
=> 'key',
668 ApiResult
::META_PRESERVE_KEYS
=> array( '_baz' ),
670 array( 'BC' => array() ),
672 $kvp( 'key', 'foo', '*', 'foo value' ),
673 $kvp( 'key', 'bar', '*', 'bar value' ),
674 $kvp( 'key', '_baz', '*', 'baz value' ),
675 ApiResult
::META_TYPE
=> 'array',
676 ApiResult
::META_KVP_KEY_NAME
=> 'key',
677 ApiResult
::META_PRESERVE_KEYS
=> array( '_baz' ),
683 ApiResult
::META_TYPE
=> 'BCarray',
685 array( 'BC' => array() ),
687 ApiResult
::META_TYPE
=> 'default',
693 ApiResult
::META_TYPE
=> 'BCassoc',
695 array( 'BC' => array() ),
697 ApiResult
::META_TYPE
=> 'default',
701 'BC: BCkvp exception',
703 ApiResult
::META_TYPE
=> 'BCkvp',
705 array( 'BC' => array() ),
706 new UnexpectedValueException(
707 'Type "BCkvp" used without setting ApiResult::META_KVP_KEY_NAME metadata item'
711 'BC: nobool, no*, nosub',
715 'content' => 'content',
716 ApiResult
::META_CONTENT
=> 'content',
718 ApiResult
::META_BC_SUBELEMENTS
=> array( 'bc' ),
719 'BCarray' => array( ApiResult
::META_TYPE
=> 'BCarray' ),
720 'BCassoc' => array( ApiResult
::META_TYPE
=> 'BCassoc' ),
722 'foo' => 'foo value',
723 'bar' => 'bar value',
724 '_baz' => 'baz value',
725 ApiResult
::META_TYPE
=> 'BCkvp',
726 ApiResult
::META_KVP_KEY_NAME
=> 'key',
727 ApiResult
::META_PRESERVE_KEYS
=> array( '_baz' ),
730 array( 'BC' => array( 'nobool', 'no*', 'nosub' ) ),
734 'content' => 'content',
736 'BCarray' => array( ApiResult
::META_TYPE
=> 'default' ),
737 'BCassoc' => array( ApiResult
::META_TYPE
=> 'default' ),
739 $kvp( 'key', 'foo', '*', 'foo value' ),
740 $kvp( 'key', 'bar', '*', 'bar value' ),
741 $kvp( 'key', '_baz', '*', 'baz value' ),
742 ApiResult
::META_TYPE
=> 'array',
743 ApiResult
::META_KVP_KEY_NAME
=> 'key',
744 ApiResult
::META_PRESERVE_KEYS
=> array( '_baz' ),
746 ApiResult
::META_CONTENT
=> 'content',
747 ApiResult
::META_BC_SUBELEMENTS
=> array( 'bc' ),
752 'Types: Normal transform',
754 array( 'Types' => array() ),
756 'defaultArray' => array( 'b', 'c', 'a', ApiResult
::META_TYPE
=> 'array' ),
757 'defaultAssoc' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
758 'defaultAssoc2' => array( 2 => 'a', 3 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
759 'array' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
760 'BCarray' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
761 'BCassoc' => array( 'a', 'b', 'c', ApiResult
::META_TYPE
=> 'assoc' ),
762 'assoc' => array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
763 'kvp' => array( 'x' => 'a', 'y' => 'b',
764 'z' => array( 'c', ApiResult
::META_TYPE
=> 'array' ),
765 ApiResult
::META_TYPE
=> 'assoc'
767 'BCkvp' => array( 'x' => 'a', 'y' => 'b',
768 ApiResult
::META_TYPE
=> 'assoc',
769 ApiResult
::META_KVP_KEY_NAME
=> 'key',
771 'emptyDefault' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'array' ),
772 'emptyAssoc' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'assoc' ),
774 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy' ),
775 ApiResult
::META_TYPE
=> 'assoc',
779 'Types: AssocAsObject',
781 array( 'Types' => array( 'AssocAsObject' => true ) ),
783 'defaultArray' => array( 'b', 'c', 'a', ApiResult
::META_TYPE
=> 'array' ),
784 'defaultAssoc' => (object)array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
785 'defaultAssoc2' => (object)array( 2 => 'a', 3 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
786 'array' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
787 'BCarray' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
788 'BCassoc' => (object)array( 'a', 'b', 'c', ApiResult
::META_TYPE
=> 'assoc' ),
789 'assoc' => (object)array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
790 'kvp' => (object)array( 'x' => 'a', 'y' => 'b',
791 'z' => array( 'c', ApiResult
::META_TYPE
=> 'array' ),
792 ApiResult
::META_TYPE
=> 'assoc'
794 'BCkvp' => (object)array( 'x' => 'a', 'y' => 'b',
795 ApiResult
::META_TYPE
=> 'assoc',
796 ApiResult
::META_KVP_KEY_NAME
=> 'key',
798 'emptyDefault' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'array' ),
799 'emptyAssoc' => (object)array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'assoc' ),
801 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy' ),
802 ApiResult
::META_TYPE
=> 'assoc',
808 array( 'Types' => array( 'ArmorKVP' => 'name' ) ),
810 'defaultArray' => array( 'b', 'c', 'a', ApiResult
::META_TYPE
=> 'array' ),
811 'defaultAssoc' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
812 'defaultAssoc2' => array( 2 => 'a', 3 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
813 'array' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
814 'BCarray' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
815 'BCassoc' => array( 'a', 'b', 'c', ApiResult
::META_TYPE
=> 'assoc' ),
816 'assoc' => array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
818 $kvp( 'name', 'x', 'value', 'a' ),
819 $kvp( 'name', 'y', 'value', 'b' ),
820 $kvp( 'name', 'z', 'value', array( 'c', ApiResult
::META_TYPE
=> 'array' ) ),
821 ApiResult
::META_TYPE
=> 'array'
824 $kvp( 'key', 'x', 'value', 'a' ),
825 $kvp( 'key', 'y', 'value', 'b' ),
826 ApiResult
::META_TYPE
=> 'array',
827 ApiResult
::META_KVP_KEY_NAME
=> 'key',
829 'emptyDefault' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'array' ),
830 'emptyAssoc' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'assoc' ),
832 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy' ),
833 ApiResult
::META_TYPE
=> 'assoc',
837 'Types: ArmorKVP + BC',
839 array( 'BC' => array(), 'Types' => array( 'ArmorKVP' => 'name' ) ),
841 'defaultArray' => array( 'b', 'c', 'a', ApiResult
::META_TYPE
=> 'array' ),
842 'defaultAssoc' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
843 'defaultAssoc2' => array( 2 => 'a', 3 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
844 'array' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
845 'BCarray' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
846 'BCassoc' => array( 'a', 'b', 'c', ApiResult
::META_TYPE
=> 'array' ),
847 'assoc' => array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
849 $kvp( 'name', 'x', '*', 'a' ),
850 $kvp( 'name', 'y', '*', 'b' ),
851 $kvp( 'name', 'z', '*', array( 'c', ApiResult
::META_TYPE
=> 'array' ) ),
852 ApiResult
::META_TYPE
=> 'array'
855 $kvp( 'key', 'x', '*', 'a' ),
856 $kvp( 'key', 'y', '*', 'b' ),
857 ApiResult
::META_TYPE
=> 'array',
858 ApiResult
::META_KVP_KEY_NAME
=> 'key',
860 'emptyDefault' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'array' ),
861 'emptyAssoc' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'assoc' ),
863 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy' ),
864 ApiResult
::META_TYPE
=> 'assoc',
868 'Types: ArmorKVP + AssocAsObject',
870 array( 'Types' => array( 'ArmorKVP' => 'name', 'AssocAsObject' => true ) ),
872 'defaultArray' => array( 'b', 'c', 'a', ApiResult
::META_TYPE
=> 'array' ),
873 'defaultAssoc' => (object)array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
874 'defaultAssoc2' => (object)array( 2 => 'a', 3 => 'b', 0 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
875 'array' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
876 'BCarray' => array( 'a', 'c', 'b', ApiResult
::META_TYPE
=> 'array' ),
877 'BCassoc' => (object)array( 'a', 'b', 'c', ApiResult
::META_TYPE
=> 'assoc' ),
878 'assoc' => (object)array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult
::META_TYPE
=> 'assoc' ),
880 (object)$kvp( 'name', 'x', 'value', 'a' ),
881 (object)$kvp( 'name', 'y', 'value', 'b' ),
882 (object)$kvp( 'name', 'z', 'value', array( 'c', ApiResult
::META_TYPE
=> 'array' ) ),
883 ApiResult
::META_TYPE
=> 'array'
886 (object)$kvp( 'key', 'x', 'value', 'a' ),
887 (object)$kvp( 'key', 'y', 'value', 'b' ),
888 ApiResult
::META_TYPE
=> 'array',
889 ApiResult
::META_KVP_KEY_NAME
=> 'key',
891 'emptyDefault' => array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'array' ),
892 'emptyAssoc' => (object)array( '_dummy' => 1, ApiResult
::META_TYPE
=> 'assoc' ),
894 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy' ),
895 ApiResult
::META_TYPE
=> 'assoc',
899 'Types: BCkvp exception',
901 ApiResult
::META_TYPE
=> 'BCkvp',
903 array( 'Types' => array() ),
904 new UnexpectedValueException(
905 'Type "BCkvp" used without setting ApiResult::META_KVP_KEY_NAME metadata item'
910 'Strip: With ArmorKVP + AssocAsObject transforms',
912 array( 'Types' => array( 'ArmorKVP' => 'name', 'AssocAsObject' => true ), 'Strip' => 'all' ),
914 'defaultArray' => array( 'b', 'c', 'a' ),
915 'defaultAssoc' => (object)array( 'x' => 'a', 1 => 'b', 0 => 'c' ),
916 'defaultAssoc2' => (object)array( 2 => 'a', 3 => 'b', 0 => 'c' ),
917 'array' => array( 'a', 'c', 'b' ),
918 'BCarray' => array( 'a', 'c', 'b' ),
919 'BCassoc' => (object)array( 'a', 'b', 'c' ),
920 'assoc' => (object)array( 2 => 'a', 0 => 'b', 1 => 'c' ),
922 (object)array( 'name' => 'x', 'value' => 'a' ),
923 (object)array( 'name' => 'y', 'value' => 'b' ),
924 (object)array( 'name' => 'z', 'value' => array( 'c' ) ),
927 (object)array( 'key' => 'x', 'value' => 'a' ),
928 (object)array( 'key' => 'y', 'value' => 'b' ),
930 'emptyDefault' => array(),
931 'emptyAssoc' => (object)array(),
939 array( 'Strip' => 'all' ),
946 '_dummy2' => 'foobaz!',
952 array( 'Strip' => 'base' ),
955 'bar' => array( '_dummy' => 'foobaz' ),
957 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
958 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
959 ApiResult
::META_PRESERVE_KEYS
=> array( 'foo', 'bar', '_dummy2', 0 ),
960 ApiResult
::META_TYPE
=> 'array',
963 '_dummy' => 'foobaz',
965 '_dummy2' => 'foobaz!',
971 array( 'Strip' => 'bc' ),
976 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
977 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
981 '_dummy2' => 'foobaz!',
982 ApiResult
::META_SUBELEMENTS
=> array( 'foo', 'bar' ),
983 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn',
995 ApiResult
::META_CONTENT
=> 'foo',
996 ApiResult
::META_PRESERVE_KEYS
=> array( '_dummy2', '_dummy3' ),
999 'Custom' => array( $this, 'customTransform' ),
1007 'baz' => array( 'a', 'b' ),
1008 '_dummy2' => '_DUMMY2',
1009 '_dummy3' => '_DUMMY3',
1010 ApiResult
::META_CONTENT
=> 'bar',
1018 * Custom transformer for testTransformations
1019 * @param array &$data
1020 * @param array &$metadata
1022 public function customTransform( &$data, &$metadata ) {
1023 // Prevent recursion
1024 if ( isset( $metadata['_added'] ) ) {
1025 $metadata[ApiResult
::META_TYPE
] = 'array';
1029 foreach ( $data as $k => $v ) {
1030 $data[$k] = strtoupper( $k );
1032 $data['baz'] = array( '_added' => 1, 'z' => 'b', 'y' => 'a' );
1033 $metadata[ApiResult
::META_PRESERVE_KEYS
][0] = '_dummy';
1034 $data[ApiResult
::META_CONTENT
] = 'bar';
1040 public function testDeprecatedFunctions() {
1041 // Ignore ApiResult deprecation warnings during this test
1042 set_error_handler( function ( $errno, $errstr ) use ( &$warnings ) {
1043 if ( preg_match( '/Use of ApiResult::\S+ was deprecated in MediaWiki \d+.\d+\./', $errstr ) ) {
1048 $reset = new ScopedCallback( 'restore_error_handler' );
1050 $context = new DerivativeContext( RequestContext
::getMain() );
1051 $context->setConfig( new HashConfig( array(
1052 'APIModules' => array(),
1053 'APIFormatModules' => array(),
1054 'APIMaxResultSize' => 42,
1056 $main = new ApiMain( $context );
1057 $result = TestingAccessWrapper
::newFromObject( new ApiResult( $main ) );
1058 $this->assertSame( 42, $result->maxSize
);
1059 $this->assertSame( $main->getErrorFormatter(), $result->errorFormatter
);
1060 $this->assertSame( $main, $result->mainForContinuation
);
1062 $result = new ApiResult( 8388608 );
1064 $result->addContentValue( null, 'test', 'content' );
1065 $result->addContentValue( array( 'foo', 'bar' ), 'test', 'content' );
1066 $result->addIndexedTagName( null, 'itn' );
1067 $result->addSubelementsList( null, array( 'sub' ) );
1068 $this->assertSame( array(
1075 ), $result->getData() );
1076 $result->setRawMode();
1077 $this->assertSame( array(
1084 '_element' => 'itn',
1085 '_subelements' => array( 'sub' ),
1086 ), $result->getData() );
1089 ApiResult
::setContent( $arr, 'value' );
1090 ApiResult
::setContent( $arr, 'value2', 'foobar' );
1091 $this->assertSame( array(
1092 ApiResult
::META_CONTENT
=> 'content',
1093 'content' => 'value',
1095 ApiResult
::META_CONTENT
=> 'content',
1096 'content' => 'value2',
1100 $result = new ApiResult( 3 );
1101 $formatter = new ApiErrorFormatter_BackCompat( $result );
1102 $result->setErrorFormatter( $formatter );
1103 $result->disableSizeCheck();
1104 $this->assertTrue( $result->addValue( null, 'foo', '1234567890' ) );
1105 $result->enableSizeCheck();
1106 $this->assertSame( 0, $result->getSize() );
1107 $this->assertFalse( $result->addValue( null, 'foo', '1234567890' ) );
1109 $arr = array( 'foo' => array( 'bar' => 1 ) );
1110 $result->setIndexedTagName_recursive( $arr, 'itn' );
1111 $this->assertSame( array(
1114 ApiResult
::META_INDEXED_TAG_NAME
=> 'itn'
1118 $status = Status
::newGood();
1119 $status->fatal( 'parentheses', '1' );
1120 $status->fatal( 'parentheses', '2' );
1121 $status->warning( 'parentheses', '3' );
1122 $status->warning( 'parentheses', '4' );
1123 $this->assertSame( array(
1126 'message' => 'parentheses',
1129 ApiResult
::META_INDEXED_TAG_NAME
=> 'param',
1134 'message' => 'parentheses',
1137 ApiResult
::META_INDEXED_TAG_NAME
=> 'param',
1140 ApiResult
::META_INDEXED_TAG_NAME
=> 'error',
1141 ), $result->convertStatusToArray( $status, 'error' ) );
1142 $this->assertSame( array(
1144 'type' => 'warning',
1145 'message' => 'parentheses',
1148 ApiResult
::META_INDEXED_TAG_NAME
=> 'param',
1152 'type' => 'warning',
1153 'message' => 'parentheses',
1156 ApiResult
::META_INDEXED_TAG_NAME
=> 'param',
1159 ApiResult
::META_INDEXED_TAG_NAME
=> 'warning',
1160 ), $result->convertStatusToArray( $status, 'warning' ) );
1166 public function testDeprecatedContinuation() {
1167 // Ignore ApiResult deprecation warnings during this test
1168 set_error_handler( function ( $errno, $errstr ) use ( &$warnings ) {
1169 if ( preg_match( '/Use of ApiResult::\S+ was deprecated in MediaWiki \d+.\d+\./', $errstr ) ) {
1175 $reset = new ScopedCallback( 'restore_error_handler' );
1176 $allModules = array(
1177 new MockApiQueryBase( 'mock1' ),
1178 new MockApiQueryBase( 'mock2' ),
1179 new MockApiQueryBase( 'mocklist' ),
1181 $generator = new MockApiQueryBase( 'generator' );
1183 $main = new ApiMain( RequestContext
::getMain() );
1184 $result = new ApiResult( 8388608 );
1185 $result->setMainForContinuation( $main );
1186 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1187 $this->assertSame( array( false, $allModules ), $ret );
1188 $result->setContinueParam( $allModules[0], 'm1continue', array( 1, 2 ) );
1189 $result->setContinueParam( $allModules[2], 'mlcontinue', 2 );
1190 $result->setGeneratorContinueParam( $generator, 'gcontinue', 3 );
1191 $result->endContinuation( 'raw' );
1192 $result->endContinuation( 'standard' );
1193 $this->assertSame( array(
1195 'm1continue' => '1|2',
1196 'continue' => '||mock2',
1197 ), $result->getResultData( 'continue' ) );
1198 $this->assertSame( null, $result->getResultData( 'batchcomplete' ) );
1199 $this->assertSame( array(
1200 'mock1' => array( 'm1continue' => '1|2' ),
1201 'mocklist' => array( 'mlcontinue' => 2 ),
1202 'generator' => array( 'gcontinue' => 3 ),
1203 ), $result->getResultData( 'query-continue' ) );
1204 $main->setContinuationManager( null );
1206 $result = new ApiResult( 8388608 );
1207 $result->setMainForContinuation( $main );
1208 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1209 $this->assertSame( array( false, $allModules ), $ret );
1210 $result->setContinueParam( $allModules[0], 'm1continue', array( 1, 2 ) );
1211 $result->setGeneratorContinueParam( $generator, 'gcontinue', array( 3, 4 ) );
1212 $result->endContinuation( 'raw' );
1213 $result->endContinuation( 'standard' );
1214 $this->assertSame( array(
1215 'm1continue' => '1|2',
1216 'continue' => '||mock2|mocklist',
1217 ), $result->getResultData( 'continue' ) );
1218 $this->assertSame( null, $result->getResultData( 'batchcomplete' ) );
1219 $this->assertSame( array(
1220 'mock1' => array( 'm1continue' => '1|2' ),
1221 'generator' => array( 'gcontinue' => '3|4' ),
1222 ), $result->getResultData( 'query-continue' ) );
1223 $main->setContinuationManager( null );
1225 $result = new ApiResult( 8388608 );
1226 $result->setMainForContinuation( $main );
1227 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1228 $this->assertSame( array( false, $allModules ), $ret );
1229 $result->setContinueParam( $allModules[2], 'mlcontinue', 2 );
1230 $result->setGeneratorContinueParam( $generator, 'gcontinue', 3 );
1231 $result->endContinuation( 'raw' );
1232 $result->endContinuation( 'standard' );
1233 $this->assertSame( array(
1236 'continue' => 'gcontinue||',
1237 ), $result->getResultData( 'continue' ) );
1238 $this->assertSame( true, $result->getResultData( 'batchcomplete' ) );
1239 $this->assertSame( array(
1240 'mocklist' => array( 'mlcontinue' => 2 ),
1241 'generator' => array( 'gcontinue' => 3 ),
1242 ), $result->getResultData( 'query-continue' ) );
1243 $main->setContinuationManager( null );
1245 $result = new ApiResult( 8388608 );
1246 $result->setMainForContinuation( $main );
1247 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1248 $this->assertSame( array( false, $allModules ), $ret );
1249 $result->setGeneratorContinueParam( $generator, 'gcontinue', 3 );
1250 $result->endContinuation( 'raw' );
1251 $result->endContinuation( 'standard' );
1252 $this->assertSame( array(
1254 'continue' => 'gcontinue||mocklist',
1255 ), $result->getResultData( 'continue' ) );
1256 $this->assertSame( true, $result->getResultData( 'batchcomplete' ) );
1257 $this->assertSame( array(
1258 'generator' => array( 'gcontinue' => 3 ),
1259 ), $result->getResultData( 'query-continue' ) );
1260 $main->setContinuationManager( null );
1262 $result = new ApiResult( 8388608 );
1263 $result->setMainForContinuation( $main );
1264 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1265 $this->assertSame( array( false, $allModules ), $ret );
1266 $result->setContinueParam( $allModules[0], 'm1continue', array( 1, 2 ) );
1267 $result->setContinueParam( $allModules[2], 'mlcontinue', 2 );
1268 $result->endContinuation( 'raw' );
1269 $result->endContinuation( 'standard' );
1270 $this->assertSame( array(
1272 'm1continue' => '1|2',
1273 'continue' => '||mock2',
1274 ), $result->getResultData( 'continue' ) );
1275 $this->assertSame( null, $result->getResultData( 'batchcomplete' ) );
1276 $this->assertSame( array(
1277 'mock1' => array( 'm1continue' => '1|2' ),
1278 'mocklist' => array( 'mlcontinue' => 2 ),
1279 ), $result->getResultData( 'query-continue' ) );
1280 $main->setContinuationManager( null );
1282 $result = new ApiResult( 8388608 );
1283 $result->setMainForContinuation( $main );
1284 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1285 $this->assertSame( array( false, $allModules ), $ret );
1286 $result->setContinueParam( $allModules[0], 'm1continue', array( 1, 2 ) );
1287 $result->endContinuation( 'raw' );
1288 $result->endContinuation( 'standard' );
1289 $this->assertSame( array(
1290 'm1continue' => '1|2',
1291 'continue' => '||mock2|mocklist',
1292 ), $result->getResultData( 'continue' ) );
1293 $this->assertSame( null, $result->getResultData( 'batchcomplete' ) );
1294 $this->assertSame( array(
1295 'mock1' => array( 'm1continue' => '1|2' ),
1296 ), $result->getResultData( 'query-continue' ) );
1297 $main->setContinuationManager( null );
1299 $result = new ApiResult( 8388608 );
1300 $result->setMainForContinuation( $main );
1301 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1302 $this->assertSame( array( false, $allModules ), $ret );
1303 $result->setContinueParam( $allModules[2], 'mlcontinue', 2 );
1304 $result->endContinuation( 'raw' );
1305 $result->endContinuation( 'standard' );
1306 $this->assertSame( array(
1308 'continue' => '-||mock1|mock2',
1309 ), $result->getResultData( 'continue' ) );
1310 $this->assertSame( true, $result->getResultData( 'batchcomplete' ) );
1311 $this->assertSame( array(
1312 'mocklist' => array( 'mlcontinue' => 2 ),
1313 ), $result->getResultData( 'query-continue' ) );
1314 $main->setContinuationManager( null );
1316 $result = new ApiResult( 8388608 );
1317 $result->setMainForContinuation( $main );
1318 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1319 $this->assertSame( array( false, $allModules ), $ret );
1320 $result->endContinuation( 'raw' );
1321 $result->endContinuation( 'standard' );
1322 $this->assertSame( null, $result->getResultData( 'continue' ) );
1323 $this->assertSame( true, $result->getResultData( 'batchcomplete' ) );
1324 $this->assertSame( null, $result->getResultData( 'query-continue' ) );
1325 $main->setContinuationManager( null );
1327 $result = new ApiResult( 8388608 );
1328 $result->setMainForContinuation( $main );
1329 $ret = $result->beginContinuation( '||mock2', $allModules, array( 'mock1', 'mock2' ) );
1331 array( false, array_values( array_diff_key( $allModules, array( 1 => 1 ) ) ) ),
1334 $main->setContinuationManager( null );
1336 $result = new ApiResult( 8388608 );
1337 $result->setMainForContinuation( $main );
1338 $ret = $result->beginContinuation( '-||', $allModules, array( 'mock1', 'mock2' ) );
1340 array( true, array_values( array_diff_key( $allModules, array( 0 => 0, 1 => 1 ) ) ) ),
1343 $main->setContinuationManager( null );
1345 $result = new ApiResult( 8388608 );
1346 $result->setMainForContinuation( $main );
1348 $result->beginContinuation( 'foo', $allModules, array( 'mock1', 'mock2' ) );
1349 $this->fail( 'Expected exception not thrown' );
1350 } catch ( UsageException
$ex ) {
1352 'Invalid continue param. You should pass the original value returned by the previous query',
1354 'Expected exception'
1357 $main->setContinuationManager( null );
1359 $result = new ApiResult( 8388608 );
1360 $result->setMainForContinuation( $main );
1361 $result->beginContinuation( '||mock2', array_slice( $allModules, 0, 2 ), array( 'mock1', 'mock2' ) );
1363 $result->setContinueParam( $allModules[1], 'm2continue', 1 );
1364 $this->fail( 'Expected exception not thrown' );
1365 } catch ( UnexpectedValueException
$ex ) {
1367 'Module \'mock2\' was not supposed to have been executed, but it was executed anyway',
1369 'Expected exception'
1373 $result->setContinueParam( $allModules[2], 'mlcontinue', 1 );
1374 $this->fail( 'Expected exception not thrown' );
1375 } catch ( UnexpectedValueException
$ex ) {
1377 'Module \'mocklist\' called ApiContinuationManager::addContinueParam but was not passed to ApiContinuationManager::__construct',
1379 'Expected exception'
1382 $main->setContinuationManager( null );
1386 public function testObjectSerialization() {
1388 ApiResult
::setValue( $arr, 'foo', (object)array( 'a' => 1, 'b' => 2 ) );
1389 $this->assertSame( array(
1392 ApiResult
::META_TYPE
=> 'assoc',
1396 ApiResult
::setValue( $arr, 'foo', new ApiResultTestStringifiableObject() );
1397 $this->assertSame( 'Ok', $arr['foo'] );
1400 ApiResult
::setValue( $arr, 'foo', new ApiResultTestSerializableObject( 'Ok' ) );
1401 $this->assertSame( 'Ok', $arr['foo'] );
1405 ApiResult
::setValue( $arr, 'foo', new ApiResultTestSerializableObject(
1406 new ApiResultTestStringifiableObject()
1408 $this->fail( 'Expected exception not thrown' );
1409 } catch ( UnexpectedValueException
$ex ) {
1411 'ApiResultTestSerializableObject::serializeForApiResult() returned an object of class ApiResultTestStringifiableObject',
1413 'Expected exception'
1419 ApiResult
::setValue( $arr, 'foo', new ApiResultTestSerializableObject( NAN
) );
1420 $this->fail( 'Expected exception not thrown' );
1421 } catch ( UnexpectedValueException
$ex ) {
1423 'ApiResultTestSerializableObject::serializeForApiResult() returned an invalid value: Cannot add non-finite floats to ApiResult',
1425 'Expected exception'
1430 ApiResult
::setValue( $arr, 'foo', new ApiResultTestSerializableObject(
1432 'one' => new ApiResultTestStringifiableObject( '1' ),
1433 'two' => new ApiResultTestSerializableObject( 2 ),
1436 $this->assertSame( array(
1444 class ApiResultTestStringifiableObject
{
1447 public function __construct( $ret = 'Ok' ) {
1451 public function __toString() {
1456 class ApiResultTestSerializableObject
{
1459 public function __construct( $ret ) {
1463 public function __toString() {
1467 public function serializeForApiResult() {