forked from LiveCarta/PayPal-PHP-SDK
Sanitize Input for Price Variables
- Updated the model to automatically format the price - Updated the samples to reflect the new changes - More Unit Tests
This commit is contained in:
@@ -18,7 +18,7 @@ class CurrencyTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public static function getJson()
|
||||
{
|
||||
return '{"currency":"TestSample","value":"TestSample"}';
|
||||
return '{"currency":"TestSample","value":"12.34"}';
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
91
tests/PayPal/Test/Common/FormatConverterTest.php
Normal file
91
tests/PayPal/Test/Common/FormatConverterTest.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
namespace PayPal\Test\Common;
|
||||
|
||||
use PayPal\Api\Amount;
|
||||
use PayPal\Api\Currency;
|
||||
use PayPal\Api\Details;
|
||||
use PayPal\Api\Item;
|
||||
use PayPal\Common\FormatConverter;
|
||||
use PayPal\Common\PPModel;
|
||||
use PayPal\Test\Validation\NumericValidatorTest;
|
||||
|
||||
class FormatConverterTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public static function classMethodListProvider(){
|
||||
return array(
|
||||
array(new Item(), 'Price'),
|
||||
array(new Item(), 'Tax'),
|
||||
array(new Amount(), 'Total'),
|
||||
array(new Currency(), 'Value'),
|
||||
array(new Details(), 'Shipping'),
|
||||
array(new Details(), 'SubTotal'),
|
||||
array(new Details(), 'Tax'),
|
||||
array(new Details(), 'Fee'),
|
||||
array(new Details(), 'ShippingDiscount'),
|
||||
array(new Details(), 'Insurance'),
|
||||
array(new Details(), 'HandlingFee'),
|
||||
array(new Details(), 'GiftWrap'),
|
||||
);
|
||||
}
|
||||
|
||||
public static function apiModelSettersProvider()
|
||||
{
|
||||
$provider = array();
|
||||
foreach (NumericValidatorTest::positiveProvider() as $value) {
|
||||
foreach (self::classMethodListProvider() as $method) {
|
||||
$provider[] = array_merge($method, array($value));
|
||||
}
|
||||
}
|
||||
return $provider;
|
||||
}
|
||||
|
||||
public static function apiModelSettersInvalidProvider()
|
||||
{
|
||||
$provider = array();
|
||||
foreach (NumericValidatorTest::invalidProvider() as $value) {
|
||||
foreach (self::classMethodListProvider() as $method) {
|
||||
$provider[] = array_merge($method, array($value));
|
||||
}
|
||||
}
|
||||
return $provider;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @dataProvider \PayPal\Test\Validation\NumericValidatorTest::positiveProvider
|
||||
*/
|
||||
public function testFormatToTwoDecimalPlaces($input, $expected)
|
||||
{
|
||||
$result = FormatConverter::formatToTwoDecimalPlaces($input);
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider apiModelSettersProvider
|
||||
*
|
||||
* @param PPModel $class Class Object
|
||||
* @param string $method Method Name where the format is being applied
|
||||
* @param array $values array of ['input', 'expectedResponse'] is provided
|
||||
*/
|
||||
public function testSettersOfKnownApiModel($class, $method, $values)
|
||||
{
|
||||
$obj = new $class();
|
||||
$setter = "set" . $method;
|
||||
$getter = "get" . $method;
|
||||
$result = $obj->$setter($values[0]);
|
||||
$this->assertEquals($values[1], $result->$getter());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider apiModelSettersInvalidProvider
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testSettersOfKnownApiModelInvalid($class, $methodName, $values)
|
||||
{
|
||||
$obj = new $class();
|
||||
$setter = "set" . $methodName;
|
||||
$obj->$setter($values[0]);
|
||||
}
|
||||
}
|
||||
61
tests/PayPal/Test/Validation/NumericValidatorTest.php
Normal file
61
tests/PayPal/Test/Validation/NumericValidatorTest.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
namespace PayPal\Test\Validation;
|
||||
|
||||
use PayPal\Common\FormatConverter;
|
||||
use PayPal\Validation\NumericValidator;
|
||||
|
||||
class NumericValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public static function positiveProvider()
|
||||
{
|
||||
return array(
|
||||
array("0", "0.00"),
|
||||
array(null, null),
|
||||
array("01", "1.00"),
|
||||
array("01.1", "1.10"),
|
||||
array("10.0", "10.00"),
|
||||
array("0.0", "0.00"),
|
||||
array("00.00", "0.00"),
|
||||
array("000.111", "0.11"),
|
||||
array("000.0001", "0.00"),
|
||||
array("-0.001", "0.00"),
|
||||
array("-0", "0.00"),
|
||||
array("-00.00", "0.00"),
|
||||
array("-10.00", "-10.00"),
|
||||
array("", null),
|
||||
array(" ", null),
|
||||
array(1.20, "1.20")
|
||||
);
|
||||
}
|
||||
|
||||
public static function invalidProvider()
|
||||
{
|
||||
return array(
|
||||
array("01.j"),
|
||||
array("j.10"),
|
||||
array("empty"),
|
||||
array("null")
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @dataProvider positiveProvider
|
||||
*/
|
||||
public function testValidate($input)
|
||||
{
|
||||
$this->assertTrue(NumericValidator::validate($input, "Test Value"));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @dataProvider invalidProvider
|
||||
* @expectedException \InvalidArgumentException
|
||||
*/
|
||||
public function testValidateException($input)
|
||||
{
|
||||
NumericValidator::validate($input, "Test Value");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user