From faa7c1572fac7409e0971de378c7a3b6d70b1aec Mon Sep 17 00:00:00 2001 From: Craig Menning Date: Mon, 11 May 2020 15:40:48 -0500 Subject: [PATCH 1/2] Switch cache purge endpoints from DELETE to POST --- src/Endpoints/CustomHostnames.php | 306 ++++++++++++------------ src/Endpoints/Zones.php | 4 +- tests/Endpoints/CustomHostnamesTest.php | 276 ++++++++++----------- tests/Endpoints/ZonesTest.php | 6 +- 4 files changed, 296 insertions(+), 296 deletions(-) diff --git a/src/Endpoints/CustomHostnames.php b/src/Endpoints/CustomHostnames.php index 4af5c9ee..798934dd 100644 --- a/src/Endpoints/CustomHostnames.php +++ b/src/Endpoints/CustomHostnames.php @@ -1,153 +1,153 @@ -adapter = $adapter; - } - - - /** - * @SuppressWarnings(PHPMD.BooleanArgumentFlag) - * - * @param string $zoneID - * @param string $hostname - * @param string $sslMethod - * @param string $sslType - * @return \stdClass - */ - public function addHostname(string $zoneID, string $hostname, string $sslMethod = 'http', string $sslType = 'dv'): \stdClass - { - $options = [ - 'hostname' => $hostname, - 'ssl' => [ - 'method' => $sslMethod, - 'type' => $sslType - ] - ]; - - $zone = $this->adapter->post('zones/'.$zoneID.'/custom_hostnames', $options); - $this->body = json_decode($zone->getBody()); - return $this->body->result; - } - - /** - * @param string $zoneID - * @param string $hostname - * @param string $id - * @param int $page - * @param int $perPage - * @param string $order - * @param string $direction - * @param int $ssl - * @return \stdClass - */ - public function listHostnames( - string $zoneID, - string $hostname = '', - string $hostnameID = '', - int $page = 1, - int $perPage = 20, - string $order = '', - string $direction = '', - int $ssl = 0 - ): \stdClass { - $query = [ - 'page' => $page, - 'per_page' => $perPage, - 'ssl' => $ssl - ]; - - if (!empty($hostname)) { - $query['hostname'] = $hostname; - } - - if (!empty($hostnameID)) { - $query['id'] = $hostnameID; - } - - if (!empty($order)) { - $query['order'] = $order; - } - - if (!empty($direction)) { - $query['direction'] = $direction; - } - - $zone = $this->adapter->get('zones/'.$zoneID.'/custom_hostnames', $query); - $this->body = json_decode($zone->getBody()); - - return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; - } - - /** - * @param string $zoneID - * @param string $hostnameID - * @return mixed - */ - public function getHostname(string $zoneID, string $hostnameID) - { - $zone = $this->adapter->get('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID); - $this->body = json_decode($zone->getBody()); - - return $this->body->result; - } - - /** - * @SuppressWarnings(PHPMD.BooleanArgumentFlag) - * - * @param string $zoneID - * @param string $hostnameID - * @param string $sslMethod - * @param string $sslType - * @return \stdClass - */ - public function updateHostname(string $zoneID, string $hostnameID, string $sslMethod = '', string $sslType = ''): \stdClass - { - $query = []; - - if (!empty($sslMethod)) { - $query['method'] = $sslMethod; - } - - if (!empty($sslType)) { - $query['type'] = $sslType; - } - - $options = [ - 'ssl' => $query - ]; - - $zone = $this->adapter->patch('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID, $options); - $this->body = json_decode($zone->getBody()); - return $this->body->result; - } - - /** - * @param string $zoneID - * @param string $hostnameID - * @return \stdClass - */ - public function deleteHostname(string $zoneID, string $hostnameID): \stdClass - { - $zone = $this->adapter->delete('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID); - $this->body = json_decode($zone->getBody()); - return $this->body; - } -} +adapter = $adapter; + } + + + /** + * @SuppressWarnings(PHPMD.BooleanArgumentFlag) + * + * @param string $zoneID + * @param string $hostname + * @param string $sslMethod + * @param string $sslType + * @return \stdClass + */ + public function addHostname(string $zoneID, string $hostname, string $sslMethod = 'http', string $sslType = 'dv'): \stdClass + { + $options = [ + 'hostname' => $hostname, + 'ssl' => [ + 'method' => $sslMethod, + 'type' => $sslType + ] + ]; + + $zone = $this->adapter->post('zones/'.$zoneID.'/custom_hostnames', $options); + $this->body = json_decode($zone->getBody()); + return $this->body->result; + } + + /** + * @param string $zoneID + * @param string $hostname + * @param string $id + * @param int $page + * @param int $perPage + * @param string $order + * @param string $direction + * @param int $ssl + * @return \stdClass + */ + public function listHostnames( + string $zoneID, + string $hostname = '', + string $hostnameID = '', + int $page = 1, + int $perPage = 20, + string $order = '', + string $direction = '', + int $ssl = 0 + ): \stdClass { + $query = [ + 'page' => $page, + 'per_page' => $perPage, + 'ssl' => $ssl + ]; + + if (!empty($hostname)) { + $query['hostname'] = $hostname; + } + + if (!empty($hostnameID)) { + $query['id'] = $hostnameID; + } + + if (!empty($order)) { + $query['order'] = $order; + } + + if (!empty($direction)) { + $query['direction'] = $direction; + } + + $zone = $this->adapter->get('zones/'.$zoneID.'/custom_hostnames', $query); + $this->body = json_decode($zone->getBody()); + + return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; + } + + /** + * @param string $zoneID + * @param string $hostnameID + * @return mixed + */ + public function getHostname(string $zoneID, string $hostnameID) + { + $zone = $this->adapter->get('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID); + $this->body = json_decode($zone->getBody()); + + return $this->body->result; + } + + /** + * @SuppressWarnings(PHPMD.BooleanArgumentFlag) + * + * @param string $zoneID + * @param string $hostnameID + * @param string $sslMethod + * @param string $sslType + * @return \stdClass + */ + public function updateHostname(string $zoneID, string $hostnameID, string $sslMethod = '', string $sslType = ''): \stdClass + { + $query = []; + + if (!empty($sslMethod)) { + $query['method'] = $sslMethod; + } + + if (!empty($sslType)) { + $query['type'] = $sslType; + } + + $options = [ + 'ssl' => $query + ]; + + $zone = $this->adapter->patch('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID, $options); + $this->body = json_decode($zone->getBody()); + return $this->body->result; + } + + /** + * @param string $zoneID + * @param string $hostnameID + * @return \stdClass + */ + public function deleteHostname(string $zoneID, string $hostnameID): \stdClass + { + $zone = $this->adapter->delete('zones/'.$zoneID.'/custom_hostnames/'.$hostnameID); + $this->body = json_decode($zone->getBody()); + return $this->body; + } +} diff --git a/src/Endpoints/Zones.php b/src/Endpoints/Zones.php index 19390b16..2335b1c8 100644 --- a/src/Endpoints/Zones.php +++ b/src/Endpoints/Zones.php @@ -217,7 +217,7 @@ public function setCachingLevel(string $zoneID, string $level = 'aggressive'): b */ public function cachePurgeEverything(string $zoneID): bool { - $user = $this->adapter->delete('zones/' . $zoneID . '/purge_cache', ['purge_everything' => true]); + $user = $this->adapter->post('zones/' . $zoneID . '/purge_cache', ['purge_everything' => true]); $this->body = json_decode($user->getBody()); @@ -247,7 +247,7 @@ public function cachePurge(string $zoneID, array $files = null, array $tags = nu $options['hosts'] = $hosts; } - $user = $this->adapter->delete('zones/' . $zoneID . '/purge_cache', $options); + $user = $this->adapter->post('zones/' . $zoneID . '/purge_cache', $options); $this->body = json_decode($user->getBody()); diff --git a/tests/Endpoints/CustomHostnamesTest.php b/tests/Endpoints/CustomHostnamesTest.php index faa247fb..4d3c6cee 100644 --- a/tests/Endpoints/CustomHostnamesTest.php +++ b/tests/Endpoints/CustomHostnamesTest.php @@ -1,138 +1,138 @@ -getPsr7JsonResponseForFixture('Endpoints/createCustomHostname.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/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames'), - $this->equalTo([ - 'hostname' => 'app.example.com', - 'ssl' => [ - 'method' => 'http', - 'type' => 'dv' - ] - ]) - ); - - $hostname = new CustomHostnames($mock); - $hostname->addHostname('023e105f4ecef8ad9ca31a8372d0c353', 'app.example.com', 'http', 'dv'); - $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $hostname->getBody()->result->id); - } - - public function testListHostnames() - { - $response = $this->getPsr7JsonResponseForFixture('Endpoints/listHostnames.json'); - - $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); - $mock->method('get')->willReturn($response); - - $mock->expects($this->once()) - ->method('get') - ->with( - $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames'), - $this->equalTo([ - 'hostname' => 'app.example.com', - 'id' => '0d89c70d-ad9f-4843-b99f-6cc0252067e9', - 'page' => 1, - 'per_page' => 20, - 'order' => 'ssl', - 'direction' => 'desc', - 'ssl' => 0 - ]) - ); - - $zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock); - $result = $zones->listHostnames('023e105f4ecef8ad9ca31a8372d0c353', 'app.example.com', '0d89c70d-ad9f-4843-b99f-6cc0252067e9', 1, 20, 'ssl', 'desc', 0); - - $this->assertObjectHasAttribute('result', $result); - $this->assertObjectHasAttribute('result_info', $result); - - $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $result->result[0]->id); - $this->assertEquals(1, $result->result_info->page); - $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $zones->getBody()->result[0]->id); - } - - public function testGetHostname() - { - $response = $this->getPsr7JsonResponseForFixture('Endpoints/getHostname.json'); - - $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); - $mock->method('get')->willReturn($response); - - $mock->expects($this->once()) - ->method('get') - ->with( - $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames/0d89c70d-ad9f-4843-b99f-6cc0252067e9') - ); - - $zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock); - $result = $zones->getHostname('023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9', '0d89c70d-ad9f-4843-b99f-6cc0252067e9'); - - $this->assertObjectHasAttribute('id', $result); - $this->assertObjectHasAttribute('hostname', $result); - $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $zones->getBody()->result->id); - } - - public function testUpdateHostname() - { - $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateHostname.json'); - - $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); - $mock->method('patch')->willReturn($response); - - $mock->expects($this->once()) - ->method('patch') - ->with( - $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames/0d89c70d-ad9f-4843-b99f-6cc0252067e9'), - $this->equalTo([ - 'ssl' => [ - 'method' => 'http', - 'type' => 'dv' - ] - ]) - ); - - $zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock); - $result = $zones->updateHostname('023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9', 'http', 'dv'); - - $this->assertObjectHasAttribute('id', $result); - $this->assertObjectHasAttribute('hostname', $result); - $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $zones->getBody()->result->id); - } - - public function testDeleteHostname() - { - $response = $this->getPsr7JsonResponseForFixture('Endpoints/deleteHostname.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/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames/0d89c70d-ad9f-4843-b99f-6cc0252067e9') - ); - - $zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock); - $result = $zones->deleteHostname('023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9'); - - $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $result->id); - $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $zones->getBody()->id); - } -} +getPsr7JsonResponseForFixture('Endpoints/createCustomHostname.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/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames'), + $this->equalTo([ + 'hostname' => 'app.example.com', + 'ssl' => [ + 'method' => 'http', + 'type' => 'dv' + ] + ]) + ); + + $hostname = new CustomHostnames($mock); + $hostname->addHostname('023e105f4ecef8ad9ca31a8372d0c353', 'app.example.com', 'http', 'dv'); + $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $hostname->getBody()->result->id); + } + + public function testListHostnames() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/listHostnames.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames'), + $this->equalTo([ + 'hostname' => 'app.example.com', + 'id' => '0d89c70d-ad9f-4843-b99f-6cc0252067e9', + 'page' => 1, + 'per_page' => 20, + 'order' => 'ssl', + 'direction' => 'desc', + 'ssl' => 0 + ]) + ); + + $zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock); + $result = $zones->listHostnames('023e105f4ecef8ad9ca31a8372d0c353', 'app.example.com', '0d89c70d-ad9f-4843-b99f-6cc0252067e9', 1, 20, 'ssl', 'desc', 0); + + $this->assertObjectHasAttribute('result', $result); + $this->assertObjectHasAttribute('result_info', $result); + + $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $result->result[0]->id); + $this->assertEquals(1, $result->result_info->page); + $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $zones->getBody()->result[0]->id); + } + + public function testGetHostname() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/getHostname.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames/0d89c70d-ad9f-4843-b99f-6cc0252067e9') + ); + + $zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock); + $result = $zones->getHostname('023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9', '0d89c70d-ad9f-4843-b99f-6cc0252067e9'); + + $this->assertObjectHasAttribute('id', $result); + $this->assertObjectHasAttribute('hostname', $result); + $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $zones->getBody()->result->id); + } + + public function testUpdateHostname() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateHostname.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('patch')->willReturn($response); + + $mock->expects($this->once()) + ->method('patch') + ->with( + $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames/0d89c70d-ad9f-4843-b99f-6cc0252067e9'), + $this->equalTo([ + 'ssl' => [ + 'method' => 'http', + 'type' => 'dv' + ] + ]) + ); + + $zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock); + $result = $zones->updateHostname('023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9', 'http', 'dv'); + + $this->assertObjectHasAttribute('id', $result); + $this->assertObjectHasAttribute('hostname', $result); + $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $zones->getBody()->result->id); + } + + public function testDeleteHostname() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/deleteHostname.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/023e105f4ecef8ad9ca31a8372d0c353/custom_hostnames/0d89c70d-ad9f-4843-b99f-6cc0252067e9') + ); + + $zones = new \Cloudflare\API\Endpoints\CustomHostnames($mock); + $result = $zones->deleteHostname('023e105f4ecef8ad9ca31a8372d0c353', '0d89c70d-ad9f-4843-b99f-6cc0252067e9'); + + $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $result->id); + $this->assertEquals('0d89c70d-ad9f-4843-b99f-6cc0252067e9', $zones->getBody()->id); + } +} diff --git a/tests/Endpoints/ZonesTest.php b/tests/Endpoints/ZonesTest.php index f6b75fc9..0deb4555 100644 --- a/tests/Endpoints/ZonesTest.php +++ b/tests/Endpoints/ZonesTest.php @@ -197,7 +197,7 @@ public function testCachePurgeEverything() $mock->method('delete')->willReturn($response); $mock->expects($this->once()) - ->method('delete') + ->method('post') ->with( $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'), $this->equalTo(['purge_everything' => true]) @@ -218,7 +218,7 @@ public function testCachePurgeHost() $mock->method('delete')->willReturn($response); $mock->expects($this->once()) - ->method('delete') + ->method('post') ->with( $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'), $this->equalTo( @@ -245,7 +245,7 @@ public function testCachePurge() $mock->method('delete')->willReturn($response); $mock->expects($this->once()) - ->method('delete') + ->method('post') ->with( $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/purge_cache'), $this->equalTo(['files' => [ From 919179644bfa0b903a13eee5dc2385ed1d0d7ecb Mon Sep 17 00:00:00 2001 From: Craig Menning Date: Mon, 11 May 2020 16:40:49 -0500 Subject: [PATCH 2/2] Fix tests for cache purge --- tests/Endpoints/ZonesTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Endpoints/ZonesTest.php b/tests/Endpoints/ZonesTest.php index 0deb4555..02addeba 100644 --- a/tests/Endpoints/ZonesTest.php +++ b/tests/Endpoints/ZonesTest.php @@ -194,7 +194,7 @@ public function testCachePurgeEverything() $response = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurgeEverything.json'); $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); - $mock->method('delete')->willReturn($response); + $mock->method('post')->willReturn($response); $mock->expects($this->once()) ->method('post') @@ -215,7 +215,7 @@ public function testCachePurgeHost() $response = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurgeHost.json'); $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); - $mock->method('delete')->willReturn($response); + $mock->method('post')->willReturn($response); $mock->expects($this->once()) ->method('post') @@ -242,7 +242,7 @@ public function testCachePurge() $response = $this->getPsr7JsonResponseForFixture('Endpoints/cachePurge.json'); $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); - $mock->method('delete')->willReturn($response); + $mock->method('post')->willReturn($response); $mock->expects($this->once()) ->method('post')