Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell committed Oct 31, 2023
1 parent 1084277 commit 9f69fb0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Events/UrlInvalidated.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
14 changes: 14 additions & 0 deletions tests/StaticCaching/ApplicationCacherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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', [
Expand All @@ -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', [
Expand All @@ -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 */
Expand Down
2 changes: 2 additions & 0 deletions tests/StaticCaching/CacherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions tests/StaticCaching/FileCacherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit 9f69fb0

Please sign in to comment.