From 464d46e33ce6910b54eece6f0e8d77182f48418a Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar <kotlyar.maksim@gmail.com> Date: Thu, 15 Sep 2016 15:56:01 +0300 Subject: [PATCH 1/2] Omnipay's Response getData method not always returns array. --- src/Action/OffsiteCaptureAction.php | 7 +- tests/Action/OffsiteCaptureActionTest.php | 126 ++++++++++++++++++++++ 2 files changed, 132 insertions(+), 1 deletion(-) diff --git a/src/Action/OffsiteCaptureAction.php b/src/Action/OffsiteCaptureAction.php index d67e60b..15b394d 100644 --- a/src/Action/OffsiteCaptureAction.php +++ b/src/Action/OffsiteCaptureAction.php @@ -114,7 +114,12 @@ public function execute($request) } } - $details->replace($response->getData()); + if (is_array($response->getData())) { + $details->replace($response->getData()); + } else { + $details['_data'] = $response->getData(); + } + $details['_reference'] = $response->getTransactionReference(); $details['_status'] = $response->isSuccessful() ? 'captured' : 'failed'; $details['_status_code'] = $response->getCode(); diff --git a/tests/Action/OffsiteCaptureActionTest.php b/tests/Action/OffsiteCaptureActionTest.php index b3bc1c6..dce3df0 100644 --- a/tests/Action/OffsiteCaptureActionTest.php +++ b/tests/Action/OffsiteCaptureActionTest.php @@ -442,6 +442,132 @@ public function shouldSetNotifyUrlIfTokenFactoryAndCaptureTokenPresent() $this->assertEquals('theNotifyUrl', $details['notifyUrl']); } + /** + * @test + */ + public function shouldMergeResponseArrayDataWithDetails() + { + $details = new \ArrayObject([ + 'card' => array('cvv' => 123), + 'clientIp' => '', + ]); + + $responseMock = $this->getMock(OmnipayAbstractResponse::class, [], [], '', false); + $responseMock + ->expects($this->any()) + ->method('getData') + ->willReturn([ + 'foo' => 'fooVal', + ]) + ; + + $requestMock = $this->getMock(OmnipayRequestInterface::class); + $requestMock + ->expects($this->any()) + ->method('send') + ->will($this->returnValue($responseMock)) + ; + + $omnipayGateway = $this->getMock(OffsiteGateway::class); + $omnipayGateway + ->expects($this->once()) + ->method('purchase') + ->willReturn($requestMock) + ; + + $captureToken = new Token(); + $captureToken->setTargetUrl('theCaptureUrl'); + $captureToken->setDetails($identity = new Identity('theId', new \stdClass())); + $captureToken->setGatewayName('theGatewayName'); + + $notifyToken = new Token(); + $notifyToken->setTargetUrl('theNotifyUrl'); + + $tokenFactoryMock = $this->getMock(GenericTokenFactoryInterface::class); + $tokenFactoryMock + ->expects($this->once()) + ->method('createNotifyToken') + ->with('theGatewayName', $this->identicalTo($identity)) + ->willReturn($notifyToken) + ; + + $request = new Capture($captureToken); + $request->setModel($details); + + $action = new OffsiteCaptureAction; + $action->setApi($omnipayGateway); + $action->setGateway($this->createGatewayMock()); + $action->setGenericTokenFactory($tokenFactoryMock); + + $action->execute($request); + + $details = (array) $details; + $this->assertArrayHasKey('foo', $details); + $this->assertEquals('fooVal', $details['foo']); + } + + /** + * @test + */ + public function shouldSetResponseStringDataToDetails() + { + $details = new \ArrayObject([ + 'card' => array('cvv' => 123), + 'clientIp' => '', + ]); + + $responseMock = $this->getMock(OmnipayAbstractResponse::class, [], [], '', false); + $responseMock + ->expects($this->any()) + ->method('getData') + ->willReturn('someData') + ; + + $requestMock = $this->getMock(OmnipayRequestInterface::class); + $requestMock + ->expects($this->any()) + ->method('send') + ->will($this->returnValue($responseMock)) + ; + + $omnipayGateway = $this->getMock(OffsiteGateway::class); + $omnipayGateway + ->expects($this->once()) + ->method('purchase') + ->willReturn($requestMock) + ; + + $captureToken = new Token(); + $captureToken->setTargetUrl('theCaptureUrl'); + $captureToken->setDetails($identity = new Identity('theId', new \stdClass())); + $captureToken->setGatewayName('theGatewayName'); + + $notifyToken = new Token(); + $notifyToken->setTargetUrl('theNotifyUrl'); + + $tokenFactoryMock = $this->getMock(GenericTokenFactoryInterface::class); + $tokenFactoryMock + ->expects($this->once()) + ->method('createNotifyToken') + ->with('theGatewayName', $this->identicalTo($identity)) + ->willReturn($notifyToken) + ; + + $request = new Capture($captureToken); + $request->setModel($details); + + $action = new OffsiteCaptureAction; + $action->setApi($omnipayGateway); + $action->setGateway($this->createGatewayMock()); + $action->setGenericTokenFactory($tokenFactoryMock); + + $action->execute($request); + + $details = (array) $details; + $this->assertArrayHasKey('_data', $details); + $this->assertEquals('someData', $details['_data']); + } + /** * @return \PHPUnit_Framework_MockObject_MockObject|GatewayInterface */ From 5b324afaf641c151d38cdf6183e5446a59060993 Mon Sep 17 00:00:00 2001 From: Maksim Kotlyar <kotlyar.maksim@gmail.com> Date: Thu, 15 Sep 2016 16:09:51 +0300 Subject: [PATCH 2/2] fix tests --- src/Action/OffsiteCaptureAction.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Action/OffsiteCaptureAction.php b/src/Action/OffsiteCaptureAction.php index 15b394d..ea35ece 100644 --- a/src/Action/OffsiteCaptureAction.php +++ b/src/Action/OffsiteCaptureAction.php @@ -114,10 +114,11 @@ public function execute($request) } } - if (is_array($response->getData())) { - $details->replace($response->getData()); + $data = $response->getData(); + if (is_array($data)) { + $details->replace($data); } else { - $details['_data'] = $response->getData(); + $details['_data'] = $data; } $details['_reference'] = $response->getTransactionReference();