forked from LiveCarta/PayPal-PHP-SDK
Validation Package Testing
- Added More Unit Tests to Validation Classes - Updated Logic accordingly
This commit is contained in:
@@ -16,18 +16,17 @@ class ArgumentValidator
|
||||
* @param $argument mixed The object to be validated
|
||||
* @param $argumentName string|null The name of the argument.
|
||||
* This will be placed in the exception message for easy reference
|
||||
* @return bool
|
||||
*/
|
||||
public static function validate($argument, $argumentName = null)
|
||||
{
|
||||
if (
|
||||
$argument != null &&
|
||||
((gettype($argument) == 'string' && $argument == '') || is_array($argument) && empty($argument))
|
||||
) {
|
||||
//Throw an Exception for string or array
|
||||
throw new \InvalidArgumentException("$argumentName cannot be null or empty");
|
||||
} elseif ($argument === null) {
|
||||
//Generic Exception
|
||||
if ($argument === null) {
|
||||
// Error if Object Null
|
||||
throw new \InvalidArgumentException("$argumentName cannot be null");
|
||||
} else if (gettype($argument) == 'string' && trim($argument) == ''){
|
||||
// Error if String Empty
|
||||
throw new \InvalidArgumentException("$argumentName string cannot be empty");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ class JsonValidator
|
||||
*/
|
||||
public static function validate($string, $silent = false)
|
||||
{
|
||||
json_decode($string);
|
||||
@json_decode($string);
|
||||
if (json_last_error() != JSON_ERROR_NONE) {
|
||||
if ($silent == false) {
|
||||
//Throw an Exception for string or array
|
||||
|
||||
@@ -24,6 +24,10 @@ class ModelAccessorValidator
|
||||
{
|
||||
$mode = PayPalConfigManager::getInstance()->get('validation.level');
|
||||
if ($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())) {
|
||||
|
||||
51
tests/PayPal/Test/Validation/ArgumentValidatorTest.php
Normal file
51
tests/PayPal/Test/Validation/ArgumentValidatorTest.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
namespace PayPal\Test\Validation;
|
||||
|
||||
use PayPal\Validation\ArgumentValidator;
|
||||
|
||||
class ArgumentValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public static function positiveProvider()
|
||||
{
|
||||
return array(
|
||||
array("1"),
|
||||
array("something here"),
|
||||
array(1),
|
||||
array(array(1,2,3)),
|
||||
array(0.123),
|
||||
array(true),
|
||||
array(false),
|
||||
array(array()),
|
||||
);
|
||||
}
|
||||
|
||||
public static function invalidProvider()
|
||||
{
|
||||
return array(
|
||||
array(null),
|
||||
array(''),
|
||||
array(' ')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @dataProvider positiveProvider
|
||||
*/
|
||||
public function testValidate($input)
|
||||
{
|
||||
$this->assertTrue(ArgumentValidator::validate($input, "Name"));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @dataProvider invalidProvider
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testInvalidDataValidate($input)
|
||||
{
|
||||
$this->assertTrue(ArgumentValidator::validate($input, "Name"));
|
||||
}
|
||||
|
||||
}
|
||||
57
tests/PayPal/Test/Validation/JsonValidatorTest.php
Normal file
57
tests/PayPal/Test/Validation/JsonValidatorTest.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
namespace PayPal\Test\Validation;
|
||||
|
||||
use PayPal\Validation\JsonValidator;
|
||||
|
||||
class JsonValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public static function positiveProvider()
|
||||
{
|
||||
return array(
|
||||
array(null),
|
||||
array(''),
|
||||
array("{}"),
|
||||
array('{"json":"value", "bool":false, "int":1, "float": 0.123, "array": [{"json":"value", "bool":false, "int":1, "float": 0.123},{"json":"value", "bool":false, "int":1, "float": 0.123} ]}')
|
||||
);
|
||||
}
|
||||
|
||||
public static function invalidProvider()
|
||||
{
|
||||
return array(
|
||||
array('{'),
|
||||
array('}'),
|
||||
array(' '),
|
||||
array(array('1' => '23')),
|
||||
array('{"json":"value, "bool":false, "int":1, "float": 0.123, "array": [{"json":"value, "bool":false, "int":1, "float": 0.123}"json":"value, "bool":false, "int":1, "float": 0.123} ]}')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @dataProvider positiveProvider
|
||||
*/
|
||||
public function testValidate($input)
|
||||
{
|
||||
$this->assertTrue(JsonValidator::validate($input));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @dataProvider invalidProvider
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testInvalidJson($input)
|
||||
{
|
||||
JsonValidator::validate($input);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @dataProvider invalidProvider
|
||||
*/
|
||||
public function testInvalidJsonSilent($input)
|
||||
{
|
||||
$this->assertFalse(JsonValidator::validate($input, true));
|
||||
}
|
||||
}
|
||||
50
tests/PayPal/Test/Validation/ModelAccessValidatorTest.php
Normal file
50
tests/PayPal/Test/Validation/ModelAccessValidatorTest.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
namespace PayPal\Test\Validation;
|
||||
|
||||
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', 'Missing Accessor: PayPal\\Test\\Common\\SimpleClass:setnotfound')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @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