Skip to content

Commit

Permalink
fixed regression, added more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
schmittjoh committed Aug 15, 2011
1 parent 3fbe506 commit 115dbfb
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Model/PaymentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ interface PaymentInterface
const STATE_EXPIRED = 4;
const STATE_FAILED = 5;
const STATE_NEW = 6;
const STATE_DEPOSITING = 7;
const STATE_DEPOSITED = 8;

function getApprovedAmount();
function getApproveTransaction();
Expand Down
6 changes: 4 additions & 2 deletions PluginController/PluginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,8 @@ protected function doDeposit(PaymentInterface $payment, $amount)

$retry = false;

$transaction = $this->createFinancialTransaction($payment);
$transaction = $this->buildFinancialTransaction();
$transaction->setPayment($payment);
$transaction->setTransactionType(FinancialTransactionInterface::TRANSACTION_TYPE_DEPOSIT);
$transaction->setRequestedAmount($amount);

Expand All @@ -589,7 +590,8 @@ protected function doDeposit(PaymentInterface $payment, $amount)

$retry = false;

$transaction = $this->createFinancialTransaction($payment);
$transaction = $this->buildFinancialTransaction();
$transaction->setPayment($payment);
$transaction->setTransactionType(FinancialTransactionInterface::TRANSACTION_TYPE_DEPOSIT);
$transaction->setRequestedAmount($amount);

Expand Down
75 changes: 75 additions & 0 deletions Tests/PluginController/PluginControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace JMS\Payment\CoreBundle\Tests\PluginController;

use JMS\Payment\CoreBundle\Plugin\Exception\FinancialException;

use JMS\Payment\CoreBundle\Model\CreditInterface;
use JMS\Payment\CoreBundle\Entity\Credit;
use JMS\Payment\CoreBundle\Entity\FinancialTransaction;
Expand Down Expand Up @@ -798,7 +800,72 @@ public function getInvalidInstructionStatesForApproval()
);
}

public function testDeposit()
{
$controller = $this->getController(array(), false);
$controller->addPlugin($plugin = $this->getPlugin());
$plugin
->expects($this->once())
->method('deposit')
;

$controller
->expects($this->once())
->method('buildFinancialTransaction')
->will($this->returnCallback(function() {
$transaction = new FinancialTransaction();
$transaction->setProcessedAmount(123.45);
$transaction->setResponseCode(PluginInterface::RESPONSE_CODE_SUCCESS);
$transaction->setReasonCode(PluginInterface::REASON_CODE_SUCCESS);

return $transaction;
}))
;

$payment = $this->getPayment();
$payment->setState(PaymentInterface::STATE_APPROVED);
$payment->setApprovedAmount(123.45);

$instruction = $payment->getPaymentInstruction();
$instruction->setState(PaymentInstructionInterface::STATE_VALID);
$instruction->setApprovedAmount(10);

$result = $this->callDeposit($controller, array($payment, 123.45));

$this->assertEquals(Result::STATUS_SUCCESS, $result->getStatus(), 'Result status is not success: '.$result->getReasonCode());
$this->assertEquals(123.45, $payment->getDepositedAmount());
$this->assertEquals(123.45, $instruction->getDepositedAmount());
$this->assertEquals(PaymentInterface::STATE_DEPOSITED, $payment->getState());
$this->assertEquals(0, $payment->getDepositingAmount());
$this->assertEquals(0, $instruction->getDepositingAmount());
}

public function testDepositPluginThrowsFinancialException()
{
$controller = $this->getController();
$controller->addPlugin($plugin = $this->getPlugin());
$plugin
->expects($this->once())
->method('deposit')
->will($this->throwException(new FinancialException('some error')))
;

$payment = $this->getPayment();
$payment->setState(PaymentInterface::STATE_APPROVED);
$payment->setApprovedAmount(10);

$instruction = $payment->getPaymentInstruction();
$instruction->setState(PaymentInstruction::STATE_VALID);
$instruction->setApprovedAmount(10);

$result = $this->callDeposit($controller, array($payment, 10));
$this->assertEquals(Result::STATUS_FAILED, $result->getStatus());
$this->assertEquals(0, $payment->getDepositingAmount());
$this->assertEquals(0, $payment->getDepositedAmount());
$this->assertEquals(PaymentInterface::STATE_FAILED, $payment->getState());
$this->assertEquals(0, $instruction->getDepositingAmount());
$this->assertEquals(0, $instruction->getDepositedAmount());
}

protected function getPlugin()
{
Expand Down Expand Up @@ -906,6 +973,14 @@ protected function callApprove(PluginController $controller, array $args)
return $reflection->invokeArgs($controller, $args);
}

protected function callDeposit(PluginController $controller, array $args)
{
$reflection = new \ReflectionMethod($controller, 'doDeposit');
$reflection->setAccessible(true);

return $reflection->invokeArgs($controller, $args);
}

protected function callCredit(PluginController $controller, array $args)
{
$reflection = new \ReflectionMethod($controller, 'doCredit');
Expand Down

0 comments on commit 115dbfb

Please sign in to comment.