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 eg
$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?
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);
...
}
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.