-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
145 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
namespace Payum\Core\Action; | ||
|
||
use Payum\Core\Bridge\Spl\ArrayObject; | ||
use Payum\Core\Exception\RequestNotSupportedException; | ||
use Payum\Core\Model\OrderInterface; | ||
use Payum\Core\Request\Notify; | ||
|
||
class NotifyOrderAction extends PaymentAwareAction | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
* | ||
* @param Notify $request | ||
*/ | ||
public function execute($request) | ||
{ | ||
RequestNotSupportedException::assertSupports($this, $request); | ||
|
||
/** @var $order OrderInterface */ | ||
$order = $request->getModel(); | ||
|
||
$details = ArrayObject::ensureArrayObject($order->getDetails()); | ||
|
||
try { | ||
$request->setModel($details); | ||
$this->payment->execute($request); | ||
|
||
$order->setDetails($details); | ||
$request->setModel($order); | ||
} catch (\Exception $e) { | ||
$order->setDetails($details); | ||
$request->setModel($order); | ||
|
||
throw $e; | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function supports($request) | ||
{ | ||
return | ||
$request instanceof Notify && | ||
$request->getModel() instanceof OrderInterface | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
namespace Payum\Core\Tests\Action; | ||
|
||
use Payum\Core\Action\NotifyOrderAction; | ||
use Payum\Core\Model\Order; | ||
use Payum\Core\Request\Notify; | ||
use Payum\Core\Request\FillOrderDetails; | ||
use Payum\Core\Request\GetHumanStatus; | ||
use Payum\Core\Tests\GenericActionTest; | ||
|
||
class NotifyOrderActionTest extends GenericActionTest | ||
{ | ||
protected $requestClass = 'Payum\Core\Request\Notify'; | ||
|
||
protected $actionClass = 'Payum\Core\Action\NotifyOrderAction'; | ||
|
||
public function provideSupportedRequests() | ||
{ | ||
$capture = new $this->requestClass($this->getMock('Payum\Security\TokenInterface')); | ||
$capture->setModel($this->getMock('Payum\Core\Model\OrderInterface')); | ||
|
||
return array( | ||
array(new $this->requestClass(new Order)), | ||
array($capture), | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldImplementPaymentAwareInterface() | ||
{ | ||
$rc = new \ReflectionClass('Payum\Core\Action\NotifyOrderAction'); | ||
|
||
$this->assertTrue($rc->implementsInterface('Payum\Core\PaymentAwareInterface')); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldExecuteNotifyDetails() | ||
{ | ||
$details = array('foo' => 'fooVal'); | ||
|
||
$order = new Order; | ||
$order->setDetails($details); | ||
|
||
$testCase = $this; | ||
|
||
$paymentMock = $this->createPaymentMock(); | ||
$paymentMock | ||
->expects($this->once()) | ||
->method('execute') | ||
->with($this->isInstanceOf('Payum\Core\Request\Notify')) | ||
->will($this->returnCallback(function(Notify $request) use ($testCase, $details) { | ||
$testCase->assertInstanceOf('ArrayAccess', $request->getModel()); | ||
$testCase->assertEquals($details, iterator_to_array($request->getModel())); | ||
})) | ||
; | ||
|
||
$action = new NotifyOrderAction; | ||
$action->setPayment($paymentMock); | ||
|
||
$action->execute($notify = new Notify($order)); | ||
|
||
$this->assertSame($order, $notify->getModel()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldKeepOrderEvenIfExceptionThrown() | ||
{ | ||
$details = array('foo' => 'fooVal'); | ||
|
||
$order = new Order; | ||
$order->setDetails($details); | ||
|
||
$paymentMock = $this->createPaymentMock(); | ||
$paymentMock | ||
->expects($this->once()) | ||
->method('execute') | ||
->with($this->isInstanceOf('Payum\Core\Request\Notify')) | ||
->will($this->throwException(new \Exception)) | ||
; | ||
|
||
$action = new NotifyOrderAction; | ||
$action->setPayment($paymentMock); | ||
|
||
$this->setExpectedException('Exception'); | ||
$action->execute($capture = new Notify($order)); | ||
|
||
$this->assertSame($order, $capture->getModel()); | ||
} | ||
} |