Skip to content

Commit

Permalink
fix: Fix tests by setting the current tenancy
Browse files Browse the repository at this point in the history
  • Loading branch information
ollieread committed Nov 18, 2024
1 parent 4329fa8 commit 6110005
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
25 changes: 23 additions & 2 deletions tests/Database/Eloquent/BelongsToManyTenantsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand All @@ -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]'
Expand Down Expand Up @@ -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());

Expand All @@ -158,6 +169,8 @@ public function throwsAnExceptionIfTheresNoTenantAndTheTenantIsNotOptionalWhenHy

$tenancy = app(TenancyManager::class)->get();

sprout()->setCurrentTenancy($tenancy);

$tenancy->setTenant($tenant);

$child = TenantChildren::factory()->create();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -270,6 +289,8 @@ public function onlyReturnsModelsForTheCurrentTenant(): void

$tenancy = app(TenancyManager::class)->get();

sprout()->setCurrentTenancy($tenancy);

$tenancy->setTenant($tenant);

$original = TenantChildren::factory()->create();
Expand Down
28 changes: 26 additions & 2 deletions tests/Database/Eloquent/BelongsToTenantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand All @@ -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]'
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -208,6 +222,8 @@ public function throwsAnExceptionIfTheresNoTenantAndTheTenantIsNotOptionalWhenHy

$tenancy = app(TenancyManager::class)->get();

sprout()->setCurrentTenancy($tenancy);

$tenancy->setTenant($tenant);

$child = TenantChild::factory()->create();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -320,6 +342,8 @@ public function onlyReturnsModelsForTheCurrentTenant(): void

$tenancy = app(TenancyManager::class)->get();

sprout()->setCurrentTenancy($tenancy);

$tenancy->setTenant($tenant);

$original = TenantChild::factory()->create();
Expand Down

0 comments on commit 6110005

Please sign in to comment.