lumen-framework: [Bug] Request parameters lost when unit testing
Laravel\Lumen\Testing\ApplicationTrait doesn’t seem to be working as expected\intended. In summary:
- Hitting routes via browser/curl works as expected, with request parameters being present.
- Hitting routes via unit testing methods does not work as expected, with request parameters being empty.
It seems I’m not alone with this:
- http://laravel.io/forum/04-19-2015-lumen-requestall-is-empty-when-using-this-call-in-unit-test
- https://laracasts.com/discuss/channels/testing/lumen-unit-testing
- http://stackoverflow.com/questions/29826362/testing-a-post-request-with-specific-parameter-in-lumen
- https://laracasts.com/discuss/channels/general-discussion/i-cant-test-a-post-request-in-lumen?page=1
Code:
routes.php:
$app->get('/', ['as' => 'getIndex', function() use ($app)
{
$request = $app->make(\Illuminate\Http\Request::class);
dd($request->all());
}]);
FooTest.php
public function test_foo_bar()
{
$response = $this->call('GET', '/', ['foo' => 'bar']);
}
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 50 (11 by maintainers)
I’m not sure why but putting
app(Request::class);before the test is run seems to resolve the problem:Hello, I have same problem using latest lumen version.
My code in test:
both header and query params are lost. can you help me?
Hi, Lumen 5.4.7 affected. Same issue with both json and post IF you’re type checking against anything other than \Illuminate\Http\Request in your controller
I have the same problem with query parameters. (Lumen 5.3)
I’m using Lumen 5.2.9 and in my tests i’m also experiencing the problem that my query parameters get lost.
$getResponse = $this->call( 'GET', '/v1/users/me?include=matches', [], [], [], $this->token );and also when using the call like this$getResponse = $this->call( 'GET', '/v1/users/me', ['includes' => 'matches'], [], [], $this->token );I’m using replace request params in TestCase.php
@cdarken since this issue is 4 years old I think it’s better to open a new issue with new steps of replication on a fresh laravel 5.6 app.
This issue shouldn’t be closed. It persists to this day in the most recent version of Lumen.
This appears to be an ongoing issue even within Laravel 5.5.
Although, in my instance, I may have narrowed the problem down to the fact that phpunit isn’t actually performing any http requests and is instead calling parts of the system directly. (Please don’t quote me on that though).
I came across this when I noticed the below block being skipped over for phpunit tests:
I have this check on almost every single one of my controllers to prevent artisan (route:list springs to mind straight away) from picking up __constructs() in it’s calls.
Downside to this is that checking php_sapi_name() as per
\Illuminate\Foundation\Application::runningInConsole()shows “cli” which of course isn’t overly unique. I’m not sure if there is a clean way to determine if phpunit is the calling process but this will most likely solve plenty of issues in my code.
I’m hoping this helps anybody (or somebody) narrow down the issue as it’s rather frustrating at present.
Same issue here with Lumen 5.5.2, only when testing:
Same issue with Lumen 5.5.2, type hinting child classes from
Illuminate\Http\Requestlooses content, params, etc while testing API.Opened another bug ticket: https://github.com/laravel/lumen-framework/issues/694
So after countless hours of debugging it turns out I narrowed the issue down to user error. I generated a blank project, recreated the controller by using
php artisan make:controllerthen adding all the same methods and lo and behold it worked. So I diffed the two controllers.My controller had the following line at the top:
The fresh one made by Laravel had:
As soon as I changed my controller to use the correct casing the reference resolved properly. This was a valuable lesson for sure, and I’m not sure how this was never caught before.
@jobnomade Did you ever find a workaround for this?
Thanks. Would be good to see if anyone is working on this. I mean it is a lightweight framework to build microservices. And we can’t even test a request on the console?
@jobnomade The last ticket about this issue, that i can find, is from december 2017.
I certainly seem to be experiencing similar issues to this. I’ve explained in more detail at https://stackoverflow.com/questions/49856295/laravel-phpunit-test-with-json-request-loses-important-request-details
I really don’t understand how this can be happening without breaking considerably more of my code.
Solved the same issue - in my case problem was with test data because json_encode returned FALSE.
You can see this function in the MakesHttpRequests.php:
And the first thing it’s doing is json_encode array with testing data to be posted. So I fixed my test scenario data so that json_encode is no more FALSE and now tests works fine.
@lzpfmh Still noticing the issue in 5.4.4
The issue is that
MakesHttpRequests::calldoes not create a request usingcreateFromBasewhich would load the json params. #577 fixes it.Same goes for Lumen 5.4.0. The request doesn’t seem to have anything bound to it. When I try to test an API endpoint with the following testcase:
The request seems to be empty inside the actual controller.
same here. query parameters lost, header parameters lost, POST end up in GET, … (lumen 5.3.3)