Skip to content

Commit

Permalink
Merge pull request #27 from makasim/omnipay-get-data-not-always-array
Browse files Browse the repository at this point in the history
Omnipay's Response getData method not always returns array.
  • Loading branch information
makasim authored Sep 15, 2016
2 parents c5364d2 + 5b324af commit 8a95c77
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Action/OffsiteCaptureAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,13 @@ public function execute($request)
}
}

$details->replace($response->getData());
$data = $response->getData();
if (is_array($data)) {
$details->replace($data);
} else {
$details['_data'] = $data;
}

$details['_reference'] = $response->getTransactionReference();
$details['_status'] = $response->isSuccessful() ? 'captured' : 'failed';
$details['_status_code'] = $response->getCode();
Expand Down
126 changes: 126 additions & 0 deletions tests/Action/OffsiteCaptureActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 8a95c77

Please sign in to comment.