Subdomain testing in Laravel

Standard

Learning about automated testing and implementing is great. Unfortunately some projects I work on are multi tenant, and each client has it’s own subdomain. I then have an “app” subdomain with separate routes on it for things like API, or switching tenants if you belong to multiple of them. You may find a few articles that say to just use $this->call() instead of $this->get() or $this->post(). It really is that simple.

I use a domain like lvh.me which points to 127.0.0.1 along with it’s subdomains.

Here is my setup:

App: app.lvh.me
Tenant App Example 1: test1.lvh.me
Tenant App Example 2: my-company.lvh.me

Example routes.php file

//app domain routes
$router->group(['domain' => 'app.lvh.me'], function($router) {
    $router->post('register', 'App\RegisterController@store');
});
 
//tenant routes
$router->group(['domain' => '{account}.lvh.me'], function($router) {
    $router->post('login', 'Tenant\LoginController@store');
});

To test the login route for the Tenant App Example 1 we do:

$this->call('POST', 'http://test1.lvh.me', ['email' => 'test@example.com', 'password' => 'mysecretpassword']);

To login to Tenant App Example 2, we would do:

$this->call('POST', 'http://my-company.lvh.me', ['email' => 'test@mycompany.com', 'password' => 'mycompanypassword']);

For routes on our App domain we do:

$this->call('GET', 'http://app.lvh.me/register');