0

I have a API endpoint that I wish to test, it receives a POST payload. The endpoint basically invokes the Omnipay Sage Pay library to handle a Server Notification.

I can POST to this endpoint using Postman client fine, but when using a Laravel phpunit test e.g.

$response = $this->post($url, $data);

The Omnipay library can't see the post data?

I think this is because the Laravel helper $this->post doesn't use application/x-www-form-urlencoded POST requests? I took a look behind the scenes at $this->post and it seems to invoke the controllers/methods directly which makes sense..

The closest I got to this working was using Guzzle directly (See below), however this routes the request outside of the 'testing' session and back into my local application I'm assuming? This then breaks my tests as I'm setting up some testing data via factories before the POST call.

$response = $client->request('POST', $url, ['form_params' => $data]);

I'm not too sure where the issue lies, either in my tests or the Omnipay library itself?

Snaver
  • 434
  • 3
  • 6
  • If you think it's caused by the missing content-type header `application/x-www-form-urlencoded`, then you could try adding the header to the request and see if that resolves the issue. – Severin Aug 30 '19 at 18:52

1 Answers1

0

Solved it.. Turns out Omnipay allows you to pass your own Request class/object as the third param when invoking the Omnipay object. The library will then have access to the POST params from the test $this->post($url, $data); call.

Laravel's Illuminate\Http\Request is an extension of Symfony\Component\HttpFoundation\Request, so we can pass this straight through.

<?php 

use Illuminate\Http\Request;

class SagePayController extends Controller
{
    protected $gateway;

    protected function setupGateway(Request $request)
    {
        $this->gateway = OmniPay::create('SagePay\Server', null, $request);

        ...
    }

    ...
Snaver
  • 434
  • 3
  • 6