From ba140c92686a22aa97787f98466a2b047f827dfc Mon Sep 17 00:00:00 2001 From: Spenser Hale Date: Thu, 1 Oct 2020 12:03:27 -0700 Subject: [PATCH 1/2] feat(Endpoints\Zones): Implementing prefixes feature of route Adding prefixes parameter for clearning assets at URLs that match the prefix will be purged from the Cloudflare cache. --- src/Endpoints/Zones.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Endpoints/Zones.php b/src/Endpoints/Zones.php index 2335b1c8..c8ef1fe0 100644 --- a/src/Endpoints/Zones.php +++ b/src/Endpoints/Zones.php @@ -228,10 +228,10 @@ public function cachePurgeEverything(string $zoneID): bool return false; } - public function cachePurge(string $zoneID, array $files = null, array $tags = null, array $hosts = null): bool + public function cachePurge(string $zoneID, array $files = null, array $tags = null, array $hosts = null, array $prefixes = null): bool { - if ($files === null && $tags === null && $hosts === null) { - throw new EndpointException('No files, tags or hosts to purge.'); + if ($files === null && $tags === null && $hosts === null && $prefixes === null) { + throw new EndpointException('No files, tags, hosts or prefixes to purge.'); } $options = []; @@ -247,6 +247,10 @@ public function cachePurge(string $zoneID, array $files = null, array $tags = nu $options['hosts'] = $hosts; } + if (!is_null($prefixes)) { + $options['prefixes'] = $prefixes; + } + $user = $this->adapter->post('zones/' . $zoneID . '/purge_cache', $options); $this->body = json_decode($user->getBody()); From d0e340c0b88d6f6a741d22d2c5132f85cf746a12 Mon Sep 17 00:00:00 2001 From: Spenser Hale Date: Thu, 1 Oct 2020 12:12:07 -0700 Subject: [PATCH 2/2] test(Endpoints\Zones): Adding test for new parameter of cachePurge --- tests/Endpoints/ZonesTest.php | 30 +++++++++++++++++++ .../Fixtures/Endpoints/cachePurgePrefix.json | 8 +++++ 2 files changed, 38 insertions(+) create mode 100644 tests/Fixtures/Endpoints/cachePurgePrefix.json diff --git a/tests/Endpoints/ZonesTest.php b/tests/Endpoints/ZonesTest.php index 02addeba..410b3ec2 100644 --- a/tests/Endpoints/ZonesTest.php +++ b/tests/Endpoints/ZonesTest.php @@ -262,4 +262,34 @@ public function testCachePurge() $this->assertTrue($result); $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id); } + + public function testCachePurgePrefix() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurgePrefix.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('post')->willReturn($response); + + $mock->expects($this->once()) + ->method('post') + ->with( + $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'), + $this->equalTo( + [ + 'files' => [], + 'tags' => [], + 'hosts' => [], + 'prefixes' => [ + 'https://example.com/path' + ] + ] + ) + ); + + $zones = new \Cloudflare\API\Endpoints\Zones($mock); + $result = $zones->cachePurge('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', [], [], [], ['https://example.com/path']); + + $this->assertTrue($result); + $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id); + } } diff --git a/tests/Fixtures/Endpoints/cachePurgePrefix.json b/tests/Fixtures/Endpoints/cachePurgePrefix.json new file mode 100644 index 00000000..bedd2f5c --- /dev/null +++ b/tests/Fixtures/Endpoints/cachePurgePrefix.json @@ -0,0 +1,8 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "023e105f4ecef8ad9ca31a8372d0c353" + } +}