laravel / lumen-framework

The Laravel Lumen Framework.
https://lumen.laravel.com
MIT License
1.47k stars 419 forks source link

Unable to get Json request after a post using phpunit #559

Closed Basquens closed 7 years ago

Basquens commented 7 years ago

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:

public function post(Request $request)
    {
        $data = $request->json()->all();

        dd($data);

        return response()->json([], 201);
    }

TestFile:

    public function testApiRequest()
    {
        $data = [
            'client' => '10001',
        ];

        $this->json('post', '/api/action', $data);

        $this->assertResponseStatus(201);
    }

Composer.json:

"require": {
        "php": ">=7.0.0",
        "laravel/lumen-framework": "5.4.*",
        "vlucas/phpdotenv": "~2.2",
        "jenssegers/mongodb": "^3.0",
        "predis/predis": "^1.0",
        "illuminate/redis": "^5.2",
        "kozz/laravel-guzzle-provider": "~6.0"
    },
"require-dev": {
        "fzaninotto/faker": "~1.4",
        "phpunit/phpunit": "~5.0",
        "mockery/mockery": "~0.9"
    },
brino commented 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!

brino commented 7 years ago

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?

brino commented 7 years ago

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 ...

lzpfmh commented 7 years ago

55 is the same issue, fixed in lumen 5.4.2,please update,thx

MaratowKyryl commented 1 year ago

Facing the same rn with lumen-framework 6.*