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:
@@ -155,6 +155,7 @@ PayPal\Validation\ModelAccessorValidator: WARNING: Missing Accessor: PayPal\Api\
|
||||
## Contributing
|
||||
|
||||
* If you find solution to an [issue/improvements](https://github.com/paypal/PayPal-PHP-SDK/issues) in sdk that would be helpful to everyone, feel free to send us a pull request.
|
||||
* The best help we could get from everyone is in writing more and more samples. We have a limited set of samples, and would appreciate if the community can help us write more and more of those, covering corner cases, that may be extremely useful to anyone using this SDK.
|
||||
* The ideal approach to create a fix would be to fork the repository, create a branch in your repository, and make a pull request out of it.
|
||||
* It is desirable if there is enough comments/documentation and Tests included in the pull request.
|
||||
* For general idea of contribution, please follow the guidelines mentioned [here](https://guides.github.com/activities/contributing-to-open-source/).
|
||||
|
||||
@@ -4,6 +4,8 @@ namespace PayPal\Api;
|
||||
|
||||
use PayPal\Common\PPModel;
|
||||
use PayPal\Rest\ApiContext;
|
||||
use PayPal\Validation\NumericValidator;
|
||||
use PayPal\Common\FormatConverter;
|
||||
|
||||
/**
|
||||
* Class Amount
|
||||
@@ -46,12 +48,14 @@ class Amount extends PPModel
|
||||
* Total amount charged from the Payer account (or card) to Payee. In case of a refund, this is the refunded amount to the original Payer from Payee account.
|
||||
*
|
||||
*
|
||||
* @param string $total
|
||||
* @param string|double $total
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setTotal($total)
|
||||
{
|
||||
NumericValidator::validate($total, "Total");
|
||||
$total = FormatConverter::formatToTwoDecimalPlaces($total);
|
||||
$this->total = $total;
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
namespace PayPal\Api;
|
||||
|
||||
use PayPal\Common\PPModel;
|
||||
use PayPal\Common\FormatConverter;
|
||||
use PayPal\Validation\NumericValidator;
|
||||
|
||||
/**
|
||||
* Class Currency
|
||||
@@ -42,12 +44,14 @@ class Currency extends PPModel
|
||||
/**
|
||||
* amount up to N digit after the decimals separator as defined in ISO 4217 for the appropriate currency code.
|
||||
*
|
||||
* @param string $value
|
||||
* @param string|double $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setValue($value)
|
||||
{
|
||||
NumericValidator::validate($value, "Value");
|
||||
$value = FormatConverter::formatToTwoDecimalPlaces($value);
|
||||
$this->value = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ namespace PayPal\Api;
|
||||
|
||||
use PayPal\Common\PPModel;
|
||||
use PayPal\Rest\ApiContext;
|
||||
use PayPal\Validation\NumericValidator;
|
||||
use PayPal\Common\FormatConverter;
|
||||
|
||||
/**
|
||||
* Class Details
|
||||
@@ -27,12 +29,14 @@ class Details extends PPModel
|
||||
* Amount being charged for shipping.
|
||||
*
|
||||
*
|
||||
* @param string $shipping
|
||||
* @param string|double $shipping
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setShipping($shipping)
|
||||
{
|
||||
NumericValidator::validate($shipping, "Shipping");
|
||||
$shipping = FormatConverter::formatToTwoDecimalPlaces($shipping);
|
||||
$this->shipping = $shipping;
|
||||
return $this;
|
||||
}
|
||||
@@ -51,12 +55,14 @@ class Details extends PPModel
|
||||
* Sub-total (amount) of items being paid for.
|
||||
*
|
||||
*
|
||||
* @param string $subtotal
|
||||
* @param string|double $subtotal
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setSubtotal($subtotal)
|
||||
{
|
||||
NumericValidator::validate($subtotal, "SubTotal");
|
||||
$subtotal = FormatConverter::formatToTwoDecimalPlaces($subtotal);
|
||||
$this->subtotal = $subtotal;
|
||||
return $this;
|
||||
}
|
||||
@@ -68,6 +74,7 @@ class Details extends PPModel
|
||||
*/
|
||||
public function getSubtotal()
|
||||
{
|
||||
|
||||
return $this->subtotal;
|
||||
}
|
||||
|
||||
@@ -75,12 +82,14 @@ class Details extends PPModel
|
||||
* Amount being charged as tax.
|
||||
*
|
||||
*
|
||||
* @param string $tax
|
||||
* @param string|double $tax
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setTax($tax)
|
||||
{
|
||||
NumericValidator::validate($tax, "Tax");
|
||||
$tax = FormatConverter::formatToTwoDecimalPlaces($tax);
|
||||
$this->tax = $tax;
|
||||
return $this;
|
||||
}
|
||||
@@ -99,12 +108,14 @@ class Details extends PPModel
|
||||
* Fee charged by PayPal. In case of a refund, this is the fee amount refunded to the original receipient of the payment.
|
||||
*
|
||||
*
|
||||
* @param string $fee
|
||||
* @param string|double $fee
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setFee($fee)
|
||||
{
|
||||
NumericValidator::validate($fee, "Fee");
|
||||
$fee = FormatConverter::formatToTwoDecimalPlaces($fee);
|
||||
$this->fee = $fee;
|
||||
return $this;
|
||||
}
|
||||
@@ -123,12 +134,14 @@ class Details extends PPModel
|
||||
* Amount being charged as shipping discount.
|
||||
*
|
||||
*
|
||||
* @param string $shipping_discount
|
||||
* @param string|double $shipping_discount
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setShippingDiscount($shipping_discount)
|
||||
{
|
||||
NumericValidator::validate($shipping_discount, "Shipping Discount");
|
||||
$shipping_discount = FormatConverter::formatToTwoDecimalPlaces($shipping_discount);
|
||||
$this->shipping_discount = $shipping_discount;
|
||||
return $this;
|
||||
}
|
||||
@@ -172,12 +185,14 @@ class Details extends PPModel
|
||||
* Amount being charged as insurance.
|
||||
*
|
||||
*
|
||||
* @param string $insurance
|
||||
* @param string|double $insurance
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setInsurance($insurance)
|
||||
{
|
||||
NumericValidator::validate($insurance, "Insurance");
|
||||
$insurance = FormatConverter::formatToTwoDecimalPlaces($insurance);
|
||||
$this->insurance = $insurance;
|
||||
return $this;
|
||||
}
|
||||
@@ -196,12 +211,14 @@ class Details extends PPModel
|
||||
* Amount being charged as handling fee.
|
||||
*
|
||||
*
|
||||
* @param string $handling_fee
|
||||
* @param string|double $handling_fee
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setHandlingFee($handling_fee)
|
||||
{
|
||||
NumericValidator::validate($handling_fee, "Handling Fee");
|
||||
$handling_fee = FormatConverter::formatToTwoDecimalPlaces($handling_fee);
|
||||
$this->handling_fee = $handling_fee;
|
||||
return $this;
|
||||
}
|
||||
@@ -244,13 +261,14 @@ class Details extends PPModel
|
||||
/**
|
||||
* Amount being charged as gift wrap fee.
|
||||
*
|
||||
*
|
||||
* @param string $gift_wrap
|
||||
* @param string|double $gift_wrap
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setGiftWrap($gift_wrap)
|
||||
{
|
||||
NumericValidator::validate($gift_wrap, "Gift Wrap");
|
||||
$gift_wrap = FormatConverter::formatToTwoDecimalPlaces($gift_wrap);
|
||||
$this->gift_wrap = $gift_wrap;
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ namespace PayPal\Api;
|
||||
use PayPal\Common\PPModel;
|
||||
use PayPal\Rest\ApiContext;
|
||||
use PayPal\Validation\UrlValidator;
|
||||
use PayPal\Validation\NumericValidator;
|
||||
use PayPal\Common\FormatConverter;
|
||||
|
||||
/**
|
||||
* Class Item
|
||||
@@ -103,12 +105,15 @@ class Item extends PPModel
|
||||
* Cost of the item.
|
||||
*
|
||||
*
|
||||
* @param string $price
|
||||
* @param double $price
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setPrice($price)
|
||||
{
|
||||
|
||||
NumericValidator::validate($price, "Price");
|
||||
$price = FormatConverter::formatToTwoDecimalPlaces($price);
|
||||
$this->price = $price;
|
||||
return $this;
|
||||
}
|
||||
@@ -127,12 +132,14 @@ class Item extends PPModel
|
||||
* tax of the item.
|
||||
*
|
||||
*
|
||||
* @param string $tax
|
||||
* @param double $tax
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setTax($tax)
|
||||
{
|
||||
NumericValidator::validate($tax, "Tax");
|
||||
$tax = FormatConverter::formatToTwoDecimalPlaces($tax);
|
||||
$this->tax = $tax;
|
||||
return $this;
|
||||
}
|
||||
|
||||
34
lib/PayPal/Common/FormatConverter.php
Normal file
34
lib/PayPal/Common/FormatConverter.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Common;
|
||||
|
||||
class FormatConverter {
|
||||
|
||||
const TWO_DECIMAL_PLACES = '%0.2f';
|
||||
|
||||
/**
|
||||
* Format the data based on the input formatter value
|
||||
*
|
||||
* @param $value
|
||||
* @param $formatter
|
||||
* @return string
|
||||
*/
|
||||
public static function format($value, $formatter)
|
||||
{
|
||||
return sprintf($formatter, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format the input data to two decimal places
|
||||
*
|
||||
* @param $value
|
||||
* @return string
|
||||
*/
|
||||
public static function formatToTwoDecimalPlaces($value)
|
||||
{
|
||||
if (trim($value) != null) {
|
||||
return static::format($value, self::TWO_DECIMAL_PLACES);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -21,11 +21,7 @@ class ArgumentValidator
|
||||
{
|
||||
if (
|
||||
$argument != null &&
|
||||
(
|
||||
(gettype($argument) == 'string' && $argument == '')
|
||||
||
|
||||
is_array($argument) && empty($argument)
|
||||
)
|
||||
((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");
|
||||
|
||||
28
lib/PayPal/Validation/NumericValidator.php
Normal file
28
lib/PayPal/Validation/NumericValidator.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace PayPal\Validation;
|
||||
|
||||
/**
|
||||
* Class NumericValidator
|
||||
*
|
||||
* @package PayPal\Validation
|
||||
*/
|
||||
class NumericValidator
|
||||
{
|
||||
|
||||
/**
|
||||
* Helper method for validating an argument if it is numeric
|
||||
*
|
||||
* @param mixed $argument
|
||||
* @param string|null $argumentName
|
||||
* @return bool
|
||||
*/
|
||||
public static function validate($argument, $argumentName = null)
|
||||
{
|
||||
if (trim($argument) != null && !is_numeric($argument)) {
|
||||
throw new \InvalidArgumentException("$argumentName is not a valid numeric value");
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -33,12 +33,12 @@ $paymentDefinition->setName('Regular Payments')
|
||||
->setFrequency('Month')
|
||||
->setFrequencyInterval("2")
|
||||
->setCycles("12")
|
||||
->setAmount(new Currency(['value' => '100', 'currency' => 'USD']));
|
||||
->setAmount(new Currency(['value' => 100, 'currency' => 'USD']));
|
||||
|
||||
// Charge Models
|
||||
$chargeModel = new ChargeModel();
|
||||
$chargeModel->setType('SHIPPING')
|
||||
->setAmount(new Currency(['value' => '10', 'currency' => 'USD']));
|
||||
->setAmount(new Currency(['value' => 10, 'currency' => 'USD']));
|
||||
|
||||
$paymentDefinition->setChargeModels(array($chargeModel));
|
||||
|
||||
@@ -49,7 +49,7 @@ $merchantPreferences->setReturnUrl("$baseUrl/ExecuteAgreement.php?success=true")
|
||||
->setAutoBillAmount("yes")
|
||||
->setInitialFailAmountAction("CONTINUE")
|
||||
->setMaxFailAttempts("0")
|
||||
->setSetupFee(new Currency(['value' => '1', 'currency' => 'USD']));
|
||||
->setSetupFee(new Currency(['value' => 1, 'currency' => 'USD']));
|
||||
|
||||
|
||||
$plan->setPaymentDefinitions(array($paymentDefinition));
|
||||
|
||||
@@ -21,7 +21,7 @@ try {
|
||||
|
||||
$amt = new Amount();
|
||||
$amt->setCurrency("USD")
|
||||
->setTotal("1.00");
|
||||
->setTotal(1);
|
||||
|
||||
### Capture
|
||||
$capture = new Capture();
|
||||
|
||||
@@ -44,7 +44,7 @@ $payer->setPaymentMethod("credit_card")
|
||||
|
||||
$amount = new Amount();
|
||||
$amount->setCurrency("USD")
|
||||
->setTotal("1.00");
|
||||
->setTotal(1);
|
||||
|
||||
$transaction = new Transaction();
|
||||
$transaction->setAmount($amount)
|
||||
|
||||
@@ -54,15 +54,15 @@ $item1->setName('Ground Coffee 40 oz')
|
||||
->setDescription('Ground Coffee 40 oz')
|
||||
->setCurrency('USD')
|
||||
->setQuantity(1)
|
||||
->setTax('0.30')
|
||||
->setPrice('7.50');
|
||||
->setTax(0.3)
|
||||
->setPrice(7.50);
|
||||
$item2 = new Item();
|
||||
$item2->setName('Granola bars')
|
||||
->setDescription('Granola Bars with Peanuts')
|
||||
->setCurrency('USD')
|
||||
->setQuantity(5)
|
||||
->setTax('0.20')
|
||||
->setPrice('2.00');
|
||||
->setTax(0.2)
|
||||
->setPrice(2);
|
||||
|
||||
$itemList = new ItemList();
|
||||
$itemList->setItems(array($item1, $item2));
|
||||
@@ -72,9 +72,9 @@ $itemList->setItems(array($item1, $item2));
|
||||
// payment information such as tax, shipping
|
||||
// charges etc.
|
||||
$details = new Details();
|
||||
$details->setShipping('1.20')
|
||||
->setTax('1.30')
|
||||
->setSubtotal('17.50');
|
||||
$details->setShipping(1.2)
|
||||
->setTax(1.3)
|
||||
->setSubtotal(17.5);
|
||||
|
||||
// ### Amount
|
||||
// Lets you specify a payment amount.
|
||||
@@ -82,7 +82,7 @@ $details->setShipping('1.20')
|
||||
// such as shipping, tax.
|
||||
$amount = new Amount();
|
||||
$amount->setCurrency("USD")
|
||||
->setTotal("20.00")
|
||||
->setTotal(20)
|
||||
->setDetails($details);
|
||||
|
||||
// ### Transaction
|
||||
|
||||
@@ -29,12 +29,12 @@ $item1 = new Item();
|
||||
$item1->setName('Ground Coffee 40 oz')
|
||||
->setCurrency('USD')
|
||||
->setQuantity(1)
|
||||
->setPrice('7.50');
|
||||
->setPrice(7.5);
|
||||
$item2 = new Item();
|
||||
$item2->setName('Granola bars')
|
||||
->setCurrency('USD')
|
||||
->setQuantity(5)
|
||||
->setPrice('2.00');
|
||||
->setPrice(2);
|
||||
|
||||
$itemList = new ItemList();
|
||||
$itemList->setItems(array($item1, $item2));
|
||||
@@ -44,9 +44,9 @@ $itemList->setItems(array($item1, $item2));
|
||||
// payment information such as tax, shipping
|
||||
// charges etc.
|
||||
$details = new Details();
|
||||
$details->setShipping('1.20')
|
||||
->setTax('1.30')
|
||||
->setSubtotal('17.50');
|
||||
$details->setShipping(1.2)
|
||||
->setTax(1.3)
|
||||
->setSubtotal(17.50);
|
||||
|
||||
// ### Amount
|
||||
// Lets you specify a payment amount.
|
||||
@@ -54,7 +54,7 @@ $details->setShipping('1.20')
|
||||
// such as shipping, tax.
|
||||
$amount = new Amount();
|
||||
$amount->setCurrency("USD")
|
||||
->setTotal("20.00")
|
||||
->setTotal(20)
|
||||
->setDetails($details);
|
||||
|
||||
// ### Transaction
|
||||
|
||||
@@ -46,12 +46,12 @@ $item1 = new Item();
|
||||
$item1->setName('Ground Coffee 40 oz')
|
||||
->setCurrency('USD')
|
||||
->setQuantity(1)
|
||||
->setPrice('7.50');
|
||||
->setPrice(7.5);
|
||||
$item2 = new Item();
|
||||
$item2->setName('Granola bars')
|
||||
->setCurrency('USD')
|
||||
->setQuantity(5)
|
||||
->setPrice('2.00');
|
||||
->setPrice(2);
|
||||
|
||||
$itemList = new ItemList();
|
||||
$itemList->setItems(array($item1, $item2));
|
||||
@@ -61,9 +61,9 @@ $itemList->setItems(array($item1, $item2));
|
||||
// payment information such as tax, shipping
|
||||
// charges etc.
|
||||
$details = new Details();
|
||||
$details->setShipping('1.20')
|
||||
->setTax('1.30')
|
||||
->setSubtotal('17.50');
|
||||
$details->setShipping(1.2)
|
||||
->setTax(1.3)
|
||||
->setSubtotal(17.5);
|
||||
|
||||
// ### Amount
|
||||
// Lets you specify a payment amount.
|
||||
@@ -71,7 +71,7 @@ $details->setShipping('1.20')
|
||||
// such as shipping, tax.
|
||||
$amount = new Amount();
|
||||
$amount->setCurrency("USD")
|
||||
->setTotal("20.00")
|
||||
->setTotal(20)
|
||||
->setDetails($details);
|
||||
|
||||
// ### Transaction
|
||||
|
||||
@@ -20,7 +20,7 @@ try {
|
||||
|
||||
$amount = new Amount();
|
||||
$amount->setCurrency("USD");
|
||||
$amount->setTotal("1.00");
|
||||
$amount->setTotal(1);
|
||||
|
||||
// ### Reauthorize with amount being reauthorized
|
||||
$authorization->setAmount($amount);
|
||||
|
||||
@@ -20,7 +20,7 @@ use PayPal\Api\Sale;
|
||||
// field to mention fees refund details.
|
||||
$amt = new Amount();
|
||||
$amt->setCurrency('USD')
|
||||
->setTotal('0.01');
|
||||
->setTotal(0.01);
|
||||
|
||||
// ### Refund object
|
||||
$refund = new Refund();
|
||||
|
||||
@@ -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