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