diff --git a/src/Endpoints/Zones.php b/src/Endpoints/Zones.php index fd9e18b0..3a14c587 100644 --- a/src/Endpoints/Zones.php +++ b/src/Endpoints/Zones.php @@ -1,4 +1,5 @@ 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()); @@ -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.'); @@ -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()); diff --git a/tests/Endpoints/ZoneCacheTest.php b/tests/Endpoints/ZoneCacheTest.php index bdefeeca..86bc04d5 100644 --- a/tests/Endpoints/ZoneCacheTest.php +++ b/tests/Endpoints/ZoneCacheTest.php @@ -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', + ] ]) ); @@ -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); + } } diff --git a/tests/Fixtures/Endpoints/getEnvironments.json b/tests/Fixtures/Endpoints/getEnvironments.json new file mode 100644 index 00000000..3a97bfe9 --- /dev/null +++ b/tests/Fixtures/Endpoints/getEnvironments.json @@ -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": [] +}