Skip to content

Commit

Permalink
[action] add notify order action.
Browse files Browse the repository at this point in the history
  • Loading branch information
makasim committed Nov 20, 2014
1 parent 75c0b04 commit f59a56a
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 1 deletion.
49 changes: 49 additions & 0 deletions Action/NotifyOrderAction.php
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
;
}
}
2 changes: 1 addition & 1 deletion Tests/Action/CaptureOrderActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function provideSupportedRequests()
*/
public function shouldImplementPaymentAwareInterface()
{
$rc = new \ReflectionClass('Payum\Core\Action\PaymentAwareAction');
$rc = new \ReflectionClass('Payum\Core\Action\CaptureOrderAction');

$this->assertTrue($rc->implementsInterface('Payum\Core\PaymentAwareInterface'));
}
Expand Down
95 changes: 95 additions & 0 deletions Tests/Action/NotifyOrderActionTest.php
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());
}
}

0 comments on commit f59a56a

Please sign in to comment.