Skip to content

Commit

Permalink
Merge pull request #63 from maximivanov/feature/purgeCacheHosts
Browse files Browse the repository at this point in the history
Add support for host-based cache purge
  • Loading branch information
IcyApril authored Aug 28, 2018
2 parents 938065c + c9d2782 commit 8df0ec5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/Endpoints/Zones.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,16 @@ public function cachePurgeEverything(string $zoneID): bool
return false;
}

public function cachePurge(string $zoneID, array $files = null, array $tags = null): bool
public function cachePurge(string $zoneID, array $files = null, array $tags = null, array $hosts = null): bool
{
if ($files === null && $tags === null) {
throw new EndpointException('No files or tags to purge.');
if ($files === null && $tags === null && $hosts === null) {
throw new EndpointException('No files, tags or hosts to purge.');
}

$options = [
'files' => $files,
'tags' => $tags
'tags' => $tags,
'hosts' => $hosts
];

$user = $this->adapter->delete('zones/' . $zoneID . '/purge_cache', $options);
Expand Down
28 changes: 27 additions & 1 deletion tests/Endpoints/ZonesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function testGetAnalyticsDashboard()
$this->assertObjectHasAttribute('since', $analytics->totals);
$this->assertObjectHasAttribute('since', $analytics->timeseries[0]);
}

public function testChangeDevelopmentMode()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/changeDevelopmentMode.json');
Expand Down Expand Up @@ -184,4 +184,30 @@ public function testCachePurgeEverything()

$this->assertTrue($result);
}

public function testCachePurgeHost()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurgeHost.json');

$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('delete')->willReturn($response);

$mock->expects($this->once())
->method('delete')
->with(
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'),
$this->equalTo(
[
'files' => [],
'tags' => [],
'hosts' => ['dash.cloudflare.com']
]
)
);

$zones = new \Cloudflare\API\Endpoints\Zones($mock);
$result = $zones->cachePurge('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', [], [], ['dash.cloudflare.com']);

$this->assertTrue($result);
}
}
8 changes: 8 additions & 0 deletions tests/Fixtures/Endpoints/cachePurgeHost.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "023e105f4ecef8ad9ca31a8372d0c353"
}
}

0 comments on commit 8df0ec5

Please sign in to comment.