mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 17:24:03 +00:00
Add current() and currentOrFail() tenant methods (#970)
* Add and test `Tenant::current()` * Add and test `Tenant::currentOrFail()` * Fix code style (php-cs-fixer) * Update currentOrFail declaration Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com> * Change self return type to static Co-authored-by: PHP CS Fixer <phpcsfixer@example.com> Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
This commit is contained in:
parent
76a3e269c8
commit
42dab2985a
2 changed files with 38 additions and 0 deletions
|
|
@ -10,6 +10,7 @@ use Stancl\Tenancy\Contracts;
|
|||
use Stancl\Tenancy\Database\Concerns;
|
||||
use Stancl\Tenancy\Database\TenantCollection;
|
||||
use Stancl\Tenancy\Events;
|
||||
use Stancl\Tenancy\Exceptions\TenancyNotInitializedException;
|
||||
|
||||
/**
|
||||
* @property string|int $id
|
||||
|
|
@ -45,6 +46,17 @@ class Tenant extends Model implements Contracts\Tenant
|
|||
return $this->getAttribute($this->getTenantKeyName());
|
||||
}
|
||||
|
||||
public static function current(): static|null
|
||||
{
|
||||
return tenant();
|
||||
}
|
||||
|
||||
/** @throws TenancyNotInitializedException */
|
||||
public static function currentOrFail(): static
|
||||
{
|
||||
return static::current() ?? throw new TenancyNotInitializedException;
|
||||
}
|
||||
|
||||
public function newCollection(array $models = []): TenantCollection
|
||||
{
|
||||
return new TenantCollection($models);
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ use Stancl\Tenancy\Jobs\CreateDatabase;
|
|||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant;
|
||||
use Stancl\Tenancy\UUIDGenerator;
|
||||
use Stancl\Tenancy\Exceptions\TenancyNotInitializedException;
|
||||
|
||||
test('created event is dispatched', function () {
|
||||
Event::fake([TenantCreated::class]);
|
||||
|
|
@ -141,6 +142,31 @@ test('a command can be run on a collection of tenants', function () {
|
|||
expect(Tenant::find('t2')->foo)->toBe('xyz');
|
||||
});
|
||||
|
||||
test('the current method returns the currently initialized tenant', function() {
|
||||
tenancy()->initialize($tenant = Tenant::create());
|
||||
|
||||
expect(Tenant::current())->toBe($tenant);
|
||||
});
|
||||
|
||||
test('the current method returns null if there is no currently initialized tenant', function() {
|
||||
tenancy()->end();
|
||||
|
||||
expect(Tenant::current())->toBeNull();
|
||||
});
|
||||
|
||||
test('currentOrFail method returns the currently initialized tenant', function() {
|
||||
tenancy()->initialize($tenant = Tenant::create());
|
||||
|
||||
expect(Tenant::currentOrFail())->toBe($tenant);
|
||||
});
|
||||
|
||||
test('currentOrFail method throws an exception if there is no currently initialized tenant', function() {
|
||||
tenancy()->end();
|
||||
|
||||
expect(fn() => Tenant::currentOrFail())->toThrow(TenancyNotInitializedException::class);
|
||||
});
|
||||
|
||||
|
||||
class MyTenant extends Tenant
|
||||
{
|
||||
protected $table = 'tenants';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue