Testing utilities for Laravel multitenancy applications. Provides a comprehensive base test case with automatic tenant database isolation, configuration management, and helper methods to simplify testing in multi-tenant environments built with Spatie Laravel Multitenancy.
You can install the package via composer:
Laravel | PHP | Version |
---|---|---|
10, 11 | 8.1+ | 1.0.0 |
composer require armcm/laravel-tenancy-testing
Extend the TenancyTestCase
class in your test classes instead of Laravel's default TestCase:
<?php
namespace Tests\Feature;
use ArmCm\LaravelTenancyTesting\TenancyTestCase;
use App\Models\User;
class UserTest extends TenancyTestCase
{
/** @test */
public function it_creates_users_in_tenant_context()
{
// The tenant is automatically set up and active
$user = User::factory()->create([
'name' => 'John Doe',
]);
$this->assertDatabaseHas('users', [
'name' => 'John Doe',
], 'tenant');
}
}
When you extend TenancyTestCase
, the following is handled for you:
- Landlord database is created with a fresh SQLite database
- Tenant database is created with isolated SQLite database
- Migrations are run automatically for both databases
- A test tenant is created and made current
- Configuration is set up correctly for multitenancy
- Cleanup happens automatically after each test
You can also test landlord-specific functionality:
<?php
namespace Tests\Feature;
use ArmCm\LaravelTenancyTesting\LandlordTestCase;
use Modules\Landlord\Models\Tenant;
class TenantTest extends LandlordTestCase
{
/** @test */
public function it_creates_tenants_in_landlord_context()
{
// The tenant is automatically set up and active
$tenant = Tenant::factory()->create([
'name' => 'Fake inc',
]);
$this->assertDatabaseHas('tenants', [
'name' => 'Fake inc',
], 'landlord');
}
}
You can publish the configuration file to customize the package behavior: The package uses the following configuration file to override:
php artisan vendor:publish --provider="ArmCm\LaravelTenancyTesting\ServiceProvider" --tag=config
This will create a config/tenancy-testing.php
file with the following options:
<?php
return [
'connections' => [
'landlord' => 'landlord',
'tenant' => 'tenant',
],
'migrations' => [
'landlord' => '',
'tenant' => 'database/migrations',
],
'tenant_model' => [
'class' => '',
'connection' => 'landlord',
],
'database' => [
'driver' => 'sqlite',
'temp_directory' => null,
'foreign_key_constraints' => true,
],
'default_tenant_attributes' => [
'name' => 'Test Tenant',
'domain' => 'test.localhost',
],
];
Laravel 11+ Add the service provider to your bootstrap/providers.php
:
<?php
return [
App\Providers\AppServiceProvider::class,
ArmCm\LaravelTenancyTesting\ServiceProvider::class,
];
Laravel 10 Add the service provider to your config/app.php
:
'providers' => [
// Other Service Providers...
ArmCm\LaravelTenancyTesting\ServiceProvider::class,
],
If you have a custom tenant model: config/tenancy-testing.php
'tenant_model' => [
'class' => \App\Models\CustomTenant::class,
'connection' => 'landlord',
],
- PHP 8.1 or higher
- Laravel 10.x or 11.x
- Spatie Laravel Multitenancy package
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.