Skip to content

Commit

Permalink
refactor(overrides): Use the stored app and sprout instances in the c…
Browse files Browse the repository at this point in the history
…ookie override
  • Loading branch information
ollieread committed Jan 27, 2025
1 parent 6b7082e commit 7de0ccc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 89 deletions.
14 changes: 7 additions & 7 deletions src/Overrides/CookieOverride.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
namespace Sprout\Overrides;

use Illuminate\Cookie\CookieJar;
use Sprout\Contracts\DeferrableServiceOverride;
use Sprout\Contracts\ServiceOverride;
use Sprout\Contracts\Tenancy;
use Sprout\Contracts\Tenant;
use function Sprout\settings;
Expand Down Expand Up @@ -35,10 +33,10 @@ final class CookieOverride extends BaseOverride
public function setup(Tenancy $tenancy, Tenant $tenant): void
{
// Collect the values
$path = settings()->getUrlPath(config('session.path') ?? '/'); // @phpstan-ignore-line
$domain = settings()->getUrlDomain(config('session.domain')); // @phpstan-ignore-line
$secure = settings()->shouldCookieBeSecure(config('session.secure', false)); // @phpstan-ignore-line
$sameSite = settings()->getCookieSameSite(config('session.same_site')); // @phpstan-ignore-line
$path = $this->getSprout()->settings()->getUrlPath(config('session.path') ?? '/'); // @phpstan-ignore-line
$domain = $this->getSprout()->settings()->getUrlDomain(config('session.domain')); // @phpstan-ignore-line
$secure = $this->getSprout()->settings()->shouldCookieBeSecure(config('session.secure', false)); // @phpstan-ignore-line
$sameSite = $this->getSprout()->settings()->getCookieSameSite(config('session.same_site')); // @phpstan-ignore-line

/**
* This is here to make PHPStan quiet down
Expand All @@ -50,6 +48,8 @@ public function setup(Tenancy $tenancy, Tenant $tenant): void
*/

// Set the default values on the cookiejar
app(CookieJar::class)->setDefaultPathAndDomain($path, $domain, $secure, $sameSite);
$this->getApp()
->make(CookieJar::class)
->setDefaultPathAndDomain($path, $domain, $secure, $sameSite);
}
}
90 changes: 8 additions & 82 deletions tests/Unit/Overrides/CookieOverrideTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,21 @@
namespace Sprout\Tests\Unit\Overrides;

use GuzzleHttp\Cookie\CookieJar;
use Illuminate\Auth\AuthManager;
use Illuminate\Config\Repository;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Facades\Event;
use Mockery;
use Mockery\MockInterface;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Sprout\Contracts\BootableServiceOverride;
use Sprout\Contracts\DeferrableServiceOverride;
use Sprout\Events\ServiceOverrideBooted;
use Sprout\Events\ServiceOverrideProcessed;
use Sprout\Events\ServiceOverrideProcessing;
use Sprout\Events\ServiceOverrideRegistered;
use Sprout\Listeners\SetCurrentTenantForJob;
use Sprout\Overrides\Auth\SproutAuthPasswordBrokerManager;
use Sprout\Overrides\AuthOverride;
use Sprout\Overrides\CookieOverride;
use Sprout\Overrides\JobOverride;
use Sprout\Overrides\StorageOverride;
use Sprout\Sprout;
use Sprout\Support\Services;
use Sprout\Support\SettingsRepository;
use Sprout\Tests\Unit\UnitTestCase;
use Workbench\App\Models\TenantModel;
use function Sprout\sprout;
Expand Down Expand Up @@ -61,83 +57,13 @@ public function isRegisteredWithSproutCorrectly(): void
$this->assertFalse($sprout->overrides()->hasOverrideBooted('cookie'));
}

#[Test]
public function isDeferredCorrectly(): void
{
$this->markTestSkipped('This test needs to be updated');

$sprout = sprout();

Event::fake();

$sprout->registerOverride(Services::COOKIE, CookieOverride::class);

Event::assertDispatched(ServiceOverrideRegistered::class);
Event::assertNotDispatched(ServiceOverrideProcessing::class);
Event::assertNotDispatched(ServiceOverrideProcessed::class);
Event::assertNotDispatched(ServiceOverrideBooted::class);

$this->assertTrue($sprout->hasRegisteredOverride(CookieOverride::class));
$this->assertFalse($sprout->hasOverride(CookieOverride::class));
$this->assertTrue($sprout->isServiceBeingOverridden(Services::COOKIE));
$this->assertFalse($sprout->isBootableOverride(CookieOverride::class));
$this->assertTrue($sprout->isDeferrableOverride(CookieOverride::class));
$this->assertFalse($sprout->hasBootedOverride(CookieOverride::class));
$this->assertFalse($sprout->hasOverrideBeenSetup(CookieOverride::class));

$overrides = $sprout->getOverrides();

$this->assertEmpty($overrides);

$overrides = $sprout->getRegisteredOverrides();

$this->assertCount(1, $overrides);
$this->assertContains(CookieOverride::class, $overrides);

app()->make('cookie');

$overrides = $sprout->getOverrides();

$this->assertCount(1, $overrides);
$this->assertInstanceOf(CookieOverride::class, $overrides[CookieOverride::class]);

$this->assertFalse($sprout->isBootableOverride(CookieOverride::class));
$this->assertFalse($sprout->hasBootedOverride(CookieOverride::class));

Event::assertDispatched(ServiceOverrideProcessing::class);
Event::assertDispatched(ServiceOverrideProcessed::class);
Event::assertNotDispatched(ServiceOverrideBooted::class);
}

#[Test]
public function performsSetup(): void
{
$this->markTestSkipped('This test needs to be updated');

$sprout = sprout();

$sprout->registerOverride(Services::COOKIE, CookieOverride::class);

app()->make('cookie');

$override = $sprout->getOverrides()[CookieOverride::class];

$this->assertInstanceOf(CookieOverride::class, $override);

$this->assertNull(sprout()->settings()->getUrlPath());
$this->assertNull(sprout()->settings()->getUrlDomain());
$this->assertNull(sprout()->settings()->shouldCookieBeSecure());
$this->assertNull(sprout()->settings()->getCookieSameSite());

$tenant = TenantModel::factory()->createOne();
$tenancy = $sprout->tenancies()->get();

$tenancy->setTenant($tenant);
$override = new CookieOverride('cookie', []);

$this->instance('cookie', $this->spy(CookieJar::class, function (MockInterface $mock) {
$mock->shouldReceive('setDefaultPathAndDomain')->once();
}));
$app = Mockery::mock(Application::class);

$override->setup($tenancy, $tenant);
$sprout = new Sprout($app, new SettingsRepository());
}
}

0 comments on commit 7de0ccc

Please sign in to comment.