donatj / mock-webserver

Simple mock web server in PHP for unit testing.
MIT License
131 stars 21 forks source link

setResponseOfPath body content #44

Closed GoldraK closed 11 months ago

GoldraK commented 1 year ago

Hello, first of all thanks for the project, it is being very useful to me.

But I want to validate that what my controller sends to the mock is correct, I am using setResponseOfPath. There is a way to recover a body like the basic one, but by customizing the responses.

{
    "_GET": {
        "get": "foobar"
    },
    "_POST": [],
    "_FILES": [],
    "_COOKIE": [],
    "HEADERS": {
        "Host": "127.0.0.1:61355",
        "Connection": "close"
    },
    "METHOD": "GET",
    "INPUT": "",
    "PARSED_INPUT": [],
    "REQUEST_URI": "\/endpoint?get=foobar",
    "PARSED_REQUEST_URI": {
        "path": "\/endpoint",
        "query": "get=foobar"
    }
}

Thanks for your time.

donatj commented 1 year ago

You're just looking for getLastRequest

<?php

use donatj\MockWebServer\Response;

require 'vendor/autoload.php';

$server = new \donatj\MockWebServer\MockWebServer();
$server->start();

$endpoint = $server->setResponseOfPath('/path', new Response('Hello World', [], 200));
file_get_contents($endpoint);

print_r($server->getLastRequest());

It will give you back a RequestInfo object with all of the above.

davidyell commented 1 year ago

..but where is the response body? If we mock the response, I would expect that calling ->getBody()->getContents() would return the configured mock response. Instead you just get meta data about the request, as shown by OP

donatj commented 1 year ago

The response body is in the return value of file_get_contents

donatj commented 11 months ago

It's been almost a year and OP never responded. Closing.