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()); 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" + } +}