Skip to content

Commit

Permalink
[payment] inject api and payment to action on execute,
Browse files Browse the repository at this point in the history
it was on action add method call before.
  • Loading branch information
makasim committed Sep 29, 2014
1 parent 1e8966b commit 3b3a9d3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 30 deletions.
44 changes: 22 additions & 22 deletions Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,6 @@ public function addApi($api, $forcePrepend = false)
*/
public function addAction(ActionInterface $action, $forcePrepend = false)
{
if ($action instanceof PaymentAwareInterface) {
$action->setPayment($this);
}

if ($action instanceof ApiAwareInterface) {
$apiSet = false;
foreach ($this->apis as $api) {
try {
$action->setApi($api);
$apiSet = true;
break;
} catch (UnsupportedApiException $e) {}
}

if (false == $apiSet) {
throw new LogicException(sprintf(
'Cannot find right api supported by %s',
get_class($action)
));
}
}

$forcePrepend ?
array_unshift($this->actions, $action) :
array_push($this->actions, $action)
Expand Down Expand Up @@ -132,6 +110,28 @@ protected function findActionSupported($request)
{
foreach ($this->actions as $action) {
if ($action->supports($request)) {
if ($action instanceof PaymentAwareInterface) {
$action->setPayment($this);
}

if ($action instanceof ApiAwareInterface) {
$apiSet = false;
foreach ($this->apis as $api) {
try {
$action->setApi($api);
$apiSet = true;
break;
} catch (UnsupportedApiException $e) {}
}

if (false == $apiSet) {
throw new LogicException(sprintf(
'Cannot find right api supported by %s',
get_class($action)
));
}
}

return $action;
}
}
Expand Down
44 changes: 36 additions & 8 deletions Tests/PaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function shouldAllowAddApiWithPrependForced()
/**
* @test
*/
public function shouldSetFirstApiToActionApiAware()
public function shouldSetFirstApiToActionApiAwareOnExecute()
{
$payment = new Payment();

Expand All @@ -140,18 +140,25 @@ public function shouldSetFirstApiToActionApiAware()

$action = $this->getMockForAbstractClass('Payum\Core\Tests\ApiAwareAction');
$action
->expects($this->once())
->expects($this->at(0))
->method('supports')
->will($this->returnValue(true))
;
$action
->expects($this->at(1))
->method('setApi')
->with($this->identicalTo($firstApi))
;

$payment->addAction($action);

$payment->execute(new \stdClass);
}

/**
* @test
*/
public function shouldSetSecondApiToActionApiAwareIfFirstUnsupported()
public function shouldSetSecondApiToActionApiAwareIfFirstUnsupportedOnExecute()
{
$payment = new Payment();

Expand All @@ -161,17 +168,24 @@ public function shouldSetSecondApiToActionApiAwareIfFirstUnsupported()
$action = $this->getMockForAbstractClass('Payum\Core\Tests\ApiAwareAction');
$action
->expects($this->at(0))
->method('supports')
->will($this->returnValue(true))
;
$action
->expects($this->at(1))
->method('setApi')
->with($this->identicalTo($firstApi))
->will($this->throwException(new UnsupportedApiException('first api not supported')))
;
$action
->expects($this->at(1))
->expects($this->at(2))
->method('setApi')
->with($this->identicalTo($secondApi))
;

$payment->addAction($action);

$payment->execute(new \stdClass);
}

/**
Expand All @@ -180,7 +194,7 @@ public function shouldSetSecondApiToActionApiAwareIfFirstUnsupported()
* @expectedException \Payum\Core\Exception\LogicException
* @expectedExceptionMessage Cannot find right api supported by
*/
public function throwIfPaymentNotHaveApiSupportedByAction()
public function throwIfPaymentNotHaveApiSupportedByActionOnExecute()
{
$payment = new Payment();

Expand All @@ -190,18 +204,25 @@ public function throwIfPaymentNotHaveApiSupportedByAction()
$action = $this->getMockForAbstractClass('Payum\Core\Tests\ApiAwareAction');
$action
->expects($this->at(0))
->method('supports')
->will($this->returnValue(true))
;
$action
->expects($this->at(1))
->method('setApi')
->with($this->identicalTo($firstApi))
->will($this->throwException(new UnsupportedApiException('first api not supported')))
;
$action
->expects($this->at(1))
->expects($this->at(2))
->method('setApi')
->with($this->identicalTo($secondApi))
->will($this->throwException(new UnsupportedApiException('second api not supported')))
;

$payment->addAction($action);

$payment->execute(new \stdClass);
}

/**
Expand Down Expand Up @@ -301,18 +322,25 @@ public function shouldNotCatchReplyByDefault()
/**
* @test
*/
public function shouldSetPaymentToActionIfActionAwareOfPayment()
public function shouldSetPaymentToActionIfActionAwareOfPaymentOnExecute()
{
$payment = new Payment();

$actionMock = $this->getMock('Payum\Core\Action\PaymentAwareAction');
$actionMock
->expects($this->once())
->expects($this->at(0))
->method('supports')
->will($this->returnValue(true))
;
$actionMock
->expects($this->at(1))
->method('setPayment')
->with($this->identicalTo($payment))
;

$payment->addAction($actionMock);

$payment->execute(new \stdClass);
}

/**
Expand Down

0 comments on commit 3b3a9d3

Please sign in to comment.