From 115dbfbeb0c3dca00d9a8b8f7744ec9ec42f66c0 Mon Sep 17 00:00:00 2001 From: Johannes Schmitt Date: Mon, 15 Aug 2011 10:44:25 +0200 Subject: [PATCH] fixed regression, added more unit tests --- Model/PaymentInterface.php | 2 + PluginController/PluginController.php | 6 +- .../PluginController/PluginControllerTest.php | 75 +++++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/Model/PaymentInterface.php b/Model/PaymentInterface.php index 6c313168..58b2c851 100644 --- a/Model/PaymentInterface.php +++ b/Model/PaymentInterface.php @@ -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(); diff --git a/PluginController/PluginController.php b/PluginController/PluginController.php index e2c26abc..b017e915 100644 --- a/PluginController/PluginController.php +++ b/PluginController/PluginController.php @@ -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); @@ -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); diff --git a/Tests/PluginController/PluginControllerTest.php b/Tests/PluginController/PluginControllerTest.php index 4bff1871..d37e53bd 100644 --- a/Tests/PluginController/PluginControllerTest.php +++ b/Tests/PluginController/PluginControllerTest.php @@ -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; @@ -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() { @@ -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');