Merge "Rename ObjectFactoryTest_Fixture to camel case name"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiResultTest.php
1 <?php
2
3 /**
4 * @covers ApiResult
5 * @group API
6 */
7 class ApiResultTest extends MediaWikiTestCase {
8
9 /**
10 * @covers ApiResult
11 */
12 public function testStaticDataMethods() {
13 $arr = array();
14
15 ApiResult::setValue( $arr, 'setValue', '1' );
16
17 ApiResult::setValue( $arr, null, 'unnamed 1' );
18 ApiResult::setValue( $arr, null, 'unnamed 2' );
19
20 ApiResult::setValue( $arr, 'deleteValue', '2' );
21 ApiResult::unsetValue( $arr, 'deleteValue' );
22
23 ApiResult::setContentValue( $arr, 'setContentValue', '3' );
24
25 $this->assertSame( array(
26 'setValue' => '1',
27 'unnamed 1',
28 'unnamed 2',
29 ApiResult::META_CONTENT => 'setContentValue',
30 'setContentValue' => '3',
31 ), $arr );
32
33 try {
34 ApiResult::setValue( $arr, 'setValue', '99' );
35 $this->fail( 'Expected exception not thrown' );
36 } catch ( RuntimeException $ex ) {
37 $this->assertSame(
38 'Attempting to add element setValue=99, existing value is 1',
39 $ex->getMessage(),
40 'Expected exception'
41 );
42 }
43
44 try {
45 ApiResult::setContentValue( $arr, 'setContentValue2', '99' );
46 $this->fail( 'Expected exception not thrown' );
47 } catch ( RuntimeException $ex ) {
48 $this->assertSame(
49 'Attempting to set content element as setContentValue2 when setContentValue ' .
50 'is already set as the content element',
51 $ex->getMessage(),
52 'Expected exception'
53 );
54 }
55
56 ApiResult::setValue( $arr, 'setValue', '99', ApiResult::OVERRIDE );
57 $this->assertSame( '99', $arr['setValue'] );
58
59 ApiResult::setContentValue( $arr, 'setContentValue2', '99', ApiResult::OVERRIDE );
60 $this->assertSame( 'setContentValue2', $arr[ApiResult::META_CONTENT] );
61
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 ) );
69
70 $arr = array();
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 );
74
75 try {
76 ApiResult::setValue( $arr, 'sub', array( 'foo' => 2, 'baz' => 2 ) );
77 $this->fail( 'Expected exception not thrown' );
78 } catch ( RuntimeException $ex ) {
79 $this->assertSame(
80 'Conflicting keys (foo) when attempting to merge element sub',
81 $ex->getMessage(),
82 'Expected exception'
83 );
84 }
85
86 $arr = array();
87 $title = Title::newFromText( "MediaWiki:Foobar" );
88 $obj = new stdClass;
89 $obj->foo = 1;
90 $obj->bar = 2;
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' ),
96 ), $arr );
97
98 $fh = tmpfile();
99 try {
100 ApiResult::setValue( $arr, 'file', $fh );
101 $this->fail( 'Expected exception not thrown' );
102 } catch ( InvalidArgumentException $ex ) {
103 $this->assertSame(
104 'Cannot add resource(stream) to ApiResult',
105 $ex->getMessage(),
106 'Expected exception'
107 );
108 }
109 try {
110 ApiResult::setValue( $arr, null, $fh );
111 $this->fail( 'Expected exception not thrown' );
112 } catch ( InvalidArgumentException $ex ) {
113 $this->assertSame(
114 'Cannot add resource(stream) to ApiResult',
115 $ex->getMessage(),
116 'Expected exception'
117 );
118 }
119 try {
120 $obj->file = $fh;
121 ApiResult::setValue( $arr, 'sub', $obj );
122 $this->fail( 'Expected exception not thrown' );
123 } catch ( InvalidArgumentException $ex ) {
124 $this->assertSame(
125 'Cannot add resource(stream) to ApiResult',
126 $ex->getMessage(),
127 'Expected exception'
128 );
129 }
130 try {
131 $obj->file = $fh;
132 ApiResult::setValue( $arr, null, $obj );
133 $this->fail( 'Expected exception not thrown' );
134 } catch ( InvalidArgumentException $ex ) {
135 $this->assertSame(
136 'Cannot add resource(stream) to ApiResult',
137 $ex->getMessage(),
138 'Expected exception'
139 );
140 }
141 fclose( $fh );
142
143 try {
144 ApiResult::setValue( $arr, 'inf', INF );
145 $this->fail( 'Expected exception not thrown' );
146 } catch ( InvalidArgumentException $ex ) {
147 $this->assertSame(
148 'Cannot add non-finite floats to ApiResult',
149 $ex->getMessage(),
150 'Expected exception'
151 );
152 }
153 try {
154 ApiResult::setValue( $arr, null, INF );
155 $this->fail( 'Expected exception not thrown' );
156 } catch ( InvalidArgumentException $ex ) {
157 $this->assertSame(
158 'Cannot add non-finite floats to ApiResult',
159 $ex->getMessage(),
160 'Expected exception'
161 );
162 }
163 try {
164 ApiResult::setValue( $arr, 'nan', NAN );
165 $this->fail( 'Expected exception not thrown' );
166 } catch ( InvalidArgumentException $ex ) {
167 $this->assertSame(
168 'Cannot add non-finite floats to ApiResult',
169 $ex->getMessage(),
170 'Expected exception'
171 );
172 }
173 try {
174 ApiResult::setValue( $arr, null, NAN );
175 $this->fail( 'Expected exception not thrown' );
176 } catch ( InvalidArgumentException $ex ) {
177 $this->assertSame(
178 'Cannot add non-finite floats to ApiResult',
179 $ex->getMessage(),
180 'Expected exception'
181 );
182 }
183
184 ApiResult::setValue( $arr, null, NAN, ApiResult::NO_VALIDATE );
185
186 try {
187 ApiResult::setValue( $arr, null, NAN, ApiResult::NO_SIZE_CHECK );
188 $this->fail( 'Expected exception not thrown' );
189 } catch ( InvalidArgumentException $ex ) {
190 $this->assertSame(
191 'Cannot add non-finite floats to ApiResult',
192 $ex->getMessage(),
193 'Expected exception'
194 );
195 }
196
197 $arr = array();
198 $result2 = new ApiResult( 8388608 );
199 $result2->addValue( null, 'foo', 'bar' );
200 ApiResult::setValue( $arr, 'baz', $result2 );
201 $this->assertSame( array(
202 'baz' => array(
203 ApiResult::META_TYPE => 'assoc',
204 'foo' => 'bar',
205 )
206 ), $arr );
207
208 $arr = array();
209 ApiResult::setValue( $arr, 'foo', "foo\x80bar" );
210 ApiResult::setValue( $arr, 'bar', "a\xcc\x81" );
211 ApiResult::setValue( $arr, 'baz', 74 );
212 ApiResult::setValue( $arr, null, "foo\x80bar" );
213 ApiResult::setValue( $arr, null, "a\xcc\x81" );
214 $this->assertSame( array(
215 'foo' => "foo\xef\xbf\xbdbar",
216 'bar' => "\xc3\xa1",
217 'baz' => 74,
218 0 => "foo\xef\xbf\xbdbar",
219 1 => "\xc3\xa1",
220 ), $arr );
221 }
222
223 /**
224 * @covers ApiResult
225 */
226 public function testInstanceDataMethods() {
227 $result = new ApiResult( 8388608 );
228
229 $result->addValue( null, 'setValue', '1' );
230
231 $result->addValue( null, null, 'unnamed 1' );
232 $result->addValue( null, null, 'unnamed 2' );
233
234 $result->addValue( null, 'deleteValue', '2' );
235 $result->removeValue( null, 'deleteValue' );
236
237 $result->addValue( array( 'a', 'b' ), 'deleteValue', '3' );
238 $result->removeValue( array( 'a', 'b', 'deleteValue' ), null, '3' );
239
240 $result->addContentValue( null, 'setContentValue', '3' );
241
242 $this->assertSame( array(
243 'setValue' => '1',
244 'unnamed 1',
245 'unnamed 2',
246 'a' => array( 'b' => array() ),
247 'setContentValue' => '3',
248 ApiResult::META_TYPE => 'assoc',
249 ApiResult::META_CONTENT => 'setContentValue',
250 ), $result->getResultData() );
251 $this->assertSame( 20, $result->getSize() );
252
253 try {
254 $result->addValue( null, 'setValue', '99' );
255 $this->fail( 'Expected exception not thrown' );
256 } catch ( RuntimeException $ex ) {
257 $this->assertSame(
258 'Attempting to add element setValue=99, existing value is 1',
259 $ex->getMessage(),
260 'Expected exception'
261 );
262 }
263
264 try {
265 $result->addContentValue( null, 'setContentValue2', '99' );
266 $this->fail( 'Expected exception not thrown' );
267 } catch ( RuntimeException $ex ) {
268 $this->assertSame(
269 'Attempting to set content element as setContentValue2 when setContentValue ' .
270 'is already set as the content element',
271 $ex->getMessage(),
272 'Expected exception'
273 );
274 }
275
276 $result->addValue( null, 'setValue', '99', ApiResult::OVERRIDE );
277 $this->assertSame( '99', $result->getResultData( array( 'setValue' ) ) );
278
279 $result->addContentValue( null, 'setContentValue2', '99', ApiResult::OVERRIDE );
280 $this->assertSame( 'setContentValue2',
281 $result->getResultData( array( ApiResult::META_CONTENT ) ) );
282
283 $result->reset();
284 $this->assertSame( array(
285 ApiResult::META_TYPE => 'assoc',
286 ), $result->getResultData() );
287 $this->assertSame( 0, $result->getSize() );
288
289 $result->addValue( null, 'foo', 1 );
290 $result->addValue( null, 'bar', 1 );
291 $result->addValue( null, 'top', '2', ApiResult::ADD_ON_TOP );
292 $result->addValue( null, null, '2', ApiResult::ADD_ON_TOP );
293 $result->addValue( null, 'bottom', '2' );
294 $result->addValue( null, 'foo', '2', ApiResult::OVERRIDE );
295 $result->addValue( null, 'bar', '2', ApiResult::OVERRIDE | ApiResult::ADD_ON_TOP );
296 $this->assertSame( array( 0, 'top', 'foo', 'bar', 'bottom', ApiResult::META_TYPE ),
297 array_keys( $result->getResultData() ) );
298
299 $result->reset();
300 $result->addValue( null, 'foo', array( 'bar' => 1 ) );
301 $result->addValue( array( 'foo', 'top' ), 'x', 2, ApiResult::ADD_ON_TOP );
302 $result->addValue( array( 'foo', 'bottom' ), 'x', 2 );
303 $this->assertSame( array( 'top', 'bar', 'bottom' ),
304 array_keys( $result->getResultData( array( 'foo' ) ) ) );
305
306 $result->reset();
307 $result->addValue( null, 'sub', array( 'foo' => 1 ) );
308 $result->addValue( null, 'sub', array( 'bar' => 1 ) );
309 $this->assertSame( array(
310 'sub' => array( 'foo' => 1, 'bar' => 1 ),
311 ApiResult::META_TYPE => 'assoc',
312 ), $result->getResultData() );
313
314 try {
315 $result->addValue( null, 'sub', array( 'foo' => 2, 'baz' => 2 ) );
316 $this->fail( 'Expected exception not thrown' );
317 } catch ( RuntimeException $ex ) {
318 $this->assertSame(
319 'Conflicting keys (foo) when attempting to merge element sub',
320 $ex->getMessage(),
321 'Expected exception'
322 );
323 }
324
325 $result->reset();
326 $title = Title::newFromText( "MediaWiki:Foobar" );
327 $obj = new stdClass;
328 $obj->foo = 1;
329 $obj->bar = 2;
330 $result->addValue( null, 'title', $title );
331 $result->addValue( null, 'obj', $obj );
332 $this->assertSame( array(
333 'title' => (string)$title,
334 'obj' => array( 'foo' => 1, 'bar' => 2, ApiResult::META_TYPE => 'assoc' ),
335 ApiResult::META_TYPE => 'assoc',
336 ), $result->getResultData() );
337
338 $fh = tmpfile();
339 try {
340 $result->addValue( null, 'file', $fh );
341 $this->fail( 'Expected exception not thrown' );
342 } catch ( InvalidArgumentException $ex ) {
343 $this->assertSame(
344 'Cannot add resource(stream) to ApiResult',
345 $ex->getMessage(),
346 'Expected exception'
347 );
348 }
349 try {
350 $result->addValue( null, null, $fh );
351 $this->fail( 'Expected exception not thrown' );
352 } catch ( InvalidArgumentException $ex ) {
353 $this->assertSame(
354 'Cannot add resource(stream) to ApiResult',
355 $ex->getMessage(),
356 'Expected exception'
357 );
358 }
359 try {
360 $obj->file = $fh;
361 $result->addValue( null, 'sub', $obj );
362 $this->fail( 'Expected exception not thrown' );
363 } catch ( InvalidArgumentException $ex ) {
364 $this->assertSame(
365 'Cannot add resource(stream) to ApiResult',
366 $ex->getMessage(),
367 'Expected exception'
368 );
369 }
370 try {
371 $obj->file = $fh;
372 $result->addValue( null, null, $obj );
373 $this->fail( 'Expected exception not thrown' );
374 } catch ( InvalidArgumentException $ex ) {
375 $this->assertSame(
376 'Cannot add resource(stream) to ApiResult',
377 $ex->getMessage(),
378 'Expected exception'
379 );
380 }
381 fclose( $fh );
382
383 try {
384 $result->addValue( null, 'inf', INF );
385 $this->fail( 'Expected exception not thrown' );
386 } catch ( InvalidArgumentException $ex ) {
387 $this->assertSame(
388 'Cannot add non-finite floats to ApiResult',
389 $ex->getMessage(),
390 'Expected exception'
391 );
392 }
393 try {
394 $result->addValue( null, null, INF );
395 $this->fail( 'Expected exception not thrown' );
396 } catch ( InvalidArgumentException $ex ) {
397 $this->assertSame(
398 'Cannot add non-finite floats to ApiResult',
399 $ex->getMessage(),
400 'Expected exception'
401 );
402 }
403 try {
404 $result->addValue( null, 'nan', NAN );
405 $this->fail( 'Expected exception not thrown' );
406 } catch ( InvalidArgumentException $ex ) {
407 $this->assertSame(
408 'Cannot add non-finite floats to ApiResult',
409 $ex->getMessage(),
410 'Expected exception'
411 );
412 }
413 try {
414 $result->addValue( null, null, NAN );
415 $this->fail( 'Expected exception not thrown' );
416 } catch ( InvalidArgumentException $ex ) {
417 $this->assertSame(
418 'Cannot add non-finite floats to ApiResult',
419 $ex->getMessage(),
420 'Expected exception'
421 );
422 }
423
424 $result->addValue( null, null, NAN, ApiResult::NO_VALIDATE );
425
426 try {
427 $result->addValue( null, null, NAN, ApiResult::NO_SIZE_CHECK );
428 $this->fail( 'Expected exception not thrown' );
429 } catch ( InvalidArgumentException $ex ) {
430 $this->assertSame(
431 'Cannot add non-finite floats to ApiResult',
432 $ex->getMessage(),
433 'Expected exception'
434 );
435 }
436
437 $result->reset();
438 $result->addParsedLimit( 'foo', 12 );
439 $this->assertSame( array(
440 'limits' => array( 'foo' => 12 ),
441 ApiResult::META_TYPE => 'assoc',
442 ), $result->getResultData() );
443 $result->addParsedLimit( 'foo', 13 );
444 $this->assertSame( array(
445 'limits' => array( 'foo' => 13 ),
446 ApiResult::META_TYPE => 'assoc',
447 ), $result->getResultData() );
448 $this->assertSame( null, $result->getResultData( array( 'foo', 'bar', 'baz' ) ) );
449 $this->assertSame( 13, $result->getResultData( array( 'limits', 'foo' ) ) );
450 try {
451 $result->getResultData( array( 'limits', 'foo', 'bar' ) );
452 $this->fail( 'Expected exception not thrown' );
453 } catch ( InvalidArgumentException $ex ) {
454 $this->assertSame(
455 'Path limits.foo is not an array',
456 $ex->getMessage(),
457 'Expected exception'
458 );
459 }
460
461 $result = new ApiResult( 10 );
462 $formatter = new ApiErrorFormatter( $result, Language::factory( 'en' ), 'none', false );
463 $result->setErrorFormatter( $formatter );
464 $this->assertFalse( $result->addValue( null, 'foo', '12345678901' ) );
465 $this->assertTrue( $result->addValue( null, 'foo', '12345678901', ApiResult::NO_SIZE_CHECK ) );
466 $this->assertSame( 0, $result->getSize() );
467 $result->reset();
468 $this->assertTrue( $result->addValue( null, 'foo', '1234567890' ) );
469 $this->assertFalse( $result->addValue( null, 'foo', '1' ) );
470 $result->removeValue( null, 'foo' );
471 $this->assertTrue( $result->addValue( null, 'foo', '1' ) );
472
473 $result = new ApiResult( 10 );
474 $obj = new ApiResultTestSerializableObject( 'ok' );
475 $obj->foobar = 'foobaz';
476 $this->assertTrue( $result->addValue( null, 'foo', $obj ) );
477 $this->assertSame( 2, $result->getSize() );
478
479 $result = new ApiResult( 8388608 );
480 $result2 = new ApiResult( 8388608 );
481 $result2->addValue( null, 'foo', 'bar' );
482 $result->addValue( null, 'baz', $result2 );
483 $this->assertSame( array(
484 'baz' => array(
485 'foo' => 'bar',
486 ApiResult::META_TYPE => 'assoc',
487 ),
488 ApiResult::META_TYPE => 'assoc',
489 ), $result->getResultData() );
490
491 $result = new ApiResult( 8388608 );
492 $result->addValue( null, 'foo', "foo\x80bar" );
493 $result->addValue( null, 'bar', "a\xcc\x81" );
494 $result->addValue( null, 'baz', 74 );
495 $result->addValue( null, null, "foo\x80bar" );
496 $result->addValue( null, null, "a\xcc\x81" );
497 $this->assertSame( array(
498 'foo' => "foo\xef\xbf\xbdbar",
499 'bar' => "\xc3\xa1",
500 'baz' => 74,
501 0 => "foo\xef\xbf\xbdbar",
502 1 => "\xc3\xa1",
503 ApiResult::META_TYPE => 'assoc',
504 ), $result->getResultData() );
505 }
506
507 /**
508 * @covers ApiResult
509 */
510 public function testMetadata() {
511 $arr = array( 'foo' => array( 'bar' => array() ) );
512 $result = new ApiResult( 8388608 );
513 $result->addValue( null, 'foo', array( 'bar' => array() ) );
514
515 $expect = array(
516 'foo' => array(
517 'bar' => array(
518 ApiResult::META_INDEXED_TAG_NAME => 'ritn',
519 ApiResult::META_TYPE => 'default',
520 ),
521 ApiResult::META_INDEXED_TAG_NAME => 'ritn',
522 ApiResult::META_TYPE => 'default',
523 ),
524 ApiResult::META_SUBELEMENTS => array( 'foo', 'bar' ),
525 ApiResult::META_INDEXED_TAG_NAME => 'itn',
526 ApiResult::META_PRESERVE_KEYS => array( 'foo', 'bar' ),
527 ApiResult::META_TYPE => 'array',
528 );
529
530 ApiResult::setSubelementsList( $arr, 'foo' );
531 ApiResult::setSubelementsList( $arr, array( 'bar', 'baz' ) );
532 ApiResult::unsetSubelementsList( $arr, 'baz' );
533 ApiResult::setIndexedTagNameRecursive( $arr, 'ritn' );
534 ApiResult::setIndexedTagName( $arr, 'itn' );
535 ApiResult::setPreserveKeysList( $arr, 'foo' );
536 ApiResult::setPreserveKeysList( $arr, array( 'bar', 'baz' ) );
537 ApiResult::unsetPreserveKeysList( $arr, 'baz' );
538 ApiResult::setArrayTypeRecursive( $arr, 'default' );
539 ApiResult::setArrayType( $arr, 'array' );
540 $this->assertSame( $expect, $arr );
541
542 $result->addSubelementsList( null, 'foo' );
543 $result->addSubelementsList( null, array( 'bar', 'baz' ) );
544 $result->removeSubelementsList( null, 'baz' );
545 $result->addIndexedTagNameRecursive( null, 'ritn' );
546 $result->addIndexedTagName( null, 'itn' );
547 $result->addPreserveKeysList( null, 'foo' );
548 $result->addPreserveKeysList( null, array( 'bar', 'baz' ) );
549 $result->removePreserveKeysList( null, 'baz' );
550 $result->addArrayTypeRecursive( null, 'default' );
551 $result->addArrayType( null, 'array' );
552 $this->assertEquals( $expect, $result->getResultData() );
553
554 $arr = array( 'foo' => array( 'bar' => array() ) );
555 $expect = array(
556 'foo' => array(
557 'bar' => array(
558 ApiResult::META_TYPE => 'kvp',
559 ApiResult::META_KVP_KEY_NAME => 'key',
560 ),
561 ApiResult::META_TYPE => 'kvp',
562 ApiResult::META_KVP_KEY_NAME => 'key',
563 ),
564 ApiResult::META_TYPE => 'BCkvp',
565 ApiResult::META_KVP_KEY_NAME => 'bc',
566 );
567 ApiResult::setArrayTypeRecursive( $arr, 'kvp', 'key' );
568 ApiResult::setArrayType( $arr, 'BCkvp', 'bc' );
569 $this->assertSame( $expect, $arr );
570 }
571
572 /**
573 * @covers ApiResult
574 */
575 public function testUtilityFunctions() {
576 $arr = array(
577 'foo' => array(
578 'bar' => array( '_dummy' => 'foobaz' ),
579 'bar2' => (object)array( '_dummy' => 'foobaz' ),
580 'x' => 'ok',
581 '_dummy' => 'foobaz',
582 ),
583 'foo2' => (object)array(
584 'bar' => array( '_dummy' => 'foobaz' ),
585 'bar2' => (object)array( '_dummy' => 'foobaz' ),
586 'x' => 'ok',
587 '_dummy' => 'foobaz',
588 ),
589 ApiResult::META_SUBELEMENTS => array( 'foo', 'bar' ),
590 ApiResult::META_INDEXED_TAG_NAME => 'itn',
591 ApiResult::META_PRESERVE_KEYS => array( 'foo', 'bar', '_dummy2', 0 ),
592 ApiResult::META_TYPE => 'array',
593 '_dummy' => 'foobaz',
594 '_dummy2' => 'foobaz!',
595 );
596 $this->assertEquals( array(
597 'foo' => array(
598 'bar' => array(),
599 'bar2' => (object)array(),
600 'x' => 'ok',
601 ),
602 'foo2' => (object)array(
603 'bar' => array(),
604 'bar2' => (object)array(),
605 'x' => 'ok',
606 ),
607 '_dummy2' => 'foobaz!',
608 ), ApiResult::stripMetadata( $arr ), 'ApiResult::stripMetadata' );
609
610 $metadata = array();
611 $data = ApiResult::stripMetadataNonRecursive( $arr, $metadata );
612 $this->assertEquals( array(
613 'foo' => array(
614 'bar' => array( '_dummy' => 'foobaz' ),
615 'bar2' => (object)array( '_dummy' => 'foobaz' ),
616 'x' => 'ok',
617 '_dummy' => 'foobaz',
618 ),
619 'foo2' => (object)array(
620 'bar' => array( '_dummy' => 'foobaz' ),
621 'bar2' => (object)array( '_dummy' => 'foobaz' ),
622 'x' => 'ok',
623 '_dummy' => 'foobaz',
624 ),
625 '_dummy2' => 'foobaz!',
626 ), $data, 'ApiResult::stripMetadataNonRecursive ($data)' );
627 $this->assertEquals( array(
628 ApiResult::META_SUBELEMENTS => array( 'foo', 'bar' ),
629 ApiResult::META_INDEXED_TAG_NAME => 'itn',
630 ApiResult::META_PRESERVE_KEYS => array( 'foo', 'bar', '_dummy2', 0 ),
631 ApiResult::META_TYPE => 'array',
632 '_dummy' => 'foobaz',
633 ), $metadata, 'ApiResult::stripMetadataNonRecursive ($metadata)' );
634
635 $metadata = null;
636 $data = ApiResult::stripMetadataNonRecursive( (object)$arr, $metadata );
637 $this->assertEquals( (object)array(
638 'foo' => array(
639 'bar' => array( '_dummy' => 'foobaz' ),
640 'bar2' => (object)array( '_dummy' => 'foobaz' ),
641 'x' => 'ok',
642 '_dummy' => 'foobaz',
643 ),
644 'foo2' => (object)array(
645 'bar' => array( '_dummy' => 'foobaz' ),
646 'bar2' => (object)array( '_dummy' => 'foobaz' ),
647 'x' => 'ok',
648 '_dummy' => 'foobaz',
649 ),
650 '_dummy2' => 'foobaz!',
651 ), $data, 'ApiResult::stripMetadataNonRecursive on object ($data)' );
652 $this->assertEquals( array(
653 ApiResult::META_SUBELEMENTS => array( 'foo', 'bar' ),
654 ApiResult::META_INDEXED_TAG_NAME => 'itn',
655 ApiResult::META_PRESERVE_KEYS => array( 'foo', 'bar', '_dummy2', 0 ),
656 ApiResult::META_TYPE => 'array',
657 '_dummy' => 'foobaz',
658 ), $metadata, 'ApiResult::stripMetadataNonRecursive on object ($metadata)' );
659 }
660
661 /**
662 * @covers ApiResult
663 * @dataProvider provideTransformations
664 * @param string $label
665 * @param array $input
666 * @param array $transforms
667 * @param array|Exception $expect
668 */
669 public function testTransformations( $label, $input, $transforms, $expect ) {
670 $result = new ApiResult( false );
671 $result->addValue( null, 'test', $input );
672
673 if ( $expect instanceof Exception ) {
674 try {
675 $output = $result->getResultData( 'test', $transforms );
676 $this->fail( 'Expected exception not thrown', $label );
677 } catch ( Exception $ex ) {
678 $this->assertEquals( $ex, $expect, $label );
679 }
680 } else {
681 $output = $result->getResultData( 'test', $transforms );
682 $this->assertEquals( $expect, $output, $label );
683 }
684 }
685
686 public function provideTransformations() {
687 $kvp = function ( $keyKey, $key, $valKey, $value ) {
688 return array(
689 $keyKey => $key,
690 $valKey => $value,
691 ApiResult::META_PRESERVE_KEYS => array( $keyKey ),
692 ApiResult::META_CONTENT => $valKey,
693 ApiResult::META_TYPE => 'assoc',
694 );
695 };
696 $typeArr = array(
697 'defaultArray' => array( 2 => 'a', 0 => 'b', 1 => 'c' ),
698 'defaultAssoc' => array( 'x' => 'a', 1 => 'b', 0 => 'c' ),
699 'defaultAssoc2' => array( 2 => 'a', 3 => 'b', 0 => 'c' ),
700 'array' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult::META_TYPE => 'array' ),
701 'BCarray' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult::META_TYPE => 'BCarray' ),
702 'BCassoc' => array( 'a', 'b', 'c', ApiResult::META_TYPE => 'BCassoc' ),
703 'assoc' => array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult::META_TYPE => 'assoc' ),
704 'kvp' => array( 'x' => 'a', 'y' => 'b', 'z' => array( 'c' ), ApiResult::META_TYPE => 'kvp' ),
705 'BCkvp' => array( 'x' => 'a', 'y' => 'b',
706 ApiResult::META_TYPE => 'BCkvp',
707 ApiResult::META_KVP_KEY_NAME => 'key',
708 ),
709 'kvpmerge' => array( 'x' => 'a', 'y' => array( 'b' ), 'z' => array( 'c' => 'd' ),
710 ApiResult::META_TYPE => 'kvp',
711 ApiResult::META_KVP_MERGE => true,
712 ),
713 'emptyDefault' => array( '_dummy' => 1 ),
714 'emptyAssoc' => array( '_dummy' => 1, ApiResult::META_TYPE => 'assoc' ),
715 '_dummy' => 1,
716 ApiResult::META_PRESERVE_KEYS => array( '_dummy' ),
717 );
718 $stripArr = array(
719 'foo' => array(
720 'bar' => array( '_dummy' => 'foobaz' ),
721 'baz' => array(
722 ApiResult::META_SUBELEMENTS => array( 'foo', 'bar' ),
723 ApiResult::META_INDEXED_TAG_NAME => 'itn',
724 ApiResult::META_PRESERVE_KEYS => array( 'foo', 'bar', '_dummy2', 0 ),
725 ApiResult::META_TYPE => 'array',
726 ),
727 'x' => 'ok',
728 '_dummy' => 'foobaz',
729 ),
730 ApiResult::META_SUBELEMENTS => array( 'foo', 'bar' ),
731 ApiResult::META_INDEXED_TAG_NAME => 'itn',
732 ApiResult::META_PRESERVE_KEYS => array( 'foo', 'bar', '_dummy2', 0 ),
733 ApiResult::META_TYPE => 'array',
734 '_dummy' => 'foobaz',
735 '_dummy2' => 'foobaz!',
736 );
737
738 return array(
739 array(
740 'BC: META_BC_BOOLS',
741 array(
742 'BCtrue' => true,
743 'BCfalse' => false,
744 'true' => true,
745 'false' => false,
746 ApiResult::META_BC_BOOLS => array( 0, 'true', 'false' ),
747 ),
748 array( 'BC' => array() ),
749 array(
750 'BCtrue' => '',
751 'true' => true,
752 'false' => false,
753 ApiResult::META_BC_BOOLS => array( 0, 'true', 'false' ),
754 )
755 ),
756 array(
757 'BC: META_BC_SUBELEMENTS',
758 array(
759 'bc' => 'foo',
760 'nobc' => 'bar',
761 ApiResult::META_BC_SUBELEMENTS => array( 'bc' ),
762 ),
763 array( 'BC' => array() ),
764 array(
765 'bc' => array(
766 '*' => 'foo',
767 ApiResult::META_CONTENT => '*',
768 ApiResult::META_TYPE => 'assoc',
769 ),
770 'nobc' => 'bar',
771 ApiResult::META_BC_SUBELEMENTS => array( 'bc' ),
772 ),
773 ),
774 array(
775 'BC: META_CONTENT',
776 array(
777 'content' => '!!!',
778 ApiResult::META_CONTENT => 'content',
779 ),
780 array( 'BC' => array() ),
781 array(
782 '*' => '!!!',
783 ApiResult::META_CONTENT => '*',
784 ),
785 ),
786 array(
787 'BC: BCkvp type',
788 array(
789 'foo' => 'foo value',
790 'bar' => 'bar value',
791 '_baz' => 'baz value',
792 ApiResult::META_TYPE => 'BCkvp',
793 ApiResult::META_KVP_KEY_NAME => 'key',
794 ApiResult::META_PRESERVE_KEYS => array( '_baz' ),
795 ),
796 array( 'BC' => array() ),
797 array(
798 $kvp( 'key', 'foo', '*', 'foo value' ),
799 $kvp( 'key', 'bar', '*', 'bar value' ),
800 $kvp( 'key', '_baz', '*', 'baz value' ),
801 ApiResult::META_TYPE => 'array',
802 ApiResult::META_KVP_KEY_NAME => 'key',
803 ApiResult::META_PRESERVE_KEYS => array( '_baz' ),
804 ),
805 ),
806 array(
807 'BC: BCarray type',
808 array(
809 ApiResult::META_TYPE => 'BCarray',
810 ),
811 array( 'BC' => array() ),
812 array(
813 ApiResult::META_TYPE => 'default',
814 ),
815 ),
816 array(
817 'BC: BCassoc type',
818 array(
819 ApiResult::META_TYPE => 'BCassoc',
820 ),
821 array( 'BC' => array() ),
822 array(
823 ApiResult::META_TYPE => 'default',
824 ),
825 ),
826 array(
827 'BC: BCkvp exception',
828 array(
829 ApiResult::META_TYPE => 'BCkvp',
830 ),
831 array( 'BC' => array() ),
832 new UnexpectedValueException(
833 'Type "BCkvp" used without setting ApiResult::META_KVP_KEY_NAME metadata item'
834 ),
835 ),
836 array(
837 'BC: nobool, no*, nosub',
838 array(
839 'true' => true,
840 'false' => false,
841 'content' => 'content',
842 ApiResult::META_CONTENT => 'content',
843 'bc' => 'foo',
844 ApiResult::META_BC_SUBELEMENTS => array( 'bc' ),
845 'BCarray' => array( ApiResult::META_TYPE => 'BCarray' ),
846 'BCassoc' => array( ApiResult::META_TYPE => 'BCassoc' ),
847 'BCkvp' => array(
848 'foo' => 'foo value',
849 'bar' => 'bar value',
850 '_baz' => 'baz value',
851 ApiResult::META_TYPE => 'BCkvp',
852 ApiResult::META_KVP_KEY_NAME => 'key',
853 ApiResult::META_PRESERVE_KEYS => array( '_baz' ),
854 ),
855 ),
856 array( 'BC' => array( 'nobool', 'no*', 'nosub' ) ),
857 array(
858 'true' => true,
859 'false' => false,
860 'content' => 'content',
861 'bc' => 'foo',
862 'BCarray' => array( ApiResult::META_TYPE => 'default' ),
863 'BCassoc' => array( ApiResult::META_TYPE => 'default' ),
864 'BCkvp' => array(
865 $kvp( 'key', 'foo', '*', 'foo value' ),
866 $kvp( 'key', 'bar', '*', 'bar value' ),
867 $kvp( 'key', '_baz', '*', 'baz value' ),
868 ApiResult::META_TYPE => 'array',
869 ApiResult::META_KVP_KEY_NAME => 'key',
870 ApiResult::META_PRESERVE_KEYS => array( '_baz' ),
871 ),
872 ApiResult::META_CONTENT => 'content',
873 ApiResult::META_BC_SUBELEMENTS => array( 'bc' ),
874 ),
875 ),
876
877 array(
878 'Types: Normal transform',
879 $typeArr,
880 array( 'Types' => array() ),
881 array(
882 'defaultArray' => array( 'b', 'c', 'a', ApiResult::META_TYPE => 'array' ),
883 'defaultAssoc' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult::META_TYPE => 'assoc' ),
884 'defaultAssoc2' => array( 2 => 'a', 3 => 'b', 0 => 'c', ApiResult::META_TYPE => 'assoc' ),
885 'array' => array( 'a', 'c', 'b', ApiResult::META_TYPE => 'array' ),
886 'BCarray' => array( 'a', 'c', 'b', ApiResult::META_TYPE => 'array' ),
887 'BCassoc' => array( 'a', 'b', 'c', ApiResult::META_TYPE => 'assoc' ),
888 'assoc' => array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult::META_TYPE => 'assoc' ),
889 'kvp' => array( 'x' => 'a', 'y' => 'b',
890 'z' => array( 'c', ApiResult::META_TYPE => 'array' ),
891 ApiResult::META_TYPE => 'assoc'
892 ),
893 'BCkvp' => array( 'x' => 'a', 'y' => 'b',
894 ApiResult::META_TYPE => 'assoc',
895 ApiResult::META_KVP_KEY_NAME => 'key',
896 ),
897 'kvpmerge' => array(
898 'x' => 'a',
899 'y' => array( 'b', ApiResult::META_TYPE => 'array' ),
900 'z' => array( 'c' => 'd', ApiResult::META_TYPE => 'assoc' ),
901 ApiResult::META_TYPE => 'assoc',
902 ApiResult::META_KVP_MERGE => true,
903 ),
904 'emptyDefault' => array( '_dummy' => 1, ApiResult::META_TYPE => 'array' ),
905 'emptyAssoc' => array( '_dummy' => 1, ApiResult::META_TYPE => 'assoc' ),
906 '_dummy' => 1,
907 ApiResult::META_PRESERVE_KEYS => array( '_dummy' ),
908 ApiResult::META_TYPE => 'assoc',
909 ),
910 ),
911 array(
912 'Types: AssocAsObject',
913 $typeArr,
914 array( 'Types' => array( 'AssocAsObject' => true ) ),
915 (object)array(
916 'defaultArray' => array( 'b', 'c', 'a', ApiResult::META_TYPE => 'array' ),
917 'defaultAssoc' => (object)array( 'x' => 'a',
918 1 => 'b', 0 => 'c', ApiResult::META_TYPE => 'assoc'
919 ),
920 'defaultAssoc2' => (object)array( 2 => 'a', 3 => 'b',
921 0 => 'c', ApiResult::META_TYPE => 'assoc'
922 ),
923 'array' => array( 'a', 'c', 'b', ApiResult::META_TYPE => 'array' ),
924 'BCarray' => array( 'a', 'c', 'b', ApiResult::META_TYPE => 'array' ),
925 'BCassoc' => (object)array( 'a', 'b', 'c', ApiResult::META_TYPE => 'assoc' ),
926 'assoc' => (object)array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult::META_TYPE => 'assoc' ),
927 'kvp' => (object)array( 'x' => 'a', 'y' => 'b',
928 'z' => array( 'c', ApiResult::META_TYPE => 'array' ),
929 ApiResult::META_TYPE => 'assoc'
930 ),
931 'BCkvp' => (object)array( 'x' => 'a', 'y' => 'b',
932 ApiResult::META_TYPE => 'assoc',
933 ApiResult::META_KVP_KEY_NAME => 'key',
934 ),
935 'kvpmerge' => (object)array(
936 'x' => 'a',
937 'y' => array( 'b', ApiResult::META_TYPE => 'array' ),
938 'z' => (object)array( 'c' => 'd', ApiResult::META_TYPE => 'assoc' ),
939 ApiResult::META_TYPE => 'assoc',
940 ApiResult::META_KVP_MERGE => true,
941 ),
942 'emptyDefault' => array( '_dummy' => 1, ApiResult::META_TYPE => 'array' ),
943 'emptyAssoc' => (object)array( '_dummy' => 1, ApiResult::META_TYPE => 'assoc' ),
944 '_dummy' => 1,
945 ApiResult::META_PRESERVE_KEYS => array( '_dummy' ),
946 ApiResult::META_TYPE => 'assoc',
947 ),
948 ),
949 array(
950 'Types: ArmorKVP',
951 $typeArr,
952 array( 'Types' => array( 'ArmorKVP' => 'name' ) ),
953 array(
954 'defaultArray' => array( 'b', 'c', 'a', ApiResult::META_TYPE => 'array' ),
955 'defaultAssoc' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult::META_TYPE => 'assoc' ),
956 'defaultAssoc2' => array( 2 => 'a', 3 => 'b', 0 => 'c', ApiResult::META_TYPE => 'assoc' ),
957 'array' => array( 'a', 'c', 'b', ApiResult::META_TYPE => 'array' ),
958 'BCarray' => array( 'a', 'c', 'b', ApiResult::META_TYPE => 'array' ),
959 'BCassoc' => array( 'a', 'b', 'c', ApiResult::META_TYPE => 'assoc' ),
960 'assoc' => array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult::META_TYPE => 'assoc' ),
961 'kvp' => array(
962 $kvp( 'name', 'x', 'value', 'a' ),
963 $kvp( 'name', 'y', 'value', 'b' ),
964 $kvp( 'name', 'z', 'value', array( 'c', ApiResult::META_TYPE => 'array' ) ),
965 ApiResult::META_TYPE => 'array'
966 ),
967 'BCkvp' => array(
968 $kvp( 'key', 'x', 'value', 'a' ),
969 $kvp( 'key', 'y', 'value', 'b' ),
970 ApiResult::META_TYPE => 'array',
971 ApiResult::META_KVP_KEY_NAME => 'key',
972 ),
973 'kvpmerge' => array(
974 $kvp( 'name', 'x', 'value', 'a' ),
975 $kvp( 'name', 'y', 'value', array( 'b', ApiResult::META_TYPE => 'array' ) ),
976 array(
977 'name' => 'z',
978 'c' => 'd',
979 ApiResult::META_TYPE => 'assoc',
980 ApiResult::META_PRESERVE_KEYS => array( 'name' )
981 ),
982 ApiResult::META_TYPE => 'array',
983 ApiResult::META_KVP_MERGE => true,
984 ),
985 'emptyDefault' => array( '_dummy' => 1, ApiResult::META_TYPE => 'array' ),
986 'emptyAssoc' => array( '_dummy' => 1, ApiResult::META_TYPE => 'assoc' ),
987 '_dummy' => 1,
988 ApiResult::META_PRESERVE_KEYS => array( '_dummy' ),
989 ApiResult::META_TYPE => 'assoc',
990 ),
991 ),
992 array(
993 'Types: ArmorKVP + BC',
994 $typeArr,
995 array( 'BC' => array(), 'Types' => array( 'ArmorKVP' => 'name' ) ),
996 array(
997 'defaultArray' => array( 'b', 'c', 'a', ApiResult::META_TYPE => 'array' ),
998 'defaultAssoc' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult::META_TYPE => 'assoc' ),
999 'defaultAssoc2' => array( 2 => 'a', 3 => 'b', 0 => 'c', ApiResult::META_TYPE => 'assoc' ),
1000 'array' => array( 'a', 'c', 'b', ApiResult::META_TYPE => 'array' ),
1001 'BCarray' => array( 'x' => 'a', 1 => 'b', 0 => 'c', ApiResult::META_TYPE => 'assoc' ),
1002 'BCassoc' => array( 'a', 'b', 'c', ApiResult::META_TYPE => 'array' ),
1003 'assoc' => array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult::META_TYPE => 'assoc' ),
1004 'kvp' => array(
1005 $kvp( 'name', 'x', '*', 'a' ),
1006 $kvp( 'name', 'y', '*', 'b' ),
1007 $kvp( 'name', 'z', '*', array( 'c', ApiResult::META_TYPE => 'array' ) ),
1008 ApiResult::META_TYPE => 'array'
1009 ),
1010 'BCkvp' => array(
1011 $kvp( 'key', 'x', '*', 'a' ),
1012 $kvp( 'key', 'y', '*', 'b' ),
1013 ApiResult::META_TYPE => 'array',
1014 ApiResult::META_KVP_KEY_NAME => 'key',
1015 ),
1016 'kvpmerge' => array(
1017 $kvp( 'name', 'x', '*', 'a' ),
1018 $kvp( 'name', 'y', '*', array( 'b', ApiResult::META_TYPE => 'array' ) ),
1019 array(
1020 'name' => 'z',
1021 'c' => 'd',
1022 ApiResult::META_TYPE => 'assoc',
1023 ApiResult::META_PRESERVE_KEYS => array( 'name' ) ),
1024 ApiResult::META_TYPE => 'array',
1025 ApiResult::META_KVP_MERGE => true,
1026 ),
1027 'emptyDefault' => array( '_dummy' => 1, ApiResult::META_TYPE => 'array' ),
1028 'emptyAssoc' => array( '_dummy' => 1, ApiResult::META_TYPE => 'assoc' ),
1029 '_dummy' => 1,
1030 ApiResult::META_PRESERVE_KEYS => array( '_dummy' ),
1031 ApiResult::META_TYPE => 'assoc',
1032 ),
1033 ),
1034 array(
1035 'Types: ArmorKVP + AssocAsObject',
1036 $typeArr,
1037 array( 'Types' => array( 'ArmorKVP' => 'name', 'AssocAsObject' => true ) ),
1038 (object)array(
1039 'defaultArray' => array( 'b', 'c', 'a', ApiResult::META_TYPE => 'array' ),
1040 'defaultAssoc' => (object)array( 'x' => 'a', 1 => 'b',
1041 0 => 'c', ApiResult::META_TYPE => 'assoc'
1042 ),
1043 'defaultAssoc2' => (object)array( 2 => 'a', 3 => 'b',
1044 0 => 'c', ApiResult::META_TYPE => 'assoc'
1045 ),
1046 'array' => array( 'a', 'c', 'b', ApiResult::META_TYPE => 'array' ),
1047 'BCarray' => array( 'a', 'c', 'b', ApiResult::META_TYPE => 'array' ),
1048 'BCassoc' => (object)array( 'a', 'b', 'c', ApiResult::META_TYPE => 'assoc' ),
1049 'assoc' => (object)array( 2 => 'a', 0 => 'b', 1 => 'c', ApiResult::META_TYPE => 'assoc' ),
1050 'kvp' => array(
1051 (object)$kvp( 'name', 'x', 'value', 'a' ),
1052 (object)$kvp( 'name', 'y', 'value', 'b' ),
1053 (object)$kvp( 'name', 'z', 'value', array( 'c', ApiResult::META_TYPE => 'array' ) ),
1054 ApiResult::META_TYPE => 'array'
1055 ),
1056 'BCkvp' => array(
1057 (object)$kvp( 'key', 'x', 'value', 'a' ),
1058 (object)$kvp( 'key', 'y', 'value', 'b' ),
1059 ApiResult::META_TYPE => 'array',
1060 ApiResult::META_KVP_KEY_NAME => 'key',
1061 ),
1062 'kvpmerge' => array(
1063 (object)$kvp( 'name', 'x', 'value', 'a' ),
1064 (object)$kvp( 'name', 'y', 'value', array( 'b', ApiResult::META_TYPE => 'array' ) ),
1065 (object)array(
1066 'name' => 'z',
1067 'c' => 'd',
1068 ApiResult::META_TYPE => 'assoc',
1069 ApiResult::META_PRESERVE_KEYS => array( 'name' )
1070 ),
1071 ApiResult::META_TYPE => 'array',
1072 ApiResult::META_KVP_MERGE => true,
1073 ),
1074 'emptyDefault' => array( '_dummy' => 1, ApiResult::META_TYPE => 'array' ),
1075 'emptyAssoc' => (object)array( '_dummy' => 1, ApiResult::META_TYPE => 'assoc' ),
1076 '_dummy' => 1,
1077 ApiResult::META_PRESERVE_KEYS => array( '_dummy' ),
1078 ApiResult::META_TYPE => 'assoc',
1079 ),
1080 ),
1081 array(
1082 'Types: BCkvp exception',
1083 array(
1084 ApiResult::META_TYPE => 'BCkvp',
1085 ),
1086 array( 'Types' => array() ),
1087 new UnexpectedValueException(
1088 'Type "BCkvp" used without setting ApiResult::META_KVP_KEY_NAME metadata item'
1089 ),
1090 ),
1091
1092 array(
1093 'Strip: With ArmorKVP + AssocAsObject transforms',
1094 $typeArr,
1095 array( 'Types' => array( 'ArmorKVP' => 'name', 'AssocAsObject' => true ), 'Strip' => 'all' ),
1096 (object)array(
1097 'defaultArray' => array( 'b', 'c', 'a' ),
1098 'defaultAssoc' => (object)array( 'x' => 'a', 1 => 'b', 0 => 'c' ),
1099 'defaultAssoc2' => (object)array( 2 => 'a', 3 => 'b', 0 => 'c' ),
1100 'array' => array( 'a', 'c', 'b' ),
1101 'BCarray' => array( 'a', 'c', 'b' ),
1102 'BCassoc' => (object)array( 'a', 'b', 'c' ),
1103 'assoc' => (object)array( 2 => 'a', 0 => 'b', 1 => 'c' ),
1104 'kvp' => array(
1105 (object)array( 'name' => 'x', 'value' => 'a' ),
1106 (object)array( 'name' => 'y', 'value' => 'b' ),
1107 (object)array( 'name' => 'z', 'value' => array( 'c' ) ),
1108 ),
1109 'BCkvp' => array(
1110 (object)array( 'key' => 'x', 'value' => 'a' ),
1111 (object)array( 'key' => 'y', 'value' => 'b' ),
1112 ),
1113 'kvpmerge' => array(
1114 (object)array( 'name' => 'x', 'value' => 'a' ),
1115 (object)array( 'name' => 'y', 'value' => array( 'b' ) ),
1116 (object)array( 'name' => 'z', 'c' => 'd' ),
1117 ),
1118 'emptyDefault' => array(),
1119 'emptyAssoc' => (object)array(),
1120 '_dummy' => 1,
1121 ),
1122 ),
1123
1124 array(
1125 'Strip: all',
1126 $stripArr,
1127 array( 'Strip' => 'all' ),
1128 array(
1129 'foo' => array(
1130 'bar' => array(),
1131 'baz' => array(),
1132 'x' => 'ok',
1133 ),
1134 '_dummy2' => 'foobaz!',
1135 ),
1136 ),
1137 array(
1138 'Strip: base',
1139 $stripArr,
1140 array( 'Strip' => 'base' ),
1141 array(
1142 'foo' => array(
1143 'bar' => array( '_dummy' => 'foobaz' ),
1144 'baz' => array(
1145 ApiResult::META_SUBELEMENTS => array( 'foo', 'bar' ),
1146 ApiResult::META_INDEXED_TAG_NAME => 'itn',
1147 ApiResult::META_PRESERVE_KEYS => array( 'foo', 'bar', '_dummy2', 0 ),
1148 ApiResult::META_TYPE => 'array',
1149 ),
1150 'x' => 'ok',
1151 '_dummy' => 'foobaz',
1152 ),
1153 '_dummy2' => 'foobaz!',
1154 ),
1155 ),
1156 array(
1157 'Strip: bc',
1158 $stripArr,
1159 array( 'Strip' => 'bc' ),
1160 array(
1161 'foo' => array(
1162 'bar' => array(),
1163 'baz' => array(
1164 ApiResult::META_SUBELEMENTS => array( 'foo', 'bar' ),
1165 ApiResult::META_INDEXED_TAG_NAME => 'itn',
1166 ),
1167 'x' => 'ok',
1168 ),
1169 '_dummy2' => 'foobaz!',
1170 ApiResult::META_SUBELEMENTS => array( 'foo', 'bar' ),
1171 ApiResult::META_INDEXED_TAG_NAME => 'itn',
1172 ),
1173 ),
1174
1175 array(
1176 'Custom transform',
1177 array(
1178 'foo' => '?',
1179 'bar' => '?',
1180 '_dummy' => '?',
1181 '_dummy2' => '?',
1182 '_dummy3' => '?',
1183 ApiResult::META_CONTENT => 'foo',
1184 ApiResult::META_PRESERVE_KEYS => array( '_dummy2', '_dummy3' ),
1185 ),
1186 array(
1187 'Custom' => array( $this, 'customTransform' ),
1188 'BC' => array(),
1189 'Types' => array(),
1190 'Strip' => 'all'
1191 ),
1192 array(
1193 '*' => 'FOO',
1194 'bar' => 'BAR',
1195 'baz' => array( 'a', 'b' ),
1196 '_dummy2' => '_DUMMY2',
1197 '_dummy3' => '_DUMMY3',
1198 ApiResult::META_CONTENT => 'bar',
1199 ),
1200 ),
1201 );
1202
1203 }
1204
1205 /**
1206 * Custom transformer for testTransformations
1207 * @param array &$data
1208 * @param array &$metadata
1209 */
1210 public function customTransform( &$data, &$metadata ) {
1211 // Prevent recursion
1212 if ( isset( $metadata['_added'] ) ) {
1213 $metadata[ApiResult::META_TYPE] = 'array';
1214 return;
1215 }
1216
1217 foreach ( $data as $k => $v ) {
1218 $data[$k] = strtoupper( $k );
1219 }
1220 $data['baz'] = array( '_added' => 1, 'z' => 'b', 'y' => 'a' );
1221 $metadata[ApiResult::META_PRESERVE_KEYS][0] = '_dummy';
1222 $data[ApiResult::META_CONTENT] = 'bar';
1223 }
1224
1225 /**
1226 * @covers ApiResult
1227 */
1228 public function testAddMetadataToResultVars() {
1229 $arr = array(
1230 'a' => "foo",
1231 'b' => false,
1232 'c' => 10,
1233 'sequential_numeric_keys' => array( 'a', 'b', 'c' ),
1234 'non_sequential_numeric_keys' => array( 'a', 'b', 4 => 'c' ),
1235 'string_keys' => array(
1236 'one' => 1,
1237 'two' => 2
1238 ),
1239 'object_sequential_keys' => (object)array( 'a', 'b', 'c' ),
1240 '_type' => "should be overwritten in result",
1241 );
1242 $this->assertSame( array(
1243 ApiResult::META_TYPE => 'kvp',
1244 ApiResult::META_KVP_KEY_NAME => 'key',
1245 ApiResult::META_PRESERVE_KEYS => array(
1246 'a', 'b', 'c',
1247 'sequential_numeric_keys', 'non_sequential_numeric_keys',
1248 'string_keys', 'object_sequential_keys'
1249 ),
1250 ApiResult::META_BC_BOOLS => array( 'b' ),
1251 ApiResult::META_INDEXED_TAG_NAME => 'var',
1252 'a' => "foo",
1253 'b' => false,
1254 'c' => 10,
1255 'sequential_numeric_keys' => array(
1256 ApiResult::META_TYPE => 'array',
1257 ApiResult::META_BC_BOOLS => array(),
1258 ApiResult::META_INDEXED_TAG_NAME => 'value',
1259 0 => 'a',
1260 1 => 'b',
1261 2 => 'c',
1262 ),
1263 'non_sequential_numeric_keys' => array(
1264 ApiResult::META_TYPE => 'kvp',
1265 ApiResult::META_KVP_KEY_NAME => 'key',
1266 ApiResult::META_PRESERVE_KEYS => array( 0, 1, 4 ),
1267 ApiResult::META_BC_BOOLS => array(),
1268 ApiResult::META_INDEXED_TAG_NAME => 'var',
1269 0 => 'a',
1270 1 => 'b',
1271 4 => 'c',
1272 ),
1273 'string_keys' => array(
1274 ApiResult::META_TYPE => 'kvp',
1275 ApiResult::META_KVP_KEY_NAME => 'key',
1276 ApiResult::META_PRESERVE_KEYS => array( 'one', 'two' ),
1277 ApiResult::META_BC_BOOLS => array(),
1278 ApiResult::META_INDEXED_TAG_NAME => 'var',
1279 'one' => 1,
1280 'two' => 2,
1281 ),
1282 'object_sequential_keys' => array(
1283 ApiResult::META_TYPE => 'kvp',
1284 ApiResult::META_KVP_KEY_NAME => 'key',
1285 ApiResult::META_PRESERVE_KEYS => array( 0, 1, 2 ),
1286 ApiResult::META_BC_BOOLS => array(),
1287 ApiResult::META_INDEXED_TAG_NAME => 'var',
1288 0 => 'a',
1289 1 => 'b',
1290 2 => 'c',
1291 ),
1292 ), ApiResult::addMetadataToResultVars( $arr ) );
1293 }
1294
1295 /**
1296 * @covers ApiResult
1297 */
1298 public function testDeprecatedFunctions() {
1299 // Ignore ApiResult deprecation warnings during this test
1300 set_error_handler( function ( $errno, $errstr ) use ( &$warnings ) {
1301 if ( preg_match( '/Use of ApiResult::\S+ was deprecated in MediaWiki \d+.\d+\./', $errstr ) ) {
1302 return true;
1303 }
1304 if ( preg_match( '/Use of ApiMain to ApiResult::__construct ' .
1305 'was deprecated in MediaWiki \d+.\d+\./', $errstr ) ) {
1306 return true;
1307 }
1308 return false;
1309 } );
1310 $reset = new ScopedCallback( 'restore_error_handler' );
1311
1312 $context = new DerivativeContext( RequestContext::getMain() );
1313 $context->setConfig( new HashConfig( array(
1314 'APIModules' => array(),
1315 'APIFormatModules' => array(),
1316 'APIMaxResultSize' => 42,
1317 ) ) );
1318 $main = new ApiMain( $context );
1319 $result = TestingAccessWrapper::newFromObject( new ApiResult( $main ) );
1320 $this->assertSame( 42, $result->maxSize );
1321 $this->assertSame( $main->getErrorFormatter(), $result->errorFormatter );
1322 $this->assertSame( $main, $result->mainForContinuation );
1323
1324 $result = new ApiResult( 8388608 );
1325
1326 $result->addContentValue( null, 'test', 'content' );
1327 $result->addContentValue( array( 'foo', 'bar' ), 'test', 'content' );
1328 $result->addIndexedTagName( null, 'itn' );
1329 $result->addSubelementsList( null, array( 'sub' ) );
1330 $this->assertSame( array(
1331 'foo' => array(
1332 'bar' => array(
1333 '*' => 'content',
1334 ),
1335 ),
1336 '*' => 'content',
1337 ), $result->getData() );
1338
1339 $arr = array();
1340 ApiResult::setContent( $arr, 'value' );
1341 ApiResult::setContent( $arr, 'value2', 'foobar' );
1342 $this->assertSame( array(
1343 ApiResult::META_CONTENT => 'content',
1344 'content' => 'value',
1345 'foobar' => array(
1346 ApiResult::META_CONTENT => 'content',
1347 'content' => 'value2',
1348 ),
1349 ), $arr );
1350
1351 $result = new ApiResult( 3 );
1352 $formatter = new ApiErrorFormatter_BackCompat( $result );
1353 $result->setErrorFormatter( $formatter );
1354 $result->disableSizeCheck();
1355 $this->assertTrue( $result->addValue( null, 'foo', '1234567890' ) );
1356 $result->enableSizeCheck();
1357 $this->assertSame( 0, $result->getSize() );
1358 $this->assertFalse( $result->addValue( null, 'foo', '1234567890' ) );
1359
1360 $arr = array( 'foo' => array( 'bar' => 1 ) );
1361 $result->setIndexedTagName_recursive( $arr, 'itn' );
1362 $this->assertSame( array(
1363 'foo' => array(
1364 'bar' => 1,
1365 ApiResult::META_INDEXED_TAG_NAME => 'itn'
1366 ),
1367 ), $arr );
1368
1369 $status = Status::newGood();
1370 $status->fatal( 'parentheses', '1' );
1371 $status->fatal( 'parentheses', '2' );
1372 $status->warning( 'parentheses', '3' );
1373 $status->warning( 'parentheses', '4' );
1374 $this->assertSame( array(
1375 array(
1376 'type' => 'error',
1377 'message' => 'parentheses',
1378 'params' => array(
1379 0 => '1',
1380 ApiResult::META_INDEXED_TAG_NAME => 'param',
1381 ),
1382 ),
1383 array(
1384 'type' => 'error',
1385 'message' => 'parentheses',
1386 'params' => array(
1387 0 => '2',
1388 ApiResult::META_INDEXED_TAG_NAME => 'param',
1389 ),
1390 ),
1391 ApiResult::META_INDEXED_TAG_NAME => 'error',
1392 ), $result->convertStatusToArray( $status, 'error' ) );
1393 $this->assertSame( array(
1394 array(
1395 'type' => 'warning',
1396 'message' => 'parentheses',
1397 'params' => array(
1398 0 => '3',
1399 ApiResult::META_INDEXED_TAG_NAME => 'param',
1400 ),
1401 ),
1402 array(
1403 'type' => 'warning',
1404 'message' => 'parentheses',
1405 'params' => array(
1406 0 => '4',
1407 ApiResult::META_INDEXED_TAG_NAME => 'param',
1408 ),
1409 ),
1410 ApiResult::META_INDEXED_TAG_NAME => 'warning',
1411 ), $result->convertStatusToArray( $status, 'warning' ) );
1412 }
1413
1414 /**
1415 * @covers ApiResult
1416 */
1417 public function testDeprecatedContinuation() {
1418 // Ignore ApiResult deprecation warnings during this test
1419 set_error_handler( function ( $errno, $errstr ) use ( &$warnings ) {
1420 if ( preg_match( '/Use of ApiResult::\S+ was deprecated in MediaWiki \d+.\d+\./', $errstr ) ) {
1421 return true;
1422 }
1423 return false;
1424 } );
1425
1426 $reset = new ScopedCallback( 'restore_error_handler' );
1427 $allModules = array(
1428 new MockApiQueryBase( 'mock1' ),
1429 new MockApiQueryBase( 'mock2' ),
1430 new MockApiQueryBase( 'mocklist' ),
1431 );
1432 $generator = new MockApiQueryBase( 'generator' );
1433
1434 $main = new ApiMain( RequestContext::getMain() );
1435 $result = new ApiResult( 8388608 );
1436 $result->setMainForContinuation( $main );
1437 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1438 $this->assertSame( array( false, $allModules ), $ret );
1439 $result->setContinueParam( $allModules[0], 'm1continue', array( 1, 2 ) );
1440 $result->setContinueParam( $allModules[2], 'mlcontinue', 2 );
1441 $result->setGeneratorContinueParam( $generator, 'gcontinue', 3 );
1442 $result->endContinuation( 'raw' );
1443 $result->endContinuation( 'standard' );
1444 $this->assertSame( array(
1445 'mlcontinue' => 2,
1446 'm1continue' => '1|2',
1447 'continue' => '||mock2',
1448 ), $result->getResultData( 'continue' ) );
1449 $this->assertSame( null, $result->getResultData( 'batchcomplete' ) );
1450 $this->assertSame( array(
1451 'mock1' => array( 'm1continue' => '1|2' ),
1452 'mocklist' => array( 'mlcontinue' => 2 ),
1453 'generator' => array( 'gcontinue' => 3 ),
1454 ), $result->getResultData( 'query-continue' ) );
1455 $main->setContinuationManager( null );
1456
1457 $result = new ApiResult( 8388608 );
1458 $result->setMainForContinuation( $main );
1459 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1460 $this->assertSame( array( false, $allModules ), $ret );
1461 $result->setContinueParam( $allModules[0], 'm1continue', array( 1, 2 ) );
1462 $result->setGeneratorContinueParam( $generator, 'gcontinue', array( 3, 4 ) );
1463 $result->endContinuation( 'raw' );
1464 $result->endContinuation( 'standard' );
1465 $this->assertSame( array(
1466 'm1continue' => '1|2',
1467 'continue' => '||mock2|mocklist',
1468 ), $result->getResultData( 'continue' ) );
1469 $this->assertSame( null, $result->getResultData( 'batchcomplete' ) );
1470 $this->assertSame( array(
1471 'mock1' => array( 'm1continue' => '1|2' ),
1472 'generator' => array( 'gcontinue' => '3|4' ),
1473 ), $result->getResultData( 'query-continue' ) );
1474 $main->setContinuationManager( null );
1475
1476 $result = new ApiResult( 8388608 );
1477 $result->setMainForContinuation( $main );
1478 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1479 $this->assertSame( array( false, $allModules ), $ret );
1480 $result->setContinueParam( $allModules[2], 'mlcontinue', 2 );
1481 $result->setGeneratorContinueParam( $generator, 'gcontinue', 3 );
1482 $result->endContinuation( 'raw' );
1483 $result->endContinuation( 'standard' );
1484 $this->assertSame( array(
1485 'mlcontinue' => 2,
1486 'gcontinue' => 3,
1487 'continue' => 'gcontinue||',
1488 ), $result->getResultData( 'continue' ) );
1489 $this->assertSame( true, $result->getResultData( 'batchcomplete' ) );
1490 $this->assertSame( array(
1491 'mocklist' => array( 'mlcontinue' => 2 ),
1492 'generator' => array( 'gcontinue' => 3 ),
1493 ), $result->getResultData( 'query-continue' ) );
1494 $main->setContinuationManager( null );
1495
1496 $result = new ApiResult( 8388608 );
1497 $result->setMainForContinuation( $main );
1498 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1499 $this->assertSame( array( false, $allModules ), $ret );
1500 $result->setGeneratorContinueParam( $generator, 'gcontinue', 3 );
1501 $result->endContinuation( 'raw' );
1502 $result->endContinuation( 'standard' );
1503 $this->assertSame( array(
1504 'gcontinue' => 3,
1505 'continue' => 'gcontinue||mocklist',
1506 ), $result->getResultData( 'continue' ) );
1507 $this->assertSame( true, $result->getResultData( 'batchcomplete' ) );
1508 $this->assertSame( array(
1509 'generator' => array( 'gcontinue' => 3 ),
1510 ), $result->getResultData( 'query-continue' ) );
1511 $main->setContinuationManager( null );
1512
1513 $result = new ApiResult( 8388608 );
1514 $result->setMainForContinuation( $main );
1515 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1516 $this->assertSame( array( false, $allModules ), $ret );
1517 $result->setContinueParam( $allModules[0], 'm1continue', array( 1, 2 ) );
1518 $result->setContinueParam( $allModules[2], 'mlcontinue', 2 );
1519 $result->endContinuation( 'raw' );
1520 $result->endContinuation( 'standard' );
1521 $this->assertSame( array(
1522 'mlcontinue' => 2,
1523 'm1continue' => '1|2',
1524 'continue' => '||mock2',
1525 ), $result->getResultData( 'continue' ) );
1526 $this->assertSame( null, $result->getResultData( 'batchcomplete' ) );
1527 $this->assertSame( array(
1528 'mock1' => array( 'm1continue' => '1|2' ),
1529 'mocklist' => array( 'mlcontinue' => 2 ),
1530 ), $result->getResultData( 'query-continue' ) );
1531 $main->setContinuationManager( null );
1532
1533 $result = new ApiResult( 8388608 );
1534 $result->setMainForContinuation( $main );
1535 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1536 $this->assertSame( array( false, $allModules ), $ret );
1537 $result->setContinueParam( $allModules[0], 'm1continue', array( 1, 2 ) );
1538 $result->endContinuation( 'raw' );
1539 $result->endContinuation( 'standard' );
1540 $this->assertSame( array(
1541 'm1continue' => '1|2',
1542 'continue' => '||mock2|mocklist',
1543 ), $result->getResultData( 'continue' ) );
1544 $this->assertSame( null, $result->getResultData( 'batchcomplete' ) );
1545 $this->assertSame( array(
1546 'mock1' => array( 'm1continue' => '1|2' ),
1547 ), $result->getResultData( 'query-continue' ) );
1548 $main->setContinuationManager( null );
1549
1550 $result = new ApiResult( 8388608 );
1551 $result->setMainForContinuation( $main );
1552 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1553 $this->assertSame( array( false, $allModules ), $ret );
1554 $result->setContinueParam( $allModules[2], 'mlcontinue', 2 );
1555 $result->endContinuation( 'raw' );
1556 $result->endContinuation( 'standard' );
1557 $this->assertSame( array(
1558 'mlcontinue' => 2,
1559 'continue' => '-||mock1|mock2',
1560 ), $result->getResultData( 'continue' ) );
1561 $this->assertSame( true, $result->getResultData( 'batchcomplete' ) );
1562 $this->assertSame( array(
1563 'mocklist' => array( 'mlcontinue' => 2 ),
1564 ), $result->getResultData( 'query-continue' ) );
1565 $main->setContinuationManager( null );
1566
1567 $result = new ApiResult( 8388608 );
1568 $result->setMainForContinuation( $main );
1569 $ret = $result->beginContinuation( null, $allModules, array( 'mock1', 'mock2' ) );
1570 $this->assertSame( array( false, $allModules ), $ret );
1571 $result->endContinuation( 'raw' );
1572 $result->endContinuation( 'standard' );
1573 $this->assertSame( null, $result->getResultData( 'continue' ) );
1574 $this->assertSame( true, $result->getResultData( 'batchcomplete' ) );
1575 $this->assertSame( null, $result->getResultData( 'query-continue' ) );
1576 $main->setContinuationManager( null );
1577
1578 $result = new ApiResult( 8388608 );
1579 $result->setMainForContinuation( $main );
1580 $ret = $result->beginContinuation( '||mock2', $allModules, array( 'mock1', 'mock2' ) );
1581 $this->assertSame(
1582 array( false, array_values( array_diff_key( $allModules, array( 1 => 1 ) ) ) ),
1583 $ret
1584 );
1585 $main->setContinuationManager( null );
1586
1587 $result = new ApiResult( 8388608 );
1588 $result->setMainForContinuation( $main );
1589 $ret = $result->beginContinuation( '-||', $allModules, array( 'mock1', 'mock2' ) );
1590 $this->assertSame(
1591 array( true, array_values( array_diff_key( $allModules, array( 0 => 0, 1 => 1 ) ) ) ),
1592 $ret
1593 );
1594 $main->setContinuationManager( null );
1595
1596 $result = new ApiResult( 8388608 );
1597 $result->setMainForContinuation( $main );
1598 try {
1599 $result->beginContinuation( 'foo', $allModules, array( 'mock1', 'mock2' ) );
1600 $this->fail( 'Expected exception not thrown' );
1601 } catch ( UsageException $ex ) {
1602 $this->assertSame(
1603 'Invalid continue param. You should pass the original value returned by the previous query',
1604 $ex->getMessage(),
1605 'Expected exception'
1606 );
1607 }
1608 $main->setContinuationManager( null );
1609
1610 $result = new ApiResult( 8388608 );
1611 $result->setMainForContinuation( $main );
1612 $result->beginContinuation( '||mock2', array_slice( $allModules, 0, 2 ),
1613 array( 'mock1', 'mock2' ) );
1614 try {
1615 $result->setContinueParam( $allModules[1], 'm2continue', 1 );
1616 $this->fail( 'Expected exception not thrown' );
1617 } catch ( UnexpectedValueException $ex ) {
1618 $this->assertSame(
1619 'Module \'mock2\' was not supposed to have been executed, but it was executed anyway',
1620 $ex->getMessage(),
1621 'Expected exception'
1622 );
1623 }
1624 try {
1625 $result->setContinueParam( $allModules[2], 'mlcontinue', 1 );
1626 $this->fail( 'Expected exception not thrown' );
1627 } catch ( UnexpectedValueException $ex ) {
1628 $this->assertSame(
1629 'Module \'mocklist\' called ApiContinuationManager::addContinueParam ' .
1630 'but was not passed to ApiContinuationManager::__construct',
1631 $ex->getMessage(),
1632 'Expected exception'
1633 );
1634 }
1635 $main->setContinuationManager( null );
1636
1637 }
1638
1639 public function testObjectSerialization() {
1640 $arr = array();
1641 ApiResult::setValue( $arr, 'foo', (object)array( 'a' => 1, 'b' => 2 ) );
1642 $this->assertSame( array(
1643 'a' => 1,
1644 'b' => 2,
1645 ApiResult::META_TYPE => 'assoc',
1646 ), $arr['foo'] );
1647
1648 $arr = array();
1649 ApiResult::setValue( $arr, 'foo', new ApiResultTestStringifiableObject() );
1650 $this->assertSame( 'Ok', $arr['foo'] );
1651
1652 $arr = array();
1653 ApiResult::setValue( $arr, 'foo', new ApiResultTestSerializableObject( 'Ok' ) );
1654 $this->assertSame( 'Ok', $arr['foo'] );
1655
1656 try {
1657 $arr = array();
1658 ApiResult::setValue( $arr, 'foo', new ApiResultTestSerializableObject(
1659 new ApiResultTestStringifiableObject()
1660 ) );
1661 $this->fail( 'Expected exception not thrown' );
1662 } catch ( UnexpectedValueException $ex ) {
1663 $this->assertSame(
1664 'ApiResultTestSerializableObject::serializeForApiResult() ' .
1665 'returned an object of class ApiResultTestStringifiableObject',
1666 $ex->getMessage(),
1667 'Expected exception'
1668 );
1669 }
1670
1671 try {
1672 $arr = array();
1673 ApiResult::setValue( $arr, 'foo', new ApiResultTestSerializableObject( NAN ) );
1674 $this->fail( 'Expected exception not thrown' );
1675 } catch ( UnexpectedValueException $ex ) {
1676 $this->assertSame(
1677 'ApiResultTestSerializableObject::serializeForApiResult() ' .
1678 'returned an invalid value: Cannot add non-finite floats to ApiResult',
1679 $ex->getMessage(),
1680 'Expected exception'
1681 );
1682 }
1683
1684 $arr = array();
1685 ApiResult::setValue( $arr, 'foo', new ApiResultTestSerializableObject(
1686 array(
1687 'one' => new ApiResultTestStringifiableObject( '1' ),
1688 'two' => new ApiResultTestSerializableObject( 2 ),
1689 )
1690 ) );
1691 $this->assertSame( array(
1692 'one' => '1',
1693 'two' => 2,
1694 ), $arr['foo'] );
1695 }
1696
1697 }
1698
1699 class ApiResultTestStringifiableObject {
1700 private $ret;
1701
1702 public function __construct( $ret = 'Ok' ) {
1703 $this->ret = $ret;
1704 }
1705
1706 public function __toString() {
1707 return $this->ret;
1708 }
1709 }
1710
1711 class ApiResultTestSerializableObject {
1712 private $ret;
1713
1714 public function __construct( $ret ) {
1715 $this->ret = $ret;
1716 }
1717
1718 public function __toString() {
1719 return "Fail";
1720 }
1721
1722 public function serializeForApiResult() {
1723 return $this->ret;
1724 }
1725 }