Skip to content
This repository has been archived by the owner on Sep 29, 2023. It is now read-only.

Commit

Permalink
Validation Package Testing
Browse files Browse the repository at this point in the history
- Added More Unit Tests to Validation Classes
- Updated Logic accordingly
  • Loading branch information
japatel committed Jan 9, 2015
1 parent 095ab24 commit f7cfd0f
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 9 deletions.
15 changes: 7 additions & 8 deletions lib/PayPal/Validation/ArgumentValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion lib/PayPal/Validation/JsonValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/PayPal/Validation/ModelAccessorValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public static function validate(PayPalModel $class, $attributeName)
{
$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())) {
Expand Down
51 changes: 51 additions & 0 deletions tests/PayPal/Test/Validation/ArgumentValidatorTest.php
Original file line number Diff line number Diff line change
@@ -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 changes: 57 additions & 0 deletions tests/PayPal/Test/Validation/JsonValidatorTest.php
Original file line number Diff line number Diff line change
@@ -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 changes: 50 additions & 0 deletions tests/PayPal/Test/Validation/ModelAccessValidatorTest.php
Original file line number Diff line number Diff line change
@@ -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());
}
}
}

0 comments on commit f7cfd0f

Please sign in to comment.