Example #1
0
 /**
  * @covers ApiResult
  */
 public function testStaticDataMethods()
 {
     $arr = array();
     ApiResult::setValue($arr, 'setValue', '1');
     ApiResult::setValue($arr, null, 'unnamed 1');
     ApiResult::setValue($arr, null, 'unnamed 2');
     ApiResult::setValue($arr, 'deleteValue', '2');
     ApiResult::unsetValue($arr, 'deleteValue');
     ApiResult::setContentValue($arr, 'setContentValue', '3');
     $this->assertSame(array('setValue' => '1', 'unnamed 1', 'unnamed 2', ApiResult::META_CONTENT => 'setContentValue', 'setContentValue' => '3'), $arr);
     try {
         ApiResult::setValue($arr, 'setValue', '99');
         $this->fail('Expected exception not thrown');
     } catch (RuntimeException $ex) {
         $this->assertSame('Attempting to add element setValue=99, existing value is 1', $ex->getMessage(), 'Expected exception');
     }
     try {
         ApiResult::setContentValue($arr, 'setContentValue2', '99');
         $this->fail('Expected exception not thrown');
     } catch (RuntimeException $ex) {
         $this->assertSame('Attempting to set content element as setContentValue2 when setContentValue ' . 'is already set as the content element', $ex->getMessage(), 'Expected exception');
     }
     ApiResult::setValue($arr, 'setValue', '99', ApiResult::OVERRIDE);
     $this->assertSame('99', $arr['setValue']);
     ApiResult::setContentValue($arr, 'setContentValue2', '99', ApiResult::OVERRIDE);
     $this->assertSame('setContentValue2', $arr[ApiResult::META_CONTENT]);
     $arr = array('foo' => 1, 'bar' => 1);
     ApiResult::setValue($arr, 'top', '2', ApiResult::ADD_ON_TOP);
     ApiResult::setValue($arr, null, '2', ApiResult::ADD_ON_TOP);
     ApiResult::setValue($arr, 'bottom', '2');
     ApiResult::setValue($arr, 'foo', '2', ApiResult::OVERRIDE);
     ApiResult::setValue($arr, 'bar', '2', ApiResult::OVERRIDE | ApiResult::ADD_ON_TOP);
     $this->assertSame(array(0, 'top', 'foo', 'bar', 'bottom'), array_keys($arr));
     $arr = array();
     ApiResult::setValue($arr, 'sub', array('foo' => 1));
     ApiResult::setValue($arr, 'sub', array('bar' => 1));
     $this->assertSame(array('sub' => array('foo' => 1, 'bar' => 1)), $arr);
     try {
         ApiResult::setValue($arr, 'sub', array('foo' => 2, 'baz' => 2));
         $this->fail('Expected exception not thrown');
     } catch (RuntimeException $ex) {
         $this->assertSame('Conflicting keys (foo) when attempting to merge element sub', $ex->getMessage(), 'Expected exception');
     }
     $arr = array();
     $title = Title::newFromText("MediaWiki:Foobar");
     $obj = new stdClass();
     $obj->foo = 1;
     $obj->bar = 2;
     ApiResult::setValue($arr, 'title', $title);
     ApiResult::setValue($arr, 'obj', $obj);
     $this->assertSame(array('title' => (string) $title, 'obj' => array('foo' => 1, 'bar' => 2, ApiResult::META_TYPE => 'assoc')), $arr);
     $fh = tmpfile();
     try {
         ApiResult::setValue($arr, 'file', $fh);
         $this->fail('Expected exception not thrown');
     } catch (InvalidArgumentException $ex) {
         $this->assertSame('Cannot add resource(stream) to ApiResult', $ex->getMessage(), 'Expected exception');
     }
     try {
         ApiResult::setValue($arr, null, $fh);
         $this->fail('Expected exception not thrown');
     } catch (InvalidArgumentException $ex) {
         $this->assertSame('Cannot add resource(stream) to ApiResult', $ex->getMessage(), 'Expected exception');
     }
     try {
         $obj->file = $fh;
         ApiResult::setValue($arr, 'sub', $obj);
         $this->fail('Expected exception not thrown');
     } catch (InvalidArgumentException $ex) {
         $this->assertSame('Cannot add resource(stream) to ApiResult', $ex->getMessage(), 'Expected exception');
     }
     try {
         $obj->file = $fh;
         ApiResult::setValue($arr, null, $obj);
         $this->fail('Expected exception not thrown');
     } catch (InvalidArgumentException $ex) {
         $this->assertSame('Cannot add resource(stream) to ApiResult', $ex->getMessage(), 'Expected exception');
     }
     fclose($fh);
     try {
         ApiResult::setValue($arr, 'inf', INF);
         $this->fail('Expected exception not thrown');
     } catch (InvalidArgumentException $ex) {
         $this->assertSame('Cannot add non-finite floats to ApiResult', $ex->getMessage(), 'Expected exception');
     }
     try {
         ApiResult::setValue($arr, null, INF);
         $this->fail('Expected exception not thrown');
     } catch (InvalidArgumentException $ex) {
         $this->assertSame('Cannot add non-finite floats to ApiResult', $ex->getMessage(), 'Expected exception');
     }
     try {
         ApiResult::setValue($arr, 'nan', NAN);
         $this->fail('Expected exception not thrown');
     } catch (InvalidArgumentException $ex) {
         $this->assertSame('Cannot add non-finite floats to ApiResult', $ex->getMessage(), 'Expected exception');
     }
     try {
         ApiResult::setValue($arr, null, NAN);
         $this->fail('Expected exception not thrown');
     } catch (InvalidArgumentException $ex) {
         $this->assertSame('Cannot add non-finite floats to ApiResult', $ex->getMessage(), 'Expected exception');
     }
     ApiResult::setValue($arr, null, NAN, ApiResult::NO_VALIDATE);
     try {
         ApiResult::setValue($arr, null, NAN, ApiResult::NO_SIZE_CHECK);
         $this->fail('Expected exception not thrown');
     } catch (InvalidArgumentException $ex) {
         $this->assertSame('Cannot add non-finite floats to ApiResult', $ex->getMessage(), 'Expected exception');
     }
     $arr = array();
     $result2 = new ApiResult(8388608);
     $result2->addValue(null, 'foo', 'bar');
     ApiResult::setValue($arr, 'baz', $result2);
     $this->assertSame(array('baz' => array(ApiResult::META_TYPE => 'assoc', 'foo' => 'bar')), $arr);
     $arr = array();
     ApiResult::setValue($arr, 'foo', "foo�bar");
     ApiResult::setValue($arr, 'bar', "á");
     ApiResult::setValue($arr, 'baz', 74);
     ApiResult::setValue($arr, null, "foo�bar");
     ApiResult::setValue($arr, null, "á");
     $this->assertSame(array('foo' => "foo�bar", 'bar' => "á", 'baz' => 74, 0 => "foo�bar", 1 => "á"), $arr);
 }