1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 22:14:03 +00:00
tenancy/tests/InitializedBootstrappersTest.php
Samuel Štancl 8f8af34c32
[4.x] Only revert initialized bootstrappers (#1385)
* Only revert initialized bootstrappers (Tenancy::initializedBootstrappers)

* Fix use of @property across the codebase
2025-08-05 11:12:25 +02:00

87 lines
2.4 KiB
PHP

<?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');
}
}