From 9f69fb0136d7f6e89146b4d4390677098bc4d0ba Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Tue, 31 Oct 2023 21:20:53 +0000 Subject: [PATCH] Add tests --- src/Events/UrlInvalidated.php | 2 ++ tests/StaticCaching/ApplicationCacherTest.php | 14 ++++++++++++++ tests/StaticCaching/CacherTest.php | 2 ++ tests/StaticCaching/FileCacherTest.php | 16 ++++++++++++++++ 4 files changed, 34 insertions(+) diff --git a/src/Events/UrlInvalidated.php b/src/Events/UrlInvalidated.php index 246a4cbc0a..b8eec810f6 100644 --- a/src/Events/UrlInvalidated.php +++ b/src/Events/UrlInvalidated.php @@ -5,11 +5,13 @@ class UrlInvalidated extends Event { public $domain; + public $fullUrl; public $url; public function __construct($url, $domain = null) { $this->domain = $domain; + $this->fullUrl = url($url); $this->url = $url; } } diff --git a/tests/StaticCaching/ApplicationCacherTest.php b/tests/StaticCaching/ApplicationCacherTest.php index 40d68d68ab..79b090c6e1 100644 --- a/tests/StaticCaching/ApplicationCacherTest.php +++ b/tests/StaticCaching/ApplicationCacherTest.php @@ -4,6 +4,8 @@ use Illuminate\Contracts\Cache\Repository; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Event; +use Statamic\Events\UrlInvalidated; use Statamic\StaticCaching\Cachers\ApplicationCacher; use Tests\TestCase; @@ -55,6 +57,8 @@ 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', [ @@ -75,11 +79,17 @@ 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', [ @@ -103,6 +113,10 @@ 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 */ diff --git a/tests/StaticCaching/CacherTest.php b/tests/StaticCaching/CacherTest.php index 56f915481a..b487b9d2dc 100644 --- a/tests/StaticCaching/CacherTest.php +++ b/tests/StaticCaching/CacherTest.php @@ -5,7 +5,9 @@ use Illuminate\Cache\Repository; use Illuminate\Http\Request; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\Event; use Mockery; +use Statamic\Events\UrlInvalidated; use Statamic\Facades\Site; use Statamic\StaticCaching\Cachers\AbstractCacher; use Tests\TestCase; diff --git a/tests/StaticCaching/FileCacherTest.php b/tests/StaticCaching/FileCacherTest.php index e09049d4e8..0a3f479d8c 100644 --- a/tests/StaticCaching/FileCacherTest.php +++ b/tests/StaticCaching/FileCacherTest.php @@ -3,6 +3,8 @@ namespace Tests\StaticCaching; use Illuminate\Contracts\Cache\Repository; +use Illuminate\Support\Facades\Event; +use Statamic\Events\UrlInvalidated; use Statamic\Facades\Site; use Statamic\StaticCaching\Cachers\FileCacher; use Statamic\StaticCaching\Cachers\Writer; @@ -292,6 +294,20 @@ public function invalidating_a_url_deletes_the_file_and_removes_the_url_when_usi $this->assertEquals(['one' => '/one'], $cacher->getUrls('http://domain.de')->all()); } + /** @test */ + public function invalidating_a_url_fires_url_invalidated_event() + { + Event::fake(); + + $cacher = $this->fileCacher(); + + $cacher->invalidateUrl('/test'); + + Event::assertDispatched(UrlInvalidated::class, function ($event) { + return $event->url === '/test'; + }); + } + private function cacheKey($domain) { return 'static-cache:'.md5($domain).'.urls';