From f59a56a8d43b446fc3b534ffe7537b35379fc62d Mon Sep 17 00:00:00 2001 From: Kotlyar Maksim Date: Thu, 20 Nov 2014 08:03:15 +0000 Subject: [PATCH] [action] add notify order action. --- Action/NotifyOrderAction.php | 49 +++++++++++++ Tests/Action/CaptureOrderActionTest.php | 2 +- Tests/Action/NotifyOrderActionTest.php | 95 +++++++++++++++++++++++++ 3 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 Action/NotifyOrderAction.php create mode 100644 Tests/Action/NotifyOrderActionTest.php diff --git a/Action/NotifyOrderAction.php b/Action/NotifyOrderAction.php new file mode 100644 index 0000000..6a3119e --- /dev/null +++ b/Action/NotifyOrderAction.php @@ -0,0 +1,49 @@ +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 + ; + } +} diff --git a/Tests/Action/CaptureOrderActionTest.php b/Tests/Action/CaptureOrderActionTest.php index 72765d2..d2a6bf6 100644 --- a/Tests/Action/CaptureOrderActionTest.php +++ b/Tests/Action/CaptureOrderActionTest.php @@ -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')); } diff --git a/Tests/Action/NotifyOrderActionTest.php b/Tests/Action/NotifyOrderActionTest.php new file mode 100644 index 0000000..41e3985 --- /dev/null +++ b/Tests/Action/NotifyOrderActionTest.php @@ -0,0 +1,95 @@ +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()); + } +} \ No newline at end of file