Skip to content

Commit eb78f3c

Browse files
committed
Tweak redirect method
1 parent 68760a4 commit eb78f3c

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

UPGRADE.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## Upgrade apps from 2.x to 3.x
2+
- The `redirect()` method no calls `exit()` after sending the content. This is up to the developer now.
3+
14
## Upgrade Gateways from 2.x to 3.x
25

36
The primary difference is the HTTP Client. We are now using HTTPlug (http://httplug.io/) but rely on our own interface.

src/Omnipay/Common/Message/AbstractResponse.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Omnipay\Common\Exception\RuntimeException;
99
use Symfony\Component\HttpFoundation\RedirectResponse as HttpRedirectResponse;
1010
use Symfony\Component\HttpFoundation\Response as HttpResponse;
11-
use Symfony\Component\HttpFoundation\Response;
1211

1312
/**
1413
* Abstract Response
@@ -158,20 +157,46 @@ public function getTransactionId()
158157
return null;
159158
}
160159

160+
/**
161+
* Gets the redirect target url.
162+
*
163+
* @return string
164+
*/
165+
public function getRedirectUrl()
166+
{
167+
return null;
168+
}
169+
170+
/**
171+
* Get the required redirect method (either GET or POST).
172+
*
173+
* @return string
174+
*/
175+
public function getRedirectMethod(){
176+
return 'GET';
177+
}
178+
179+
/**
180+
* Gets the redirect form data array, if the redirect method is POST.
181+
*
182+
* @return array
183+
*/
184+
public function getRedirectData()
185+
{
186+
return [];
187+
}
188+
161189
/**
162190
* Automatically perform any required redirect
163191
*
164192
* This method is meant to be a helper for simple scenarios. If you want to customize the
165193
* redirection page, just call the getRedirectUrl() and getRedirectData() methods directly.
166194
*
167-
* @codeCoverageIgnore
168-
*
169195
* @return void
170196
*/
171197
public function redirect()
172198
{
173199
$this->getRedirectResponse()->send();
174-
exit;
175200
}
176201

177202
/**
@@ -183,7 +208,10 @@ public function getRedirectResponse()
183208
throw new RuntimeException('This response does not support redirection.');
184209
}
185210

186-
/** @var $this RedirectResponseInterface */
211+
if (empty($this->getRedirectUrl())) {
212+
throw new RuntimeException('The given redirectUrl cannot be empty.');
213+
}
214+
187215
if ('GET' === $this->getRedirectMethod()) {
188216
return HttpRedirectResponse::create($this->getRedirectUrl());
189217
} elseif ('POST' === $this->getRedirectMethod()) {

tests/Omnipay/Common/Message/AbstractResponseTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,33 @@ public function testGetRedirectResponseNotSupported()
5555
$this->response->getRedirectResponse();
5656
}
5757

58+
/**
59+
* @expectedException \Omnipay\Common\Exception\RuntimeException
60+
* @expectedExceptionMessage The given redirectUrl cannot be empty.
61+
*/
62+
public function testGetRedirectResponseUrlNotEmpty()
63+
{
64+
$this->response = m::mock('\Omnipay\Common\Message\AbstractResponseTest_MockRedirectResponse')->makePartial();
65+
$this->response->shouldReceive('getRedirectUrl')->once()->andReturn(null);
66+
67+
$this->response->getRedirectResponse();
68+
}
69+
70+
/**
71+
* @runInSeparateProcess
72+
*/
73+
public function testRedirect()
74+
{
75+
$this->response = m::mock('\Omnipay\Common\Message\AbstractResponseTest_MockRedirectResponse')->makePartial();
76+
$this->response->shouldReceive('getRedirectMethod')->andReturn('GET');
77+
78+
ob_start();
79+
$this->response->redirect();
80+
$body = ob_get_clean();
81+
82+
$this->assertContains('Redirecting to https://example.com/redirect?a=1&b=2', $body);
83+
}
84+
5885
public function testGetRedirectResponseGet()
5986
{
6087
$this->response = m::mock('\Omnipay\Common\Message\AbstractResponseTest_MockRedirectResponse')->makePartial();

0 commit comments

Comments
 (0)