PayPalModel to differentiate between empty objects and array

- Fixes to PayPalModel Conversion
- Fixes #262
This commit is contained in:
Jay Patel
2015-03-03 16:55:18 -06:00
parent 207c2c233e
commit fd6a38d0ef
5 changed files with 101 additions and 6 deletions

View File

@@ -1,6 +1,8 @@
<?php
namespace PayPal\Test\Common;
use PayPal\Api\Payment;
use PayPal\Common\PayPalModel;
use PayPal\Core\PayPalConfigManager;
class ModelTest extends \PHPUnit_Framework_TestCase
@@ -120,6 +122,62 @@ class ModelTest extends \PHPUnit_Framework_TestCase
PayPalConfigManager::getInstance()->addConfigs(array('validation.level' => 'strict'));
}
/**
* Test Case to determine if the unknown object is returned, it would not add that object to the model.
*/
public function testUnknownArrayConversion()
{
PayPalConfigManager::getInstance()->addConfigs(array('validation.level' => 'disabled'));
$json = '{"name":"test","unknown":[{"object": { "id" : "123", "object": "456"}}, {"more": { "id" : "123", "object": "456"}}],"description":"description"}';
$obj = new SimpleClass();
$obj->fromJson($json);
$this->assertEquals("test", $obj->getName());
$this->assertEquals("description", $obj->getDescription());
$resultJson = $obj->toJSON();
$this->assertContains("unknown", $resultJson);
$this->assertContains("id", $resultJson);
$this->assertContains("object", $resultJson);
$this->assertContains("123", $resultJson);
$this->assertContains("456", $resultJson);
PayPalConfigManager::getInstance()->addConfigs(array('validation.level' => 'strict'));
}
public function testEmptyArrayConversion()
{
$json = '{"id":"PAY-5DW86196ER176274EKT3AEYA","transactions":[{"related_resources":[]}]}';
$payment = new Payment($json);
$result = $payment->toJSON();
$this->assertContains('"related_resources":[]', $result);
$this->assertNotNull($result);
}
public function testMultipleEmptyArrayConversion()
{
$json = '{"id":"PAY-5DW86196ER176274EKT3AEYA","transactions":[{"related_resources":[{},{}]}]}';
$payment = new Payment($json);
$result = $payment->toJSON();
$this->assertContains('"related_resources":[{},{}]', $result);
$this->assertNotNull($result);
}
public function testSetterMagicMethod()
{
$obj = new PayPalModel();
$obj->something = "other";
$obj->else = array();
$obj->there = null;
$obj->obj = '{}';
$obj->objs = array('{}');
$this->assertEquals("other", $obj->something);
$this->assertTrue(is_array($obj->else));
$this->assertNull($obj->there);
$this->assertEquals('{}', $obj->obj);
$this->assertTrue(is_array($obj->objs));
$this->assertEquals('{}', $obj->objs[0]);
}
public function testInvalidMagicMethodWithDisabledValidation()
{
PayPalConfigManager::getInstance()->addConfigs(array('validation.level' => 'disabled'));