diff --git a/tests/Database/Eloquent/BelongsToManyTenantsTest.php b/tests/Database/Eloquent/BelongsToManyTenantsTest.php index 2b39c40..5e2e9a0 100644 --- a/tests/Database/Eloquent/BelongsToManyTenantsTest.php +++ b/tests/Database/Eloquent/BelongsToManyTenantsTest.php @@ -23,6 +23,7 @@ use Workbench\App\Models\TenantChildren; use Workbench\App\Models\TenantChildrenOptional; use Workbench\App\Models\TenantModel; +use function Sprout\sprout; #[Group('database'), Group('eloquent')] class BelongsToManyTenantsTest extends TestCase @@ -73,7 +74,11 @@ public function automaticallyAssociatesWithTenantWhenCreating(): void { $tenant = TenantModel::factory()->create(); - app(TenancyManager::class)->get()->setTenant($tenant); + $tenancy = app(TenancyManager::class)->get(); + + sprout()->setCurrentTenancy($tenancy); + + $tenancy->setTenant($tenant); $child = TenantChildren::factory()->create(); @@ -85,6 +90,8 @@ public function automaticallyAssociatesWithTenantWhenCreating(): void #[Test] public function throwsAnExceptionIfTheresNoTenantAndTheTenantIsNotOptionalWhenCreating(): void { + sprout()->setCurrentTenancy(app(TenancyManager::class)->get()); + $this->expectException(TenantMissing::class); $this->expectExceptionMessage( 'There is no current tenant for tenancy [tenants]' @@ -142,7 +149,11 @@ public function automaticallyPopulateTheTenantRelationWhenHydrating(): void { $tenant = TenantModel::factory()->create(); - app(TenancyManager::class)->get()->setTenant($tenant); + $tenancy = app(TenancyManager::class)->get(); + + sprout()->setCurrentTenancy($tenancy); + + $tenancy->setTenant($tenant); $child = TenantChildren::query()->find(TenantChildren::factory()->create()->getKey()); @@ -158,6 +169,8 @@ public function throwsAnExceptionIfTheresNoTenantAndTheTenantIsNotOptionalWhenHy $tenancy = app(TenancyManager::class)->get(); + sprout()->setCurrentTenancy($tenancy); + $tenancy->setTenant($tenant); $child = TenantChildren::factory()->create(); @@ -223,7 +236,10 @@ public function throwsAnExceptionIfTheTenantIsAlreadySetOnTheModelAndItIsDiffere $tenancy = app(TenancyManager::class)->get(); + sprout()->setCurrentTenancy($tenancy); + $tenancy->setTenant($tenant); + $tenancy->addOption(TenancyOptions::throwIfNotRelated()); $child = TenantChildren::factory()->create(); @@ -248,7 +264,10 @@ public function doesNotThrowAnExceptionForTenantMismatchIfNotSetToWhenHydrating( $tenancy = app(TenancyManager::class)->get(); + sprout()->setCurrentTenancy($tenancy); + $tenancy->setTenant($tenant); + $tenancy->removeOption(TenancyOptions::throwIfNotRelated()); $child = TenantChildren::factory()->create(); @@ -270,6 +289,8 @@ public function onlyReturnsModelsForTheCurrentTenant(): void $tenancy = app(TenancyManager::class)->get(); + sprout()->setCurrentTenancy($tenancy); + $tenancy->setTenant($tenant); $original = TenantChildren::factory()->create(); diff --git a/tests/Database/Eloquent/BelongsToTenantTest.php b/tests/Database/Eloquent/BelongsToTenantTest.php index 2b187da..df1b3d1 100644 --- a/tests/Database/Eloquent/BelongsToTenantTest.php +++ b/tests/Database/Eloquent/BelongsToTenantTest.php @@ -21,6 +21,7 @@ use Workbench\App\Models\TenantChild; use Workbench\App\Models\TenantChildOptional; use Workbench\App\Models\TenantModel; +use function Sprout\sprout; #[Group('database'), Group('eloquent')] class BelongsToTenantTest extends TestCase @@ -71,7 +72,11 @@ public function automaticallyAssociatesWithTenantWhenCreating(): void { $tenant = TenantModel::factory()->create(); - app(TenancyManager::class)->get()->setTenant($tenant); + $tenancy = app(TenancyManager::class)->get(); + + sprout()->setCurrentTenancy($tenancy); + + $tenancy->setTenant($tenant); $child = TenantChild::factory()->create(); @@ -83,6 +88,8 @@ public function automaticallyAssociatesWithTenantWhenCreating(): void #[Test] public function throwsAnExceptionIfTheresNoTenantAndTheTenantIsNotOptionalWhenCreating(): void { + sprout()->setCurrentTenancy(app(TenancyManager::class)->get()); + $this->expectException(TenantMissing::class); $this->expectExceptionMessage( 'There is no current tenant for tenancy [tenants]' @@ -138,7 +145,10 @@ public function throwsAnExceptionIfTheTenantIsAlreadySetOnTheModelAndItIsDiffere $tenancy = app(TenancyManager::class)->get(); + sprout()->setCurrentTenancy($tenancy); + $tenancy->setTenant($tenant); + $tenancy->addOption(TenancyOptions::throwIfNotRelated()); $this->expectException(TenantMismatch::class); @@ -174,7 +184,11 @@ public function automaticallyPopulateTheTenantRelationWhenHydrating(): void { $tenant = TenantModel::factory()->create(); - app(TenancyManager::class)->get()->setTenant($tenant); + $tenancy = app(TenancyManager::class)->get(); + + sprout()->setCurrentTenancy($tenancy); + + $tenancy->setTenant($tenant); $child = TenantChild::query()->find(TenantChild::factory()->create()->getKey()); @@ -208,6 +222,8 @@ public function throwsAnExceptionIfTheresNoTenantAndTheTenantIsNotOptionalWhenHy $tenancy = app(TenancyManager::class)->get(); + sprout()->setCurrentTenancy($tenancy); + $tenancy->setTenant($tenant); $child = TenantChild::factory()->create(); @@ -273,7 +289,10 @@ public function throwsAnExceptionIfTheTenantIsAlreadySetOnTheModelAndItIsDiffere $tenancy = app(TenancyManager::class)->get(); + sprout()->setCurrentTenancy($tenancy); + $tenancy->setTenant($tenant); + $tenancy->addOption(TenancyOptions::throwIfNotRelated()); $child = TenantChild::factory()->create(); @@ -298,7 +317,10 @@ public function doesNotThrowAnExceptionForTenantMismatchIfNotSetToWhenHydrating( $tenancy = app(TenancyManager::class)->get(); + sprout()->setCurrentTenancy($tenancy); + $tenancy->setTenant($tenant); + $tenancy->removeOption(TenancyOptions::throwIfNotRelated()); $child = TenantChild::factory()->create(); @@ -320,6 +342,8 @@ public function onlyReturnsModelsForTheCurrentTenant(): void $tenancy = app(TenancyManager::class)->get(); + sprout()->setCurrentTenancy($tenancy); + $tenancy->setTenant($tenant); $original = TenantChild::factory()->create();