Skip to content

Commit

Permalink
make cache purge environment aware (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
jafowler authored Dec 17, 2024
1 parent 20da7aa commit 2d3f198
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 5 deletions.
25 changes: 23 additions & 2 deletions src/Endpoints/Zones.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Created by PhpStorm.
* User: junade
Expand Down Expand Up @@ -216,9 +217,18 @@ public function setCachingLevel(string $zoneID, string $level = 'aggressive'): b
* Purge Everything
* @param string $zoneID
* @return bool
*
* @SuppressWarnings(PHPMD)
*/
public function cachePurgeEverything(string $zoneID): bool
public function cachePurgeEverything(string $zoneID, bool $includeEnvironments = false): bool
{
if ($includeEnvironments) {
$env = $this->adapter->get("zones/$zoneID/environments");
$envs = json_decode($env->getBody(), true);
foreach ($envs["result"]["environments"] as $env) {
$this->adapter->post("zones/$zoneID/environments/{$env["ref"]}/purge_cache", ['purge_everything' => true]);
}
}
$user = $this->adapter->post('zones/' . $zoneID . '/purge_cache', ['purge_everything' => true]);

$this->body = json_decode($user->getBody());
Expand All @@ -230,7 +240,10 @@ public function cachePurgeEverything(string $zoneID): bool
return false;
}

public function cachePurge(string $zoneID, array $files = null, array $tags = null, array $hosts = null): bool
/**
* @SuppressWarnings(PHPMD)
*/
public function cachePurge(string $zoneID, array $files = null, array $tags = null, array $hosts = null, bool $includeEnvironments = false): bool
{
if ($files === null && $tags === null && $hosts === null) {
throw new EndpointException('No files, tags or hosts to purge.');
Expand All @@ -249,6 +262,14 @@ public function cachePurge(string $zoneID, array $files = null, array $tags = nu
$options['hosts'] = $hosts;
}

if ($includeEnvironments) {
$env = $this->adapter->get("zones/$zoneID/environments");
$envs = json_decode($env->getBody(), true);
foreach ($envs["result"]["environments"] as $env) {
$this->adapter->post("zones/$zoneID/environments/{$env["ref"]}/purge_cache", $options);
}
}

$user = $this->adapter->post('zones/' . $zoneID . '/purge_cache', $options);

$this->body = json_decode($user->getBody());
Expand Down
109 changes: 106 additions & 3 deletions tests/Endpoints/ZoneCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ public function testCachePurge()
->method('post')
->with(
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'),
$this->equalTo(['files' => [
'https://example.com/file.jpg',
]
$this->equalTo([
'files' => [
'https://example.com/file.jpg',
]
])
);

Expand All @@ -75,4 +76,106 @@ public function testCachePurge()
$this->assertTrue($result);
$this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id);
}

public function testCachePurgeIncludingEnvironments()
{
$envResp = $this->getPsr7JsonResponseForFixture('Endpoints/getEnvironments.json');
$cacheResp = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurge.json');
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();

$mock->expects($this->once())
->method('get')
->willReturn($envResp)
->with(
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments')
);

$mock->expects($this->exactly(4))
->method('post')
->willReturn($cacheResp)
->withConsecutive(
[
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments/first/purge_cache'),
$this->equalTo([
'files' => [
'https://example.com/file.jpg',
]
])
],
[
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments/second/purge_cache'),
$this->equalTo([
'files' => [
'https://example.com/file.jpg',
]
])
],
[
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments/third/purge_cache'),
$this->equalTo([
'files' => [
'https://example.com/file.jpg',
]
])
],
[
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'),
$this->equalTo([
'files' => [
'https://example.com/file.jpg',
]
])
]
);

$zones = new \Cloudflare\API\Endpoints\Zones($mock);
$result = $zones->cachePurge('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', [
'https://example.com/file.jpg',
], null, null, true);

$this->assertTrue($result);
$this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id);
}

public function testCachePurgeEverythingIncludingEnvironments()
{
$envResp = $this->getPsr7JsonResponseForFixture('Endpoints/getEnvironments.json');
$cacheResp = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurgeEverything.json');
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();

$mock->expects($this->once())
->method('get')
->willReturn($envResp)
->with(
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments')
);

$mock->expects($this->exactly(4))
->method('post')
->willReturn($cacheResp)
->withConsecutive(
[
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments/first/purge_cache'),
$this->equalTo(['purge_everything' => true])
],
[
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments/second/purge_cache'),
$this->equalTo(['purge_everything' => true])
],
[
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/environments/third/purge_cache'),
$this->equalTo(['purge_everything' => true])
],
[
$this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'),
$this->equalTo(['purge_everything' => true])
]
);

$zones = new \Cloudflare\API\Endpoints\Zones($mock);
$result = $zones->cachePurgeEverything('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', true);

$this->assertTrue($result);
$this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id);
}
}
40 changes: 40 additions & 0 deletions tests/Fixtures/Endpoints/getEnvironments.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"result": {
"environments": [
{
"name": "Production",
"ref": "first",
"version": 0,
"expression": "expression_1",
"locked_on_deployment": false,
"position": {
"before": "second"
}
},
{
"name": "Staging",
"ref": "second",
"version": 0,
"expression": "expression_2",
"locked_on_deployment": false,
"position": {
"before": "third",
"after": "first"
}
},
{
"name": "Development",
"ref": "third",
"version": 0,
"expression": "expression_3",
"locked_on_deployment": false,
"position": {
"after": "second"
}
}
]
},
"success": true,
"errors": [],
"messages": []
}

0 comments on commit 2d3f198

Please sign in to comment.