Skip to content

Commit

Permalink
test: Add unit test for eloquent tenant provider
Browse files Browse the repository at this point in the history
  • Loading branch information
ollieread committed Nov 19, 2024
1 parent 45d6c80 commit dc25c65
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions tests/Unit/Providers/EloquentProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);

namespace Sprout\Tests\Unit\Providers;

use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\Attributes\Test;
use Sprout\Providers\EloquentTenantProvider;
use Sprout\Tests\Unit\UnitTestCase;
use Workbench\App\Models\TenantModel;
use function Sprout\provider;

class EloquentProviderTest extends UnitTestCase
{
use RefreshDatabase;

protected function defineEnvironment($app): void
{
tap($app['config'], static function ($config) {
$config->set('multitenancy.providers.tenants.model', TenantModel::class);
});
}

#[Test]
public function hasARegisteredName(): void
{
$provider = provider('tenants');

$this->assertInstanceOf(EloquentTenantProvider::class, $provider);
$this->assertSame('tenants', $provider->getName());
}

#[Test]
public function hasAModelClass(): void
{
$provider = provider('tenants');

$this->assertInstanceOf(EloquentTenantProvider::class, $provider);
$this->assertSame(TenantModel::class, $provider->getModelClass());
}

#[Test]
public function retrievesTenantsByTheirIdentifier(): void
{
$provider = provider('tenants');

$tenant = TenantModel::factory()->createOne();

$found = $provider->retrieveByIdentifier($tenant->getTenantIdentifier());

$this->assertNotNull($found);
$this->assertTrue($tenant->is($found));

$this->assertNull($provider->retrieveByIdentifier('fake-identifier'));
}

#[Test]
public function retrievesTenantsByTheirKey(): void
{
$provider = provider('tenants');

$tenant = TenantModel::factory()->createOne();

$found = $provider->retrieveByKey($tenant->getTenantKey());

$this->assertNotNull($found);
$this->assertTrue($tenant->is($found));

$this->assertNull($provider->retrieveByKey(-999));
}
}

0 comments on commit dc25c65

Please sign in to comment.