Using Named Routes in a Lumen Test


November 20th, 2017


When writing tests in Lumen, I recently discovered that the route() helper doesn’t work with tests out-of-the-box.

I prefer to define named routes and make requests against them in my tests. If you follow the Lumen documentation, the typical way that you make a request for a test looks like this:

1$this->json('GET', '/posts/1')
2 ->seeJson(['title' => 'Hello World']);

Using the route helper, your test might look like this:

1public function testShowingAPost()
3 $this->json('GET', route('', ['id' => 1]))
4 ->seeJson(['title' => "Hello World"]);

If you run the above test, here’s what the URL would return:

2PHPUnit 6.4.4 by Sebastian Bergmann and contributors.
4.F 2 / 2 (100%)
6Time: 48 ms, Memory: 6.00MB
8There was 1 failure:
101) ExampleTest::testShowingAPost
11Invalid JSON was returned from the route. Perhaps an exception was thrown?

If you inspect things a little closer, you can see the issue:

1public function testShowingAPost()
3 $this->json('GET', route('', ['id' => 1]));
4 dd(
5 $this->response->getContent(),
6 route('', ['id' => 1])
7 );
9 $this->seeJson(['title' => "Hello World"]);

Interestingly, the route doesn’t look quite right, and the router is returning the / route:

2PHPUnit 6.4.4 by Sebastian Bergmann and contributors.
4string(40) "Lumen (5.5.2) (Laravel Components 5.5.*)"
5string(16) "http://:/posts/1"

It looks like the localhost part of the request isn’t being set, and the route isn’t matching. We can fix that by bootstrapping the request as Laravel does.

Bootstrapping the Request

When you run tests in the Laravel framework, the project includes CreatesApplication trait that gets called during setup which in turn bootstraps classes for the application. One of those “bootstrappers” is the SetRequestForConsole which bootstraps the request for the console.

We can replicate this functionality so that tests in Lumen have a bootstrapped request on setup, but it requires a little work on our end.

The base TestCase class eventually calls refreshApplication() during setup which in turn calls createApplication() defined in the tests/TestCase.php file.

The createApplication() method returns the application, so we can bootstrap the request at this point before returning the application and assigning it to $this->app.

Here’s what the default base TestCase class looks like that ships with Lumen:

3abstract class TestCase extends Laravel\Lumen\Testing\TestCase
5 /**
6 * Creates the application.
7 *
8 * @return \Laravel\Lumen\Application
9 */
10 public function createApplication()
11 {
12 return require __DIR__.'/../bootstrap/app.php';
13 }

Open the tests/TestCase.php file and update it to the following in order to replicate Laravel’s SetRequestForConsole:

3use Illuminate\Http\Request;
4use Laravel\Lumen\Testing\TestCase as BaseTestCase;
6abstract class TestCase extends BaseTestCase
8 /**
9 * Creates the application.
10 *
11 * @return \Laravel\Lumen\Application
12 */
13 public function createApplication()
14 {
15 $app = require __DIR__.'/../bootstrap/app.php';
17 $uri = $app->make('config')->get('app.url', 'http://localhost');
19 $components = parse_url($uri);
21 $server = $_SERVER;
23 if (isset($components['path'])) {
24 $server = array_merge($server, [
25 'SCRIPT_FILENAME' => $components['path'],
26 'SCRIPT_NAME' => $components['path'],
27 ]);
28 }
30 $app->instance('request', Request::create(
31 $uri, 'GET', [], [], [], $server
32 ));
34 return $app;
35 }

First, we require the application instance by assigning require to the $app variable. Instead of returning it, we create a request instance with a default URL of http://localhost.

When we re-run our tests, you should get the route that you’d expect from the dd() call we tried earlier:

2PHPUnit 6.4.4 by Sebastian Bergmann and contributors.
4.string(23) "{"title":"Hello World"}"
5string(24) "http://localhost/posts/1"

Now the original test using the route() helper will work as expected if you remove the dd() from the test case.


If you want to make the application URL configurable, you could update the application to import a local configuration.

Let’s first create a config folder and copy the app configuration:

1$ mkdir config/
2$ cp vendor/laravel/lumen-framework/config/app.php config/

Next, open the config/app.php file and add the following somewhere to the array:

1'url' => env('APP_URL', 'http://localhost'),

Last, you need to register the configuration file by adding the following line to the bootstrap/app.php file after the application instance is created:


Learn More

Lumen’s testing API is around testing HTTP APIs and thus is slightly different yet familiar. To make Lumen more lightweight than Laravel, there are some differences and tradeoffs made. For example, Lumen uses the FastRoute package for the router instead of the router that ships with Laravel.

I feel that often Lumen is misunderstood as a micro-framework that is more generic, when in fact, it’s a framework for making APIs. Once you stick within those boundaries, Lumen can be a perfect fit for building APIs. I suggest reading over the whole manual, specifically the testing documentation.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.

Laravel News Partners