@sh1r3f Testing a multi-tenancy application can be a bit tricky, as you need to make sure that the tenant is properly set up and switched for each test. Here are some general steps you can take to write tests for your stancl/tenancy app:
-
Set up your test environment: Make sure you have a testing database set up and that your tests are configured to use it.
-
Create a test tenant: Create a test tenant in your testing database, so that you can test your application's tenant-specific functionality.
-
Switch to the tenant in your tests: Use the Tenancy facade provided by the stancl/tenancy package to switch to the test tenant before running each test. This will ensure that the tenant is properly set up and that all database queries are executed within the context of the tenant.
-
Write your tests: Write your tests as you would normally, making sure to take into account any tenant-specific functionality.
-
Run your tests: Run your tests to make sure that everything is working as expected.
Here are some example of test set up:
use Stancl\Tenancy\Facades\Tenancy;
public function setUp(): void
{
parent::setUp();
// Create a test tenant.
$tenant = Tenancy::createTenant([
'domain' => 'test.localhost',
]);
// Switch to the tenant.
Tenancy::setTenant($tenant);
}