thephpleague / omnipay

A framework agnostic, multi-gateway payment processing library for PHP 5.6+
http://omnipay.thephpleague.com/
MIT License
5.92k stars 929 forks source link

Question - PHPUnit Test $response->redirect()? #567

Open Evaske opened 5 years ago

Evaske commented 5 years ago

Hi,

So I have integrated Omnipay Paypal and it redirects successfully but I wanted to write some PHPUnit code to check that a redirect happens but I seem to be having problems working out a way to test it.

My code that I'm trying to test is as follows:

$paypal = new PayPal; 
$response = $paypal->purchase(request()->all(), Cart::total());

if ($response->isRedirect()) {
    $response->redirect();
}

So I'm trying to check for a redirect in PHPUnit but it doesn't seem to be doing one?

$response = $this->followingRedirects()->post(route('cart.pay', $data));
$response->assertRedirect();

And the redirect text just gets dumped in to the unit test as follows:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="refresh" content="0;url=https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&amp;useraction=commit&amp;token=EC-3B700573HD007635J" />

    <title>Redirecting to https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&amp;useraction=commit&amp;token=EC-3B700573HD007635J</title>
</head>
<body>
    Redirecting to <a href="https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&amp;useraction=commit&amp;token=EC-3B700573HD007635J">https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&amp;useraction=commit&amp;token=EC-3B700573HD007635J</a>.
</body>
</html>

Is there a way for me to test that my code is actually redirecting users?

The code in the file AbstractResponse.php for Omnipay calls the following:

public function redirect()
{
    $this->getRedirectResponse()->send();
}

If I do dd($this->getRedirectResponse()->send()); then I can see that there is a redirect there and can see the #targetURL etc but if I do dd($response->redirect()) in my code, it returns null. Tres confused!

Evaske commented 5 years ago

Is there any issue in me using return redirect($response->getRedirectURL()); instead of $response->redirect(); so that I can PHPUnit test my code?