Laravel 5.2 Codeception functional test issue with PUT / PATCH requests

流过昼夜 提交于 2019-12-11 01:47:12

问题


I have a problem on the update treatment processed into my functional tests on Codeception : I systematically have a 404 error. Here are the details.

Everything is going well for every other simulated http requests but when codeception tries to execute the update method on a put HTTP request (POST request with a "_method" param having the value "PUT") is never executed by my controller, which lead codeception on the update url without the redirection that should happen after the treatment in this update method. I tried to redirect to my home page on top of my update method to test that fact and the redirection never happens.

For information, I use the Laravel5 module. Here is my functional.suite.yml file content :

class_name: FunctionalTester
modules:
    enabled:
        - Asserts
        - \Helper\Functional
        - MailCatcher
        - Laravel5:
            environment_file: .env.testing
    config:
        MailCatcher:
            url: 'http://192.168.10.10'
            port: '1080'

I tried to replace the "PUT" value of the "_method" param by "PATCH" to see if any change could be seen but the problem persists and the behaviour is still the same.

Codeception includes the Laravel5 app to test it. The only clue I have is that the testing environment has a problem to interpret the PUT or PATCH request. There is no problem for a simple POST request (creation doesn't cause any problem).

I precise that the HTML form is correct, the update happens correctly in my local environment and the http request contains correct params. Here is what I have when I execute it the with the --debug option :

[Uri] http://project/en/permissions/update
[Method] POST
[Parameters] {"_token":"RUx7DjU3b6GEjodnpwXvJJYJIcmQJGbabj23q0yK","_method":"PATCH","_id":"1","name_en":"Administrator","name_fr":"Administrateur","slug":"admin"}
[Page] http://project/en/permissions/update
[Response] 200
[Request Cookies] {"XSRF-TOKEN":"eyJpdiI6InZQV2NVcTRoZHVONXYzZzNLTnBWU1E9PSIsInZhbHVlIjoiWWhWa0kyUGxJNkJRTXIyaEhVcDdHR0tRcklHZStpVWdlTjlDdmRKVmEyVDFPWkxBVmhLc1lra05zeWh1ZWtKMENCc29lWFZTN2lSd3dIbjZyNEo5eWc9PSIsIm1hYyI6ImEzMDNmOWM5OGQzNzE4ZWI5MDg0MTI0ZmQwMTI1ZTk0OTM1OTY4NjA5ZTZjMGFhYTI0MTdlMzMzM2QyMWQ4MzUifQ==","laravel_session":"eyJpdiI6ImF4cVFYYVNUU3J0WUd2VzNRZlhSc3c9PSIsInZhbHVlIjoibDdPd3ZEZVZOdDJwRlBjMVZtc2dNM0I3WUw0REEzK25NVFVWT1FIRjEzR05tRGZLXC9SYUZkRmhEdXlyQVdybURHTWVQVUtucnBkZEwwaTN4NWF6XC9YQT09IiwibWFjIjoiMzliODY4ZWUwYmZjODI1OTVkMTBiYjA4ODY2OWNiODc3ZTI1NzAzZmJhMjg4OTY4Y2MzM2VkMjYyYTkwOTQ2MyJ9"}
[Response Headers] {"cache-control":["no-cache"],"Set-Cookie":[{},{}]}

As you can see, the process end on a 200 response and that is all.. I tested it with a few pages and the problem is the same everywhere.

I'm blocked on this problem for weeks now without finding any solution. If someone has a clue, I am listening !.

 
EDIT 1 : 26/01/2015
As it has been asked to me, here is my routes file.
As you can see, I use LaravelLocalization, this is a multilingual app.
I only show you the permissions routes but they are all managed this way.

Route::group([
'prefix'     => LaravelLocalization::setLocale(),
'middleware' => [
    'auth',
    'localize',
    'localeSessionRedirect',
    'localizationRedirect',
]], function () {

// permissions
Route::get(LaravelLocalization::transRoute('routes.permissions.index'), ['as' => 'permissions.index', 'uses' => 'User\PermissionsController@index']);
Route::get(LaravelLocalization::transRoute('routes.permissions.create'), ['as' => 'permissions.create', 'uses' => 'User\PermissionsController@create']);
Route::post(LaravelLocalization::transRoute('routes.permissions.store'), ['as' => 'permissions.store', 'uses' => 'User\PermissionsController@store']);
Route::get(LaravelLocalization::transRoute('routes.permissions.edit'), ['as' => 'permissions.edit', 'uses' => 'User\PermissionsController@edit']);
Route::put(LaravelLocalization::transRoute('routes.permissions.update'), ['as' => 'permissions.update', 'uses' => 'User\PermissionsController@update']);
Route::delete(LaravelLocalization::transRoute('routes.permissions.destroy'), ['as' => 'permissions.destroy', 'uses' => 'User\PermissionsController@destroy']); 

});

 
EDIT 2 : 29/01/2015
Following the Lerzenit solution explained here, I tried to place the following code into the file tests/_bootstrap.php :

Request::enableHttpMethodParameterOverride();

It had no effect, I still have a 200 response and the update method from my controller is never reached.
I checked if the $httpMethodParameterOverride param was passed to true just after the method execution and could verify that is has been successful set to true.
I am still looking for a solution.

 
EDIT 3 : 05/02/2015
I have a clue on an investigation way : I succeeded to execute an update on a entity named 'District' and this is the only one on my project to have the same route in english and in french. As I specified earlier, I use the LaravelLocalization plugin and the routes translation may be the cause of the bad interpretation of the _method param with the PUTvalue that should redirect toward the update method of my controller.

I would be grateful if someone could help me on this.


回答1:


I finally found the solution !

The update method was never reached because, on my POST requests, I sometimes passed the id of the resource into the request by using hidden variables as _id for example.

Laravel expect an id to be given on the post UPDATEor DELETErequests after a slash, that is why the UPDATE / DELETE methods were never reached. In fact, this is a bad practice to pass params in the request on PUT / PATCH / DELETE requests because the targeted resource must be explicitly shown in the url.

So, if you encounter the same problem, be sure you precise a correct POST route in your form as route('users.update', ['id' => $user->id])for example.

Pfiuuu, I now can write my tests in order to have a 100% tested app. That was a painful investigation ;)



来源:https://stackoverflow.com/questions/34990335/laravel-5-2-codeception-functional-test-issue-with-put-patch-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!