mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 22:14:03 +00:00
[4.x] Only revert initialized bootstrappers (#1385)
* Only revert initialized bootstrappers (Tenancy::initializedBootstrappers) * Fix use of @property across the codebase
This commit is contained in:
parent
f308e2f84d
commit
8f8af34c32
5 changed files with 110 additions and 4 deletions
87
tests/InitializedBootstrappersTest.php
Normal file
87
tests/InitializedBootstrappersTest.php
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Stancl\Tenancy\Events\TenancyEnded;
|
||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||
use Stancl\Tenancy\Listeners\BootstrapTenancy;
|
||||
use Stancl\Tenancy\Listeners\RevertToCentralContext;
|
||||
use Stancl\Tenancy\Tests\Etc\Tenant as TenantModel;
|
||||
|
||||
test('only bootstrappers that have been initialized are reverted', function () {
|
||||
config(['tenancy.bootstrappers' => [
|
||||
Initialized_DummyBootstrapperFoo::class,
|
||||
Initialized_DummyBootstrapperBar::class,
|
||||
Initialized_DummyBootstrapperBaz::class,
|
||||
]]);
|
||||
|
||||
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
|
||||
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
|
||||
|
||||
// Only needs to be done in tests
|
||||
app()->singleton(Initialized_DummyBootstrapperFoo::class);
|
||||
app()->singleton(Initialized_DummyBootstrapperBar::class);
|
||||
app()->singleton(Initialized_DummyBootstrapperBaz::class);
|
||||
|
||||
$tenant = TenantModel::create();
|
||||
|
||||
try {
|
||||
$tenant->run(fn() => null);
|
||||
// Should throw an exception
|
||||
expect(true)->toBe(false);
|
||||
} catch (Exception $e) {
|
||||
// NOT 'baz fail in revert' as was the behavior before
|
||||
// the commit that added this test
|
||||
expect($e->getMessage())->toBe('bar fail in bootstrap');
|
||||
}
|
||||
|
||||
expect(tenancy()->initializedBootstrappers)->toBe([
|
||||
Initialized_DummyBootstrapperFoo::class,
|
||||
]);
|
||||
});
|
||||
|
||||
class Initialized_DummyBootstrapperFoo implements TenancyBootstrapper
|
||||
{
|
||||
public string $bootstrapped = 'uninitialized';
|
||||
|
||||
public function bootstrap(Tenant $tenant): void
|
||||
{
|
||||
$this->bootstrapped = 'bootstrapped';
|
||||
}
|
||||
|
||||
public function revert(): void
|
||||
{
|
||||
$this->bootstrapped = 'reverted';
|
||||
}
|
||||
}
|
||||
|
||||
class Initialized_DummyBootstrapperBar implements TenancyBootstrapper
|
||||
{
|
||||
public string $bootstrapped = 'uninitialized';
|
||||
|
||||
public function bootstrap(Tenant $tenant): void
|
||||
{
|
||||
throw new Exception('bar fail in bootstrap');
|
||||
}
|
||||
|
||||
public function revert(): void
|
||||
{
|
||||
$this->bootstrapped = 'reverted';
|
||||
}
|
||||
}
|
||||
|
||||
class Initialized_DummyBootstrapperBaz implements TenancyBootstrapper
|
||||
{
|
||||
public string $bootstrapped = 'uninitialized';
|
||||
|
||||
public function bootstrap(Tenant $tenant): void
|
||||
{
|
||||
$this->bootstrapped = 'bootstrapped';
|
||||
}
|
||||
|
||||
public function revert(): void
|
||||
{
|
||||
throw new Exception('baz fail in revert');
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue