laravel / lumen-framework

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

Exception is thrown when calling $request->{route-param} on test mode #881

Closed kanidjar closed 5 years ago

kanidjar commented 5 years ago

Description:

On test mode (phpunit), an exception occurs when calling :

$request->parameter;
// OR
$request->route('parameter');
// OR
$request->route()->getParameter('parameter');
[2019-02-22 14:56:38] testing.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function parameter() on array in vendor\illuminate\http\Request.php:531

See https://github.com/laravel/lumen-framework/issues/685#issuecomment-443393222

kanidjar commented 5 years ago

The error only occurs when running tests.

driesvints commented 5 years ago

Can you provide some code which reproduces this? Perhaps the test itself?

kanidjar commented 5 years ago

Sure!

Routes

<?php

use Illuminate\Http\Request;

$router->get('/{id}', function(Request $request) {
    return $request->id;
});

Test :

<?php

use Laravel\Lumen\Testing\DatabaseMigrations;
use Laravel\Lumen\Testing\DatabaseTransactions;

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {
        $this->get('/1');
        dd($this->response);
    }
}

Throws:

+exception: Symfony\Component\Debug\Exception\FatalThrowableError {#52
    -originalClassName: "Error"
    #message: "Call to a member function parameter() on array"
    #code: 0
    #file: "./vendor/illuminate/http/Request.php"
    #line: 531
    #severity: E_ERROR
    trace: {
      ./vendor/illuminate/http/Request.php:531 { …}
      ./vendor/illuminate/http/Request.php:694 { …}
      ./vendor/illuminate/support/helpers.php:1138 { …}
      ./vendor/illuminate/support/Arr.php:293 { …}
      ./vendor/illuminate/http/Request.php:695 { …}
      ./routes/web.php:17 {
        › $router->get('/{id}', function(Request $request) {
        ›     return $request->id;
        › });
        arguments: {
          $key: "id"
        }
      }
      TestCase->{closure}() {}
      ./vendor/illuminate/container/BoundMethod.php:29 { …}
      ./vendor/illuminate/container/BoundMethod.php:75 { …}
      ./vendor/illuminate/container/BoundMethod.php:31 { …}
      ./vendor/illuminate/container/Container.php:572 { …}
      ./vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php:291 { …}
      ./vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php:265 { …}
      ./vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php:235 { …}
      ./vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php:169 { …}
      ./vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php:418 { …}
      ./vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php:171 { …}
      ./vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php:91 { …}
      ./vendor/laravel/lumen-framework/src/Testing/Concerns/MakesHttpRequests.php:341 { …}
      ./vendor/laravel/lumen-framework/src/Testing/Concerns/MakesHttpRequests.php:63 { …}
      ./tests/ExampleTest.php:15 { …}
      ./vendor/phpunit/phpunit/src/Framework/TestCase.php:1153 { …}
      ./vendor/phpunit/phpunit/src/Framework/TestCase.php:842 { …}
      ./vendor/phpunit/phpunit/src/Framework/TestResult.php:687 { …}
      ./vendor/phpunit/phpunit/src/Framework/TestCase.php:796 { …}
      ./vendor/phpunit/phpunit/src/Framework/TestSuite.php:746 { …}
      ./vendor/phpunit/phpunit/src/Framework/TestSuite.php:746 { …}
      ./vendor/phpunit/phpunit/src/TextUI/TestRunner.php:641 { …}
      ./vendor/phpunit/phpunit/src/TextUI/Command.php:206 { …}
      ./vendor/phpunit/phpunit/src/TextUI/Command.php:162 { …}
      ./vendor/phpunit/phpunit/phpunit:61 { …}
    }
kanidjar commented 5 years ago

screenshot 2019-02-22 at 17 07 26

The route is working when the app is served by the PHP embedded server.

driesvints commented 5 years ago

I can confirm that this is a bug. I've sent in a fix for this.

kanidjar commented 5 years ago

Great! Thanks for your reactivity on this 👍