From 6c68d02ce575428145ee389150cd3a6f4b9bcba1 Mon Sep 17 00:00:00 2001 From: monofone Date: Wed, 8 Jan 2014 23:03:19 +0100 Subject: [PATCH] changed handling of transfer amounts as this lead ton an erro #28 Added test for bcmath in case the provided amount is a float --- .../BaseTransferInformation.php | 7 +- phpunit.xml.dist | 8 +- tests/Unit/SumOutputTest.php | 144 ++++++++++++++++++ 3 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 tests/Unit/SumOutputTest.php diff --git a/lib/Digitick/Sepa/TransferInformation/BaseTransferInformation.php b/lib/Digitick/Sepa/TransferInformation/BaseTransferInformation.php index 2b82df8..8c8068b 100644 --- a/lib/Digitick/Sepa/TransferInformation/BaseTransferInformation.php +++ b/lib/Digitick/Sepa/TransferInformation/BaseTransferInformation.php @@ -23,6 +23,7 @@ namespace Digitick\Sepa\TransferInformation; use Digitick\Sepa\DomBuilder\DomBuilderInterface; +use Digitick\Sepa\Exception\InvalidArgumentException; use Digitick\Sepa\Util\StringHelper; class BaseTransferInformation implements TransferInformationInterface @@ -84,7 +85,11 @@ public function __construct($amount, $iban, $name) { $amount += 0; if (is_float($amount)) { - $amount = (integer)($amount * 100); + if(!function_exists('bcscale')) { + throw new InvalidArgumentException('Using floats for amount is only possible with bcmath enabled'); + } + bcscale(2); + $amount = (integer)bcmul($amount, 100); } $this->transferAmount = $amount; $this->iban = $iban; diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 4bc87ee..7f12fe1 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,6 +1,12 @@ - + diff --git a/tests/Unit/SumOutputTest.php b/tests/Unit/SumOutputTest.php new file mode 100644 index 0000000..aa9f27a --- /dev/null +++ b/tests/Unit/SumOutputTest.php @@ -0,0 +1,144 @@ +addPaymentInfo( + 'firstPayment', + array( + 'id' => 'firstPayment', + 'creditorName' => 'My Company', + 'creditorAccountIBAN' => 'FI1350001540000056', + 'creditorAgentBIC' => 'PSSTFRPPMON', + 'seqType' => PaymentInformation::S_ONEOFF, + 'creditorId' => 'DE21WVM1234567890' + ) + ); + // Add a Single Transaction to the named payment + $directDebit->addTransfer( + 'firstPayment', + array( + 'amount' => $amount, + 'debtorIban' => 'FI1350001540000056', + 'debtorBic' => 'OKOYFIHH', + 'debtorName' => 'Their Company', + 'debtorMandate' => 'AB12345', + 'debtorMandateSignDate' => '13.10.2012', + 'remittanceInformation' => 'Purpose of this direct debit' + ) + ); + // Retrieve the resulting XML + $xml = $directDebit->asXML(); + $doc = new \DOMDocument(); + $doc->loadXML($xml); + $this->directDebitXpath = new \DOMXPath($doc); + $this->directDebitXpath->registerNamespace('sepa', 'urn:iso:std:iso:20022:tech:xsd:pain.008.002.02'); + } + + /** + * @test + */ + public function validSumIsCalculatedCorrectly() + { + $this->createDirectDebitXpathObject('19.99'); + $controlSum = $this->directDebitXpath->query('//sepa:GrpHdr/sepa:CtrlSum'); + $this->assertEquals('19.99', $controlSum->item(0)->textContent, 'GroupHeader ControlSum should be 19.99'); + + $controlSum = $this->directDebitXpath->query('//sepa:PmtInf/sepa:CtrlSum'); + $this->assertEquals( + '19.99', + $controlSum->item(0)->textContent, + 'PaymentInformation ControlSum should be 19.99' + ); + $controlSum = $this->directDebitXpath->query('//sepa:DrctDbtTxInf/sepa:InstdAmt'); + $this->assertEquals( + '19.99', + $controlSum->item(0)->textContent, + 'DirectDebitTransferInformation InstructedAmount should be 19.99' + ); + } + + /** + * @test + */ + public function floatSumIsCalculatedCorrectly() + { + $this->createDirectDebitXpathObject('19.999'); + $controlSum = $this->directDebitXpath->query('//sepa:GrpHdr/sepa:CtrlSum'); + $this->assertEquals('19.99', $controlSum->item(0)->textContent, 'GroupHeader ControlSum should be 19.99'); + + $controlSum = $this->directDebitXpath->query('//sepa:PmtInf/sepa:CtrlSum'); + $this->assertEquals( + '19.99', + $controlSum->item(0)->textContent, + 'PaymentInformation ControlSum should be 19.99' + ); + $controlSum = $this->directDebitXpath->query('//sepa:DrctDbtTxInf/sepa:InstdAmt'); + $this->assertEquals( + '19.99', + $controlSum->item(0)->textContent, + 'DirectDebitTransferInformation InstructedAmount should be 19.99' + ); + } + + /** + * @test + */ + public function floatsAreAcceptedIfBcMathExtensionIsAvailable() + { + if(!function_exists('bcscale')) { + $this->markTestSkipped('no bcmath extension available'); + } + $transfer = new CustomerDirectDebitTransferInformation( + '19.999', + 'IbanOfDebitor', + 'DebitorName' + ); + + $this->assertEquals(1999, $transfer->getTransferAmount()); + } + + /** + * @test + * @expectedException \Digitick\Sepa\Exception\InvalidArgumentException + */ + public function exceptionIsThrownIfBcMathExtensionIsNotAvailableAndInputIsFloat() + { + if(function_exists('bcscale')) { + $this->markTestSkipped('bcmath extension available, not possible to test exceptions'); + } + $transfer = new CustomerDirectDebitTransferInformation( + '19.999', + 'IbanOfDebitor', + 'DebitorName' + ); + + $this->assertEquals(1999, $transfer->getTransferAmount()); + } +}