forked from LiveCarta/PayPal-PHP-SDK
Removed ModelAccessValidator in favor of Forward Compatilibity Issues
- Model Access Validator causes unnecessary issues in existing integrations. - Causes merchant to break integration if configured incorrectly.
This commit is contained in:
@@ -122,7 +122,6 @@ class PayPalModel
|
||||
*/
|
||||
public function __set($key, $value)
|
||||
{
|
||||
ModelAccessorValidator::validate($this, $this->convertToCamelCase($key));
|
||||
if (!is_array($value) && $value === null) {
|
||||
$this->__unset($key);
|
||||
} else {
|
||||
@@ -249,13 +248,7 @@ class PayPalModel
|
||||
|
||||
private function assignValue($key, $value)
|
||||
{
|
||||
// If we find the getter setter, use that, otherwise use magic method.
|
||||
if (ModelAccessorValidator::validate($this, $this->convertToCamelCase($key))) {
|
||||
$setter = "set" . $this->convertToCamelCase($key);
|
||||
$this->$setter($value);
|
||||
} else {
|
||||
$this->__set($key, $value);
|
||||
}
|
||||
$this->__set($key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Validation;
|
||||
|
||||
use PayPal\Common\PayPalModel;
|
||||
use PayPal\Core\PayPalConfigManager;
|
||||
use PayPal\Core\PayPalLoggingManager;
|
||||
|
||||
/**
|
||||
* Class ModelAccessorValidator
|
||||
*
|
||||
* @package PayPal\Validation
|
||||
*/
|
||||
class ModelAccessorValidator
|
||||
{
|
||||
/**
|
||||
* Helper method for validating if the class contains accessor methods (getter and setter) for a given attribute
|
||||
*
|
||||
* @param PayPalModel $class An object of PayPalModel
|
||||
* @param string $attributeName Attribute name
|
||||
* @return bool
|
||||
*/
|
||||
public static function validate(PayPalModel $class, $attributeName)
|
||||
{
|
||||
$mode = PayPalConfigManager::getInstance()->get('validation.level');
|
||||
if (!empty($mode) && $mode != 'disabled') {
|
||||
//Check if $attributeName is string
|
||||
if (gettype($attributeName) !== 'string') {
|
||||
return false;
|
||||
}
|
||||
//If the mode is disabled, bypass the validation
|
||||
foreach (array('set' . $attributeName, 'get' . $attributeName) as $methodName) {
|
||||
if (get_class($class) == get_class(new PayPalModel())) {
|
||||
// Silently return false on cases where you are using PayPalModel instance directly
|
||||
return false;
|
||||
}
|
||||
//Check if both getter and setter exists for given attribute
|
||||
elseif (!method_exists($class, $methodName)) {
|
||||
//Delegate the error based on the choice
|
||||
$className = is_object($class) ? get_class($class) : (string)$class;
|
||||
$errorMessage = "It seems that $className:$methodName is a new field added to the API response. If not, create an issue at https://github.com/paypal/PayPal-PHP-SDK/issues";
|
||||
PayPalLoggingManager::getInstance(__CLASS__)->debug($errorMessage);
|
||||
if ($mode == 'strict') {
|
||||
trigger_error($errorMessage, E_USER_NOTICE);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -75,7 +75,6 @@ function getApiContext($clientId, $clientSecret)
|
||||
'log.LogEnabled' => true,
|
||||
'log.FileName' => '../PayPal.log',
|
||||
'log.LogLevel' => 'DEBUG', // PLEASE USE `FINE` LEVEL FOR LOGGING IN LIVE ENVIRONMENTS
|
||||
'validation.level' => 'log',
|
||||
'cache.enabled' => true,
|
||||
// 'http.CURLOPT_CONNECTTIMEOUT' => 30
|
||||
// 'http.headers.PayPal-Partner-Attribution-Id' => '123123123'
|
||||
|
||||
@@ -43,16 +43,6 @@ log.FileName=../PayPal.log
|
||||
; with a warning message
|
||||
log.LogLevel=INFO
|
||||
|
||||
;Validation Configuration
|
||||
[validation]
|
||||
; If validation is set to strict, the PayPalModel would make sure that
|
||||
; there are proper accessors (Getters and Setters) for each model
|
||||
; objects. Accepted value is
|
||||
; 'log' : logs the error message to logger only (default)
|
||||
; 'strict' : throws a php notice message
|
||||
; 'disable' : disable the validation
|
||||
validation.level=disable
|
||||
|
||||
;Caching Configuration
|
||||
[cache]
|
||||
; If Cache is enabled, it stores the access token retrieved from ClientId and Secret from the
|
||||
|
||||
@@ -209,17 +209,6 @@ class WebhookEventTest extends \PHPUnit_Framework_TestCase
|
||||
WebhookEvent::validateAndGetReceivedEvent('something-invalid', $mockApiContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider mockProvider
|
||||
* @param $mockApiContext ApiContext
|
||||
* @expectedException \PHPUnit_Framework_Error_Notice
|
||||
* @expectedExceptionMessage It seems that PayPal\Api\WebhookEvent:setValid is a new field added to the API response. If not, create an issue at https://github.com/paypal/PayPal-PHP-SDK/issues
|
||||
*/
|
||||
public function testValidateWebhookValidJSONWithMissingObject($obj, $mockApiContext)
|
||||
{
|
||||
WebhookEvent::validateAndGetReceivedEvent('{"valid":"json"}', $mockApiContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider mockProvider
|
||||
* @param $mockApiContext ApiContext
|
||||
|
||||
@@ -86,20 +86,6 @@ class ModelTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
}
|
||||
|
||||
public function testInvalidMagicMethod()
|
||||
{
|
||||
$obj = new SimpleClass();
|
||||
try {
|
||||
$obj->invalid = "value2";
|
||||
$this->assertEquals($obj->invalid, "value2");
|
||||
if (PayPalConfigManager::getInstance()->get('validation.level') == 'strict') {
|
||||
$this->fail("It should have thrown a Notice Error");
|
||||
}
|
||||
} catch (\PHPUnit_Framework_Error_Notice $ex) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test Case to determine if the unknown object is returned, it would not add that object to the model.
|
||||
*/
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
<?php
|
||||
namespace PayPal\Test\Validation;
|
||||
|
||||
use PayPal\Core\PayPalConfigManager;
|
||||
use PayPal\Test\Common\SimpleClass;
|
||||
use PayPal\Validation\ModelAccessorValidator;
|
||||
|
||||
class ModelAccessValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public static function positiveProvider()
|
||||
{
|
||||
return array(
|
||||
array(new SimpleClass(), 'name'),
|
||||
array(new SimpleClass(), 'description')
|
||||
);
|
||||
}
|
||||
|
||||
public static function invalidProvider()
|
||||
{
|
||||
return array(
|
||||
array(null, null,'must be an instance of PayPal\Common\PayPalModel, null given'),
|
||||
array(array(), array() ,'must be an instance of PayPal\Common\PayPalModel, array given'),
|
||||
array(new SimpleClass(), null,'Error'),
|
||||
array(new SimpleClass(), array(),'Error'),
|
||||
array(null, 'name','must be an instance of PayPal\Common\PayPalModel, null given'),
|
||||
array(new SimpleClass(),'notfound', 'It seems that PayPal\Test\Common\SimpleClass:setnotfound is a new field added to the API response')
|
||||
);
|
||||
}
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
PayPalConfigManager::getInstance()->addConfigs(array('validation.level' => 'strict'));
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
PayPalConfigManager::getInstance()->addConfigs(array('validation.level' => 'strict'));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @dataProvider positiveProvider
|
||||
*/
|
||||
public function testValidate($class, $name)
|
||||
{
|
||||
$this->assertTrue(ModelAccessorValidator::validate($class, $name));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @dataProvider invalidProvider
|
||||
*/
|
||||
public function testInvalidValidate($class, $name, $exMessage)
|
||||
{ try {
|
||||
$this->assertFalse(ModelAccessorValidator::validate($class, $name));
|
||||
} catch(\Exception $ex) {
|
||||
$this->assertContains($exMessage, $ex->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user