navihtot's avatar

Controller test - request empty in service provider

I have a service provider which binds a service based on route parameter. Everything works nice but now I'm trying to test my controller, and for some reason request parameters are empty in my service provider :(

Service provider:

    public function register()
    {
        $this->app->call([$this, 'registerServices']);
    }

    public function registerServices(Request $request)
    {
        dd($request); //here request params are empty
        ....
        $this->app->bind...
    }
        

Controller test - tried different methods but my request is always empty:

$response = $this->json('POST', $route, $params);

$response = $this->post($route, $params);

$this->call('POST', $route, $params);

And this is the output of request object

Illuminate\Http\Request {#17
  #json: null
  #convertedFiles: null
  #userResolver: null
  #routeResolver: null
  +attributes: Symfony\Component\HttpFoundation\ParameterBag {#29
    #parameters: []
  }
  +request: Symfony\Component\HttpFoundation\ParameterBag {#370
    #parameters: []
  }
  +query: Symfony\Component\HttpFoundation\ParameterBag {#30
    #parameters: []
  }
  +server: Symfony\Component\HttpFoundation\ServerBag {#36
...
pathInfo: "/"
  requestUri: "/"
  baseUrl: ""
  basePath: ""
  method: "GET"
  format: "html"

Am I doing something wrong here? Why are request params empty here? Also I'm doing a post request and in my dump of request object there is a get method. Whats the reason for this?

0 likes
3 replies
Talinon's avatar

What is the full code for your test?

My immediate suspicion might be that for your test you are first signing in a user, which is why your dump of the Request object is showing 'GET'

jsamaniegog's avatar

2 years ago, i know, but i have the same problem, any solution?

In my code i have this:

public function register()
{
    switch ($this->app->request->notification_type) {
...

It works perfectly, but in testing the request it is empty.

Thanks in advance.

Please or to participate in this conversation.