I am using Stancl Tenancy for a multi tenant Laravel 8 app. I have this testing well in a single core, however I want to take advantage of the new feature to run tests in parallel.
I think to do this sensibly I should create a new tenant for each database that is created by the parallelization. I've been trying to do this using the ParallelTesting::setUpTestDatabase callback, but am getting some odd behaviour.
When I go on to run tests in parallel, only database 5 of 7 (and consistently this one) has a tenant created for it:
AppServiceProvider
ParallelTesting::setUpTestDatabase(function ($database, $token) {
$tenant = Tenant::create(['id'=>"testTenant$token",'name'=>"PHPUnit Process $token"]);
});
Without the call to create a model, the databases are being setup correctly:
ParallelTesting::setUpTestDatabase(function ($database, $token) {
//$tenant = Tenant::create(['id'=>"testTenant$token",'name'=>"PHPUnit Process $token"]);
Log::info("Setup called by $token My database connection is ".DB::connection()->getDatabaseName());
});
[2021-10-21 14:45:32] testing.INFO: Setup called by 4 My database connection is project_test_4
[2021-10-21 14:45:32] testing.INFO: Setup called by 3 My database connection is project_test_3
[2021-10-21 14:45:32] testing.INFO: Setup called by 7 My database connection is project_test_7
[2021-10-21 14:45:32] testing.INFO: Setup called by 6 My database connection is project_test_6
[2021-10-21 14:45:34] testing.INFO: Setup called by 5 My database connection is project_test_5
With the line to create the model uncommented, nothing is logged and only the last connection 5 gets a Tenant created for it. Subsequent tests then appear to use the correct connection as 4/5 of them fail for TenantNotIdentified (because they have no tenant or domain).
Can anyone spot the problem?