Skip to content

Commit

Permalink
chore: Tidy up code to use helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ollieread committed Nov 19, 2024
1 parent 7d73645 commit f6eaf00
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 28 deletions.
7 changes: 3 additions & 4 deletions tests/Unit/Attributes/CurrentTenantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
use Orchestra\Testbench\Attributes\DefineEnvironment;
use PHPUnit\Framework\Attributes\Test;
use Sprout\Attributes\CurrentTenant;
use Sprout\Contracts\Tenant;
use Sprout\Managers\TenancyManager;
use Sprout\Support\GenericTenant;
use Sprout\Tests\Unit\UnitTestCase;
use Workbench\App\Models\TenantModel;
use function Sprout\sprout;
use function Sprout\tenancy;

class CurrentTenantTest extends UnitTestCase
{
Expand Down Expand Up @@ -41,7 +40,7 @@ protected function setupSecondTenancy($app): void
public function resolvesCurrentTenant(): void
{
/** @var \Sprout\Contracts\Tenancy $tenancy */
$tenancy = app(TenancyManager::class)->get('tenants');
$tenancy = tenancy('tenants');

sprout()->setCurrentTenancy($tenancy);

Expand All @@ -63,7 +62,7 @@ public function resolvesCurrentTenant(): void
public function resolvesCurrentTenantForSpecificTenancy(): void
{
/** @var \Sprout\Contracts\Tenancy $tenancy */
$tenancy = app(TenancyManager::class)->get('backup');
$tenancy = tenancy('backup');

sprout()->setCurrentTenancy($tenancy);

Expand Down
35 changes: 17 additions & 18 deletions tests/Unit/Support/ResolutionHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
use PHPUnit\Framework\Attributes\Test;
use Sprout\Exceptions\MisconfigurationException;
use Sprout\Exceptions\NoTenantFound;
use Sprout\Managers\IdentityResolverManager;
use Sprout\Managers\TenancyManager;
use Sprout\Support\ResolutionHelper;
use Sprout\Support\ResolutionHook;
use Sprout\Tests\Unit\UnitTestCase;
use Workbench\App\Models\TenantModel;
use function Sprout\resolver;
use function Sprout\sprout;
use function Sprout\tenancy;

class ResolutionHelperTest extends UnitTestCase
{
Expand Down Expand Up @@ -67,12 +66,12 @@ public function throwsExceptionWhenHandlingResolutionForUnsupportedHook(): void
public function returnsFalseIfThereIsAlreadyATenant(): void
{
/** @var \Sprout\Contracts\Tenancy<TenantModel> $tenancy */
$tenancy = app(TenancyManager::class)->get();
$tenancy = tenancy();

$tenancy->setTenant(TenantModel::factory()->createOne());

/** @var \Sprout\Contracts\IdentityResolver $resolver */
$resolver = app(IdentityResolverManager::class)->get('path');
$resolver = resolver('path');

/** @var \Illuminate\Http\Request $fakeRequest */
$fakeRequest = $this->mock(Request::class);
Expand All @@ -86,10 +85,10 @@ public function returnsFalseIfThereIsAlreadyATenant(): void
public function returnsFalseIfTheResolverCannotResolve(): void
{
/** @var \Sprout\Contracts\Tenancy<TenantModel> $tenancy */
$tenancy = app(TenancyManager::class)->get();
$tenancy = tenancy();

/** @var \Sprout\Contracts\IdentityResolver $resolver */
$resolver = app(IdentityResolverManager::class)->get('path');
$resolver = resolver('path');

$tenancy->setTenant(TenantModel::factory()->createOne())
->resolvedVia($resolver)
Expand All @@ -109,10 +108,10 @@ public function resolvesTenantUsingRouteParameters(): void
$tenant = TenantModel::factory()->createOne();

/** @var \Sprout\Contracts\Tenancy<TenantModel> $tenancy */
$tenancy = app(TenancyManager::class)->get();
$tenancy = tenancy();

/** @var \Sprout\Contracts\IdentityResolver&\Sprout\Contracts\IdentityResolverUsesParameters $resolver */
$resolver = app(IdentityResolverManager::class)->get('path');
$resolver = resolver('path');

/** @var \Illuminate\Routing\Route $fakeRoute */
$fakeRoute = $this->mock(Route::class, function (MockInterface $mock) use ($tenant, $tenancy, $resolver) {
Expand Down Expand Up @@ -158,10 +157,10 @@ public function resolvesTenantUsingRouteParameters(): void
public function throwsAnExceptionWhenUnableToIdentifyATenantFromTheRoute(): void
{
/** @var \Sprout\Contracts\Tenancy<TenantModel> $tenancy */
$tenancy = app(TenancyManager::class)->get();
$tenancy = tenancy();

/** @var \Sprout\Contracts\IdentityResolver&\Sprout\Contracts\IdentityResolverUsesParameters $resolver */
$resolver = app(IdentityResolverManager::class)->get('path');
$resolver = resolver('path');

/** @var \Illuminate\Routing\Route $fakeRoute */
$fakeRoute = $this->mock(Route::class, function (MockInterface $mock) use ($tenancy, $resolver) {
Expand Down Expand Up @@ -199,10 +198,10 @@ public function throwsAnExceptionWhenUnableToIdentifyATenantFromTheRoute(): void
public function returnsFalseWhenUnableToIdentifyATenantFromTheRouteAndToldNotToThrow(): void
{
/** @var \Sprout\Contracts\Tenancy<TenantModel> $tenancy */
$tenancy = app(TenancyManager::class)->get();
$tenancy = tenancy();

/** @var \Sprout\Contracts\IdentityResolver&\Sprout\Contracts\IdentityResolverUsesParameters $resolver */
$resolver = app(IdentityResolverManager::class)->get('path');
$resolver = resolver('path');

/** @var \Illuminate\Routing\Route $fakeRoute */
$fakeRoute = $this->mock(Route::class, function (MockInterface $mock) use ($tenancy, $resolver) {
Expand Down Expand Up @@ -246,10 +245,10 @@ public function resolvesTenantWithoutRouteParameters(): void
$tenant = TenantModel::factory()->createOne();

/** @var \Sprout\Contracts\Tenancy<TenantModel> $tenancy */
$tenancy = app(TenancyManager::class)->get();
$tenancy = tenancy();

/** @var \Sprout\Contracts\IdentityResolver&\Sprout\Contracts\IdentityResolverUsesParameters $resolver */
$resolver = app(IdentityResolverManager::class)->get('path');
$resolver = resolver('path');

/** @var \Illuminate\Http\Request $fakeRequest */
$fakeRequest = $this->mock(Request::class, function (MockInterface $mock) use ($tenant) {
Expand Down Expand Up @@ -283,10 +282,10 @@ public function resolvesTenantWithoutRouteParameters(): void
public function throwsAnExceptionWhenUnableToIdentifyATenantFromTheRequest(): void
{
/** @var \Sprout\Contracts\Tenancy<TenantModel> $tenancy */
$tenancy = app(TenancyManager::class)->get();
$tenancy = tenancy();

/** @var \Sprout\Contracts\IdentityResolver&\Sprout\Contracts\IdentityResolverUsesParameters $resolver */
$resolver = app(IdentityResolverManager::class)->get('path');
$resolver = resolver('path');

/** @var \Illuminate\Http\Request $fakeRequest */
$fakeRequest = $this->mock(Request::class, function (MockInterface $mock) {
Expand All @@ -312,10 +311,10 @@ public function throwsAnExceptionWhenUnableToIdentifyATenantFromTheRequest(): vo
public function returnsFalseWhenUnableToIdentifyATenantFromTheRequestAndToldNotToThrow(): void
{
/** @var \Sprout\Contracts\Tenancy<TenantModel> $tenancy */
$tenancy = app(TenancyManager::class)->get();
$tenancy = tenancy();

/** @var \Sprout\Contracts\IdentityResolver&\Sprout\Contracts\IdentityResolverUsesParameters $resolver */
$resolver = app(IdentityResolverManager::class)->get('path');
$resolver = resolver('path');

/** @var \Illuminate\Http\Request $fakeRequest */
$fakeRequest = $this->mock(Request::class, function (MockInterface $mock) {
Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/TenancyOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use Illuminate\Config\Repository;
use Orchestra\Testbench\Attributes\DefineEnvironment;
use PHPUnit\Framework\Attributes\Test;
use Sprout\Managers\TenancyManager;
use Sprout\TenancyOptions;
use function Sprout\tenancy;

class TenancyOptionsTest extends UnitTestCase
{
Expand Down Expand Up @@ -40,7 +40,7 @@ public function throwIfNotRelatedOption(): void
#[Test, DefineEnvironment('setupSecondTenancy')]
public function correctlyReportsHydrateTenantRelationOptionPresence(): void
{
$tenancy = app(TenancyManager::class)->get('tenants');
$tenancy = tenancy('tenants');
$tenancy->removeOption(TenancyOptions::hydrateTenantRelation());

$this->assertFalse(TenancyOptions::shouldHydrateTenantRelation($tenancy));
Expand All @@ -49,15 +49,15 @@ public function correctlyReportsHydrateTenantRelationOptionPresence(): void

$this->assertTrue(TenancyOptions::shouldHydrateTenantRelation($tenancy));

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

$this->assertFalse(TenancyOptions::shouldHydrateTenantRelation($tenancy));
}

#[Test, DefineEnvironment('setupSecondTenancy')]
public function correctlyReportsThrowIfNotRelatedOptionPresence(): void
{
$tenancy = app(TenancyManager::class)->get('tenants');
$tenancy = tenancy('tenants');
$tenancy->removeOption(TenancyOptions::throwIfNotRelated());

$this->assertFalse(TenancyOptions::shouldThrowIfNotRelated($tenancy));
Expand All @@ -66,7 +66,7 @@ public function correctlyReportsThrowIfNotRelatedOptionPresence(): void

$this->assertTrue(TenancyOptions::shouldThrowIfNotRelated($tenancy));

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

$this->assertFalse(TenancyOptions::shouldThrowIfNotRelated($tenancy));
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/UnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
namespace Sprout\Tests\Unit;

use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;

abstract class UnitTestCase extends \Orchestra\Testbench\TestCase
abstract class UnitTestCase extends TestCase
{
use WithWorkbench;

Expand Down

0 comments on commit f6eaf00

Please sign in to comment.