From f694b6a6e68a5904f3b58f263dda1f6c1e090d06 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 22 Nov 2023 11:18:29 -0500 Subject: [PATCH] move to a dedicated test, and test for when no domain is passed in --- tests/StaticCaching/ApplicationCacherTest.php | 40 ++++++++++++++----- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/tests/StaticCaching/ApplicationCacherTest.php b/tests/StaticCaching/ApplicationCacherTest.php index 79b090c6e1..3804cb3338 100644 --- a/tests/StaticCaching/ApplicationCacherTest.php +++ b/tests/StaticCaching/ApplicationCacherTest.php @@ -6,6 +6,7 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Event; use Statamic\Events\UrlInvalidated; +use Statamic\StaticCaching\Cacher; use Statamic\StaticCaching\Cachers\ApplicationCacher; use Tests\TestCase; @@ -57,8 +58,6 @@ public function checking_if_page_is_cached_then_retrieving_it_will_only_hit_the_ /** @test */ public function invalidating_a_url_removes_the_html_and_the_url() { - Event::fake(); - $cache = app(Repository::class); $cacher = new ApplicationCacher($cache, ['base_url' => 'http://example.com']); $cache->forever('static-cache:'.md5('http://example.com').'.urls', [ @@ -79,17 +78,11 @@ public function invalidating_a_url_removes_the_html_and_the_url() $this->assertNull($cache->get('static-cache:responses:one')); $this->assertNotNull($cache->get('static-cache:responses:onemore')); $this->assertNotNull($cache->get('static-cache:responses:two')); - - Event::assertDispatched(UrlInvalidated::class, function ($event) { - return $event->url === '/one'; - }); } /** @test */ public function invalidating_a_url_will_invalidate_all_query_string_versions_too() { - Event::fake(); - $cache = app(Repository::class); $cacher = new ApplicationCacher($cache, ['base_url' => 'http://example.com']); $cache->forever('static-cache:'.md5('http://example.com').'.urls', [ @@ -113,12 +106,39 @@ public function invalidating_a_url_will_invalidate_all_query_string_versions_too $this->assertNull($cache->get('static-cache:responses:oneqs')); $this->assertNotNull($cache->get('static-cache:responses:onemore')); $this->assertNotNull($cache->get('static-cache:responses:two')); + } - Event::assertDispatched(UrlInvalidated::class, function ($event) { - return $event->url === '/one'; + /** + * @test + * + * @dataProvider invalidateEventProvider + */ + public function invalidating_a_url_dispatches_event($domain, $expectedUrl) + { + Event::fake(); + + $cache = app(Repository::class); + $cacher = new ApplicationCacher($cache, ['base_url' => 'http://base.com']); + + // Put it in the container so that the event can resolve it. + $this->instance(Cacher::class, $cacher); + + $cacher->invalidateUrl('/foo', $domain); + + Event::assertDispatched(UrlInvalidated::class, function ($event) use ($expectedUrl) { + return $event->url === $expectedUrl; }); } + public function invalidateEventProvider() + { + return [ + 'no domain' => [null, 'http://base.com/foo'], + 'configured base domain' => ['http://base.com', 'http://base.com/foo'], + 'another domain' => ['http://another.com', 'http://another.com/foo'], + ]; + } + /** @test */ public function it_flushes() {