danyal14's avatar

Lumen PHPUnit lost post data

Hi Guys,

When running following test, in controller action $request->json() get nothing, even $request contains nothing.

// Test
$this->post('/api/breakingnews', ['hello' => 'world'], [
            'Content-Type' => 'application/json',
            'X-Requested-With' => 'XMLHttpRequest'
        ]);

        $this->get('/api/breakingnews');
        $this->seeStatusCode(StatusCode::HTTP_CREATED);

// Controller
 /**
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(Request $request)
    {

        $payload = $request->json();
        dd($payload);
    }


Any idea?, I found some old post related to Lumen 5.3 and looks like issue was fixed in 5.4 but I am getting same issue.

 In controller action dd(get_class($request)); has different request namespace 

When Testing
"Illuminate\Http\Request"
Illuminate\Http\Request {#107
  #json: Symfony\Component\HttpFoundation\ParameterBag {#99
    #parameters: []
  }
  #convertedFiles: null

In PostMan
"Laravel\Lumen\Http\Request"
Request {#45
  #json: ParameterBag {#28
    #parameters: array:5 [
      "name" => "Hell of new article title :)"
      "link" => "https://google.dk"
      "expired_at" => "2019-02-27"
      "user_id" => 12
    ]
  }
0 likes
3 replies
dele's avatar
dele
Best Answer
Level 2

You are calling both the post and get request after each other? Any reason why? I also dont think you need to add all those headers except you really need to. Content-type for example is not necessary because laravel will handle that for you. Remove the get call and make the post call like so

$this->post('/api/breakingnews', ['hello' => 'world']);

OR

$this->json('post', '/api/breakingnews', ['hello' => 'world']);

either will work.

and in your controller:

dd($request->all());

also make sure you declare this for the Request

use Illuminate\Http\Request;
1 like

Please or to participate in this conversation.