From c9d2782b889c90661f2d641535eb043e6463df47 Mon Sep 17 00:00:00 2001 From: Max Ivanov Date: Thu, 23 Aug 2018 16:13:56 +0100 Subject: [PATCH 1/2] Add support for host-based cache purge --- src/Endpoints/Zones.php | 9 ++++--- tests/Endpoints/ZonesTest.php | 28 +++++++++++++++++++- tests/Fixtures/Endpoints/cachePurgeHost.json | 8 ++++++ 3 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 tests/Fixtures/Endpoints/cachePurgeHost.json diff --git a/src/Endpoints/Zones.php b/src/Endpoints/Zones.php index db8f823..31cba0b 100644 --- a/src/Endpoints/Zones.php +++ b/src/Endpoints/Zones.php @@ -158,15 +158,16 @@ class Zones implements API 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); diff --git a/tests/Endpoints/ZonesTest.php b/tests/Endpoints/ZonesTest.php index 5277643..5221eb7 100644 --- a/tests/Endpoints/ZonesTest.php +++ b/tests/Endpoints/ZonesTest.php @@ -144,7 +144,7 @@ class ZonesTest extends TestCase $this->assertObjectHasAttribute('since', $analytics->totals); $this->assertObjectHasAttribute('since', $analytics->timeseries[0]); } - + public function testChangeDevelopmentMode() { $response = $this->getPsr7JsonResponseForFixture('Endpoints/changeDevelopmentMode.json'); @@ -184,4 +184,30 @@ class ZonesTest extends TestCase $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); + } } diff --git a/tests/Fixtures/Endpoints/cachePurgeHost.json b/tests/Fixtures/Endpoints/cachePurgeHost.json new file mode 100644 index 0000000..bedd2f5 --- /dev/null +++ b/tests/Fixtures/Endpoints/cachePurgeHost.json @@ -0,0 +1,8 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "023e105f4ecef8ad9ca31a8372d0c353" + } +} From 9076d714f4f2961b203362e193e194e20efffd65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robbert=20M=C3=BCller?= Date: Tue, 28 Aug 2018 08:47:50 +0200 Subject: [PATCH 2/2] Fixes problem with creating an MX record Fixes #60 php api expects a string and cloudflare api expects a int this keeps everybody happy without changing API's --- src/Endpoints/DNS.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Endpoints/DNS.php b/src/Endpoints/DNS.php index ffc7feb..dc57566 100644 --- a/src/Endpoints/DNS.php +++ b/src/Endpoints/DNS.php @@ -52,7 +52,7 @@ class DNS implements API } if (!empty($priority)) { - $options['priority'] = $priority; + $options['priority'] = (int)$priority; } $user = $this->adapter->post('zones/' . $zoneID . '/dns_records', $options);