Closed Basquens closed 7 years ago
I believe I am having the exact same issue ... the endpoint works just fine via Curl or Postman request. But during the unit tests, NO post data is received by the controller method!
Definitely Having the same problem ... here is my test
public function testRetrievePlaces()
{
$this->json('POST','/places', ['id' => ['ChIJMzs0CvBYwokRNqTIhiLV_8M']])
->seeJson(['id'=>'ChIJMzs0CvBYwokRNqTIhiLV_8M']);
}
And here is my controller method...
/**
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function places(Request $request)
{
if($request->has('id')) {
$ids = collect($request->input('id'));
} else {
return response()->json(['error' => 'Json Payload must contain id array i.e. {id:[]}'],400);
}
if($ids->count()) {
try {
$places = Place::whereIn('id',$ids)->get();
} catch(\Exception $e) {
$reason = $e->getMessage();
}
if($places->count()) {
return $places;
}
}
$data = ['error' => 'No Places Found','status'=>500];
if(!empty($reason)) {
$data['reason'] = $reason;
}
return response()->json($data,500);
}
When I run the test... I see my error message saying you didn't provide any inputs
There was 1 failure:
1) PlaceTest::testRetrievePlaces
Unable to find JSON fragment ["id":"ChIJMzs0CvBYwokRNqTIhiLV_8M"] within [{"error":"Json Payload must contain id array i.e. {id:[]}"}].
Failed asserting that false is true.
/home/vagrant/Code/geoloc/vendor/laravel/lumen-framework/src/Testing/Concerns/MakesHttpRequests.php:284
/home/vagrant/Code/geoloc/vendor/laravel/lumen-framework/src/Testing/Concerns/MakesHttpRequests.php:209
/home/vagrant/Code/geoloc/tests/PlaceTest.php:23
But Like I said ... if I run the request through a HTTP Client, it works just fine!
I traced the request through json(), then call(), then handle(), then dispatch() in RoutesRequests.php. In dispatch() I don't see where it does anything with the content of the request like here:
list($method, $pathInfo) = $this->parseIncomingRequest($request);
dispatch() then uses $method and $pathInfo ... and does nothing with the rest of the request?? I'm no phpunit expert ... but it seems like this might have something to do with it?
Weird ... I downgraded to 5.3, and the test worked ... then upgraded to 5.4 and NOW IT WORKS!
Not sure what the deal is ... but it must have been something stupid that I was doing ...
Facing the same rn with lumen-framework 6.*
I'm attempting to test a brand new controller in lumen, it'a suposed to receive only a json input. If I use "call" or "json" function in my test file I'm unable to retrieve the data from the post.
I already tried to make the request via Chrome Postman and in this case I had no problems at all.
I'm using Lumen 5.4.1 with phpunit 5.7.7, if i downgrade it to lumen 5.3.3 and phpunit 4.8.34 the same code works just fine.
Here are the code to simulate the issue:
Controller:
TestFile:
Composer.json: