Search code examples
laraveltestingphpunitmulti-tenantlaravel-7

Setting up testing in Laravel project using Hyn/multi-tenant


I am using the Hyn/multi-tenant 5.6 package for multi tenant application support. My application is working fine, I can create tenants and they are running propperly.

The problem comes with testing. I want to setup an existing tenant for my testing purpose. But somehow the application can't create the correct routes during testing. In CreateApplication.php I'm setting the tenant used for testing. Which has website_id = 1

trait CreatesApplication
{
    /**
     * Creates the application.
     *
     * @return \Illuminate\Foundation\Application
     */
    public function createApplication()
    {
        $app = require __DIR__.'/../bootstrap/app.php';

        $app->make(Kernel::class)->bootstrap();

        return $app;
    }

    public function setUp() : void{

        parent::setUp();

        $hostname = Hostname::find(1);
        $website = Website::findOrFail($hostname->website_id);
        $tenancy = app(Environment::class);
        $tenancy->hostname($hostname);

        Artisan::call('optimize:clear');
        Artisan::call('tenancy:migrate:fresh', ['--website_id' => 1]);
        Artisan::call('tenancy:db:seed',['--class' => 'TenantSeeder','--website_id' => 1]);

    }

}

I found some similar problems by googling but none of their solutions worked for me: https://github.com/tenancy/multi-tenant/issues/584

All my routes are in my tenants.php file and working. I am using the latest version of PHPUNIT.

The error I get is:

Symfony\Component\Routing\Exception\RouteNotFoundException : Route [new_anmeldung_single] not defined.


Solution

  • As I was writing this question, I tried some more options, added and removed some code (I at least did 10h of it the last week) and to my surprise I found the solution.

    I simply forgot to do:

    $tenancy->tenant($website); // switches the tenant and reconfigures the app (new RouteProvider(app()))->boot();//reloads the routes

    So if you want to setup an existing tenant for your testing you will need to add the following piece of code into your setUp() method. If you want to create a new tenant you can follow the example in the issue posted in the question.

    public function setUp() : void{
    
            parent::setUp();
    
    
            $hostname = Hostname::find(1);
            $website = Website::findOrFail($hostname->website_id);
            $tenancy = app(Environment::class);
            $tenancy->hostname($hostname);
            $tenancy->tenant($website); // switches the tenant and reconfigures the app
    
            (new RouteProvider(app()))->boot();
    
            Artisan::call('tenancy:migrate:fresh', ['--website_id' => 1]);
            Artisan::call('tenancy:db:seed',['--class' => 'TenantSeeder','--website_id' => 1]);
    
        }
    

    Sadly this part is not documented, so I hope that someone with similar problems will find this post.