For anyone struggling with this, in your test, import your tenant class from App\Models\Tenant rather than Stancl\Tenancy\Database\Models\Tenant
Test fails for Stancl\Tenancy when trying to create the database
I am working on a multi-tenant Laravel application and I've implemented a feature where the tenant_id is supposed to be set in the session upon user login. I wrote a test to verify this functionality, however, the test fails with a TypeError which suggests that the wrong type of Tenant object is being used.
Test Error:
Stancl\Tenancy\Jobs\CreateDatabase::__construct(): Argument #1 ($tenant) must be of type Stancl\Tenancy\Contracts\TenantWithDatabase, Stancl\Tenancy\Database\Models\Tenant given, called in /var/www/html/vendor/stancl/jobpipeline/src/JobPipeline.php on line 64
This error points to an issue in the Stancl Tenancy package when attempting to create a database for the Tenant. The type expected is TenantWithDatabase, but the test is providing a Tenant model.
Steps Taken So Far:
I created a Tenant model that implements the TenantWithDatabase contract to ensure the correct Tenant object type. This didn't resolve the issue and the test still fails with the same error.
I verified that the tenant_id is indeed being set in the session after a successful login. The test checks this with the line $this->assertEquals(session('tenant_id'), $tenant->id);.
Test Code:
namespace Tests\Feature;
use App\Models\User;
use App\Models\Tenant;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class TenantSessionTest extends TestCase
{
use RefreshDatabase;
/** @test */
public function tenant_id_is_set_in_session_after_login()
{
// Create a tenant
$tenant = \App\Models\Tenant::factory()->create();
// Create a user associated with the tenant
$user = User::factory()->create([
'tenant_id' => $tenant->id,
]);
// Act as if the user logs in
$response = $this->post('/login', [
'email' => $user->email,
'password' => 'password', // Assuming this is the default password
]);
// Assert the login was successful
$response->assertRedirect('/dashboard');
// Assert that tenant_id is set in the session
$this->assertAuthenticated();
$this->assertEquals(session('tenant_id'), $tenant->id);
}
}
My Model:
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Stancl\Tenancy\Database\Models\Tenant as BaseTenant;
use Stancl\Tenancy\Contracts\TenantWithDatabase;
use Stancl\Tenancy\Database\Concerns\HasDatabase;
use Stancl\Tenancy\Database\Concerns\HasDomains;
class Tenant extends BaseTenant implements TenantWithDatabase
{
use HasDatabase;
use HasFactory;
}
config/tenancy.php
return [
'tenant_model' => \App\Models\Tenant::class,
'id_generator' => Stancl\Tenancy\UUIDGenerator::class,
'domain_model' => Domain::class,
I am still struggling with this issue and I am unsure what the problem might be. The test seems to be structured correctly, but the error suggests that there's a discrepancy between the types of Tenant objects. I would appreciate any guidance or suggestions on what might be going wrong and how to resolve this issue.
Please or to participate in this conversation.