Ejemplo n.º 1
0
 public function testObjectSerialization()
 {
     $arr = array();
     ApiResult::setValue($arr, 'foo', (object) array('a' => 1, 'b' => 2));
     $this->assertSame(array('a' => 1, 'b' => 2, ApiResult::META_TYPE => 'assoc'), $arr['foo']);
     $arr = array();
     ApiResult::setValue($arr, 'foo', new ApiResultTestStringifiableObject());
     $this->assertSame('Ok', $arr['foo']);
     $arr = array();
     ApiResult::setValue($arr, 'foo', new ApiResultTestSerializableObject('Ok'));
     $this->assertSame('Ok', $arr['foo']);
     try {
         $arr = array();
         ApiResult::setValue($arr, 'foo', new ApiResultTestSerializableObject(new ApiResultTestStringifiableObject()));
         $this->fail('Expected exception not thrown');
     } catch (UnexpectedValueException $ex) {
         $this->assertSame('ApiResultTestSerializableObject::serializeForApiResult() ' . 'returned an object of class ApiResultTestStringifiableObject', $ex->getMessage(), 'Expected exception');
     }
     try {
         $arr = array();
         ApiResult::setValue($arr, 'foo', new ApiResultTestSerializableObject(NAN));
         $this->fail('Expected exception not thrown');
     } catch (UnexpectedValueException $ex) {
         $this->assertSame('ApiResultTestSerializableObject::serializeForApiResult() ' . 'returned an invalid value: Cannot add non-finite floats to ApiResult', $ex->getMessage(), 'Expected exception');
     }
     $arr = array();
     ApiResult::setValue($arr, 'foo', new ApiResultTestSerializableObject(array('one' => new ApiResultTestStringifiableObject('1'), 'two' => new ApiResultTestSerializableObject(2))));
     $this->assertSame(array('one' => '1', 'two' => 2), $arr['foo']);
 }