Skip to content

Commit

Permalink
Merge pull request #29 from monofone/master
Browse files Browse the repository at this point in the history
changed handling of transfer amounts as this lead to an error #28
  • Loading branch information
ianare committed Jan 13, 2014
2 parents f37fe2b + 6c68d02 commit 3463ded
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="./tests/bootstrap.php" color="true">
<phpunit bootstrap="./tests/bootstrap.php"
colors="true"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
verbose="true">

<testsuites>
<testsuite name="PHP Sepa XML">
Expand Down
144 changes: 144 additions & 0 deletions tests/Unit/SumOutputTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php
/**
* Created by JetBrains PhpStorm.
* User: srohweder
* Date: 1/8/14
* Time: 10:28 PM
* To change this template use File | Settings | File Templates.
*/

namespace Tests\Unit;


use Digitick\Sepa\Exception\InvalidArgumentException;
use Digitick\Sepa\PaymentInformation;
use Digitick\Sepa\TransferFile\Factory\TransferFileFacadeFactory;
use Digitick\Sepa\TransferInformation\CustomerDirectDebitTransferInformation;

class SumOutputTest extends \PHPUnit_Framework_TestCase
{

/**
* @var \DOMXPath
*/
protected $directDebitXpath;

protected function createDirectDebitXpathObject($amount)
{
$directDebit = TransferFileFacadeFactory::createDirectDebit('test123', 'Me');

// create a payment, it's possible to create multiple payments,
// "firstPayment" is the identifier for the transactions
$directDebit->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());
}
}

0 comments on commit 3463ded

Please sign in to comment.