@mostafaalotfi@gmail.com There are a few ways you can set the HTTP_HOST dynamically while integration testing on a multitenant application with different domains:
- Use environment variables to set the
HTTP_HOST: You can set theHTTP_HOSTin an environment variable, and then use that variable in your tests. This way, you can change the value of the environment variable without modifying the code of your tests.
public function testTenant1() {
putenv("HTTP_HOST=tenant1.com");
$response = $this->get('/');
$response->assertStatus(200);
}
public function testTenant2() {
putenv("HTTP_HOST=tenant2.com");
$response = $this->get('/');
$response->assertStatus(200);
}
- Use a loop to iterate through all the tenants: You can use a loop to iterate through all the tenants, and set the
HTTP_HOSTto the domain of the current tenant.
public function testTenant() {
$tenants = ['tenant1.com', 'tenant2.com'];
foreach ($tenants as $tenant) {
$_SERVER['HTTP_HOST'] = $tenant;
$response = $this->get('/');
$response->assertStatus(200);
}
}
- Use a custom middleware: you can create a custom middleware that sets the
HTTP_HOSTbased on the tenant's domain, and then use that middleware in your tests.
public function testTenant() {
$tenants = ['tenant1.com', 'tenant2.com'];
foreach ($tenants as $tenant) {
$response = $this->get('/', [
'HTTP_HOST' => $tenant,
]);
$response->assertStatus(200);
}
}
It's important to note that, to change the HTTP_HOST and test the different scenarios, you have to make sure that you are using the current tenant's domain in all the requests, not only in the first one.