From dde2de44955ee8c7d7092d968de7a58784c6a517 Mon Sep 17 00:00:00 2001 From: Kyle Yee Date: Mon, 28 Jan 2019 11:24:45 +0800 Subject: [PATCH 01/11] Feature: add accounts end point, add get domains --- src/Endpoints/Accounts.php | 61 ++++++++++++++++++++++ tests/Endpoints/AccountsTest.php | 37 +++++++++++++ tests/Fixtures/Endpoints/listAccounts.json | 20 +++++++ 3 files changed, 118 insertions(+) create mode 100644 src/Endpoints/Accounts.php create mode 100644 tests/Endpoints/AccountsTest.php create mode 100644 tests/Fixtures/Endpoints/listAccounts.json diff --git a/src/Endpoints/Accounts.php b/src/Endpoints/Accounts.php new file mode 100644 index 0000000..4ded300 --- /dev/null +++ b/src/Endpoints/Accounts.php @@ -0,0 +1,61 @@ +adapter = $adapter; + } + + public function listAccounts( + int $page = 1, + int $perPage = 20, + string $direction = '' + ): \stdClass { + $query = [ + 'page' => $page, + 'per_page' => $perPage + ]; + + if (!empty($direction)) { + $query['direction'] = $direction; + } + + $user = $this->adapter->get('accounts', $query); + $this->body = json_decode($user->getBody()); + + return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; + } + + public function getDomains(string $accountID): \stdClass + { + $response = $this->adapter->get('accounts/' . $accountID . '/registrar/domains'); + + $this->body = $response->getBody(); + + return json_decode($this->body)->result; + } + + public function getDomainDetails(string $accountID, string $domainName): \stdClass + { + $response = $this->adapter->get('accounts/' . $accountID . '/registrar/domains' . $domainName); + + $this->body = $response->getBody(); + + return json_decode($this->body)->result; + } +} diff --git a/tests/Endpoints/AccountsTest.php b/tests/Endpoints/AccountsTest.php new file mode 100644 index 0000000..db2b1be --- /dev/null +++ b/tests/Endpoints/AccountsTest.php @@ -0,0 +1,37 @@ +getPsr7JsonResponseForFixture('Endpoints/listAccounts.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('accounts'), + $this->equalTo([ + 'page' => 1, + 'per_page' => 20, + 'direction' => 'desc', + ]) + ); + + $accounts = new \Cloudflare\API\Endpoints\Accounts($mock); + $result = $accounts->listAccounts(1, 20, 'desc'); + + $this->assertObjectHasAttribute('result', $result); + $this->assertObjectHasAttribute('result_info', $result); + + $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $result->result[0]->id); + $this->assertEquals(1, $result->result_info->page); + $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $accounts->getBody()->result[0]->id); + } +} diff --git a/tests/Fixtures/Endpoints/listAccounts.json b/tests/Fixtures/Endpoints/listAccounts.json new file mode 100644 index 0000000..0e6275a --- /dev/null +++ b/tests/Fixtures/Endpoints/listAccounts.json @@ -0,0 +1,20 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": [ + { + "id": "023e105f4ecef8ad9ca31a8372d0c353", + "name": "Example Account", + "settings": { + "enforce_twofactor": true + } + } + ], + "result_info": { + "page": 1, + "per_page": 20, + "count": 1, + "total_count": 2000 + } +} From fb45932677328e317719ceac624c95a0ce697c9e Mon Sep 17 00:00:00 2001 From: Kyle Yee Date: Mon, 28 Jan 2019 11:35:58 +0800 Subject: [PATCH 02/11] cast result as object --- src/Endpoints/Accounts.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Endpoints/Accounts.php b/src/Endpoints/Accounts.php index 4ded300..3004ea9 100644 --- a/src/Endpoints/Accounts.php +++ b/src/Endpoints/Accounts.php @@ -47,7 +47,7 @@ class Accounts implements API $this->body = $response->getBody(); - return json_decode($this->body)->result; + return (object)['result' => $this->body->result]; } public function getDomainDetails(string $accountID, string $domainName): \stdClass @@ -56,6 +56,6 @@ class Accounts implements API $this->body = $response->getBody(); - return json_decode($this->body)->result; + return (object)['result' => $this->body->result]; } } From 44eb77f9ca43e36a7bffcbc625d93c06f337b26e Mon Sep 17 00:00:00 2001 From: Kyle Yee Date: Mon, 28 Jan 2019 11:39:49 +0800 Subject: [PATCH 03/11] cast response as array instead of object --- src/Endpoints/Accounts.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Endpoints/Accounts.php b/src/Endpoints/Accounts.php index 3004ea9..fd42189 100644 --- a/src/Endpoints/Accounts.php +++ b/src/Endpoints/Accounts.php @@ -41,21 +41,21 @@ class Accounts implements API return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; } - public function getDomains(string $accountID): \stdClass + public function getDomains(string $accountID): array { $response = $this->adapter->get('accounts/' . $accountID . '/registrar/domains'); - $this->body = $response->getBody(); + $this->body = json_decode($response->getBody()); - return (object)['result' => $this->body->result]; + return $this->body->result; } - public function getDomainDetails(string $accountID, string $domainName): \stdClass + public function getDomainDetails(string $accountID, string $domainName): array { $response = $this->adapter->get('accounts/' . $accountID . '/registrar/domains' . $domainName); - $this->body = $response->getBody(); + $this->body = json_decode($response->getBody()); - return (object)['result' => $this->body->result]; + return $this->body->result; } } From b04abe73bcfb3f8b60b5e02f628759468fa5326e Mon Sep 17 00:00:00 2001 From: Kyle Yee Date: Thu, 6 Jun 2019 15:00:57 +0800 Subject: [PATCH 04/11] Feature: add pause --- src/Endpoints/Zones.php | 15 +++++++++++++++ tests/Endpoints/ZonesTest.php | 23 +++++++++++++++++++++++ tests/Fixtures/Endpoints/pauseTest.json | 8 ++++++++ 3 files changed, 46 insertions(+) create mode 100644 tests/Fixtures/Endpoints/pauseTest.json diff --git a/src/Endpoints/Zones.php b/src/Endpoints/Zones.php index 8595cb6..22db2ee 100644 --- a/src/Endpoints/Zones.php +++ b/src/Endpoints/Zones.php @@ -58,6 +58,21 @@ class Zones implements API return false; } + public function pause(string $zoneID, bool $paused = true): bool + { + $options = [ + 'paused' => $paused, + ]; + $user = $this->adapter->patch('zones/' . $zoneID, $options); + $this->body = json_decode($user->getBody()); + + if (isset($this->body->result->id)) { + return true; + } + + return false; + } + public function listZones( string $name = '', string $status = '', diff --git a/tests/Endpoints/ZonesTest.php b/tests/Endpoints/ZonesTest.php index 0bda651..a95f690 100644 --- a/tests/Endpoints/ZonesTest.php +++ b/tests/Endpoints/ZonesTest.php @@ -49,6 +49,29 @@ class ZonesTest extends TestCase $this->assertEquals('9a7806061c88ada191ed06f989cc3dac', $zones->getBody()->result->id); } + public function testPauseTest() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/pauseTest.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/c2547eb745079dac9320b638f5e225cf483cc5cfdda41'), + $this->equalTo([ + 'pause' => true, + ]) + ); + + $zones = new \Cloudflare\API\Endpoints\Zones($mock); + $result = $zones->pause('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', true); + + $this->assertTrue($result); + $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id); + } + public function testActivationTest() { $response = $this->getPsr7JsonResponseForFixture('Endpoints/activationTest.json'); diff --git a/tests/Fixtures/Endpoints/pauseTest.json b/tests/Fixtures/Endpoints/pauseTest.json new file mode 100644 index 0000000..bedd2f5 --- /dev/null +++ b/tests/Fixtures/Endpoints/pauseTest.json @@ -0,0 +1,8 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "023e105f4ecef8ad9ca31a8372d0c353" + } +} From f4c2f546b0ca75c70a5ca1cce50962003848fafb Mon Sep 17 00:00:00 2001 From: Kyle Yee Date: Sat, 14 Dec 2019 15:24:43 +0800 Subject: [PATCH 05/11] Feature: add lock and unlock domain --- src/Endpoints/Accounts.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Endpoints/Accounts.php b/src/Endpoints/Accounts.php index fd42189..2264534 100644 --- a/src/Endpoints/Accounts.php +++ b/src/Endpoints/Accounts.php @@ -58,4 +58,18 @@ class Accounts implements API return $this->body->result; } + + public function lockDomain(string $accountID, string $domainName): array + { + $response = $this->adapter->put('accounts/' . $accountID . '/registrar/domains' . $domainName, ["locked" => true]); + $this->body = json_decode($response->getBody()); + return $this->body; + } + + public function unlockDomain(string $accountID, string $domainName): array + { + $response = $this->adapter->put('accounts/' . $accountID . '/registrar/domains' . $domainName, ["locked" => false]); + $this->body = json_decode($response->getBody()); + return $this->body; + } } From d918f7b99e8eab0f89265f2177118f74c52fe9b0 Mon Sep 17 00:00:00 2001 From: Kyle Yee Date: Sat, 14 Dec 2019 15:36:38 +0800 Subject: [PATCH 06/11] fix typo --- src/Endpoints/Accounts.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Endpoints/Accounts.php b/src/Endpoints/Accounts.php index 2264534..9509feb 100644 --- a/src/Endpoints/Accounts.php +++ b/src/Endpoints/Accounts.php @@ -52,7 +52,7 @@ class Accounts implements API public function getDomainDetails(string $accountID, string $domainName): array { - $response = $this->adapter->get('accounts/' . $accountID . '/registrar/domains' . $domainName); + $response = $this->adapter->get('accounts/' . $accountID . '/registrar/domains/' . $domainName); $this->body = json_decode($response->getBody()); @@ -61,14 +61,14 @@ class Accounts implements API public function lockDomain(string $accountID, string $domainName): array { - $response = $this->adapter->put('accounts/' . $accountID . '/registrar/domains' . $domainName, ["locked" => true]); + $response = $this->adapter->put('accounts/' . $accountID . '/registrar/domains/' . $domainName, ["locked" => true]); $this->body = json_decode($response->getBody()); return $this->body; } public function unlockDomain(string $accountID, string $domainName): array { - $response = $this->adapter->put('accounts/' . $accountID . '/registrar/domains' . $domainName, ["locked" => false]); + $response = $this->adapter->put('accounts/' . $accountID . '/registrar/domains/' . $domainName, ["locked" => false]); $this->body = json_decode($response->getBody()); return $this->body; } From f0892ac64fb63c794c654ffe66d59505b4ced3ba Mon Sep 17 00:00:00 2001 From: Kyle Yee Date: Sat, 14 Dec 2019 15:40:29 +0800 Subject: [PATCH 07/11] fix return type --- src/Endpoints/Accounts.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Endpoints/Accounts.php b/src/Endpoints/Accounts.php index 9509feb..1bfd61f 100644 --- a/src/Endpoints/Accounts.php +++ b/src/Endpoints/Accounts.php @@ -50,7 +50,7 @@ class Accounts implements API return $this->body->result; } - public function getDomainDetails(string $accountID, string $domainName): array + public function getDomainDetails(string $accountID, string $domainName): \stdClass { $response = $this->adapter->get('accounts/' . $accountID . '/registrar/domains/' . $domainName); @@ -59,14 +59,14 @@ class Accounts implements API return $this->body->result; } - public function lockDomain(string $accountID, string $domainName): array + public function lockDomain(string $accountID, string $domainName): \stdClass { $response = $this->adapter->put('accounts/' . $accountID . '/registrar/domains/' . $domainName, ["locked" => true]); $this->body = json_decode($response->getBody()); return $this->body; } - public function unlockDomain(string $accountID, string $domainName): array + public function unlockDomain(string $accountID, string $domainName): \stdClass { $response = $this->adapter->put('accounts/' . $accountID . '/registrar/domains/' . $domainName, ["locked" => false]); $this->body = json_decode($response->getBody()); From b54c3481a39f01bc372c98fc287fca45ec9b8a2d Mon Sep 17 00:00:00 2001 From: Kyle Yee Date: Mon, 16 Dec 2019 17:39:29 +0800 Subject: [PATCH 08/11] fix typo in test case --- tests/Endpoints/ZonesTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Endpoints/ZonesTest.php b/tests/Endpoints/ZonesTest.php index d0158bf..906d01a 100644 --- a/tests/Endpoints/ZonesTest.php +++ b/tests/Endpoints/ZonesTest.php @@ -61,7 +61,7 @@ class ZonesTest extends TestCase ->with( $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41'), $this->equalTo([ - 'pause' => true, + 'paused' => true, ]) ); From b9051fc1f2d32b52a4dbad173b636e6eb7966c97 Mon Sep 17 00:00:00 2001 From: Kyle Yee Date: Mon, 16 Dec 2019 17:55:48 +0800 Subject: [PATCH 09/11] fix lint --- src/Endpoints/Accounts.php | 4 ++-- src/Endpoints/Zones.php | 21 +++++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/Endpoints/Accounts.php b/src/Endpoints/Accounts.php index 1bfd61f..4d4d80a 100644 --- a/src/Endpoints/Accounts.php +++ b/src/Endpoints/Accounts.php @@ -61,14 +61,14 @@ class Accounts implements API public function lockDomain(string $accountID, string $domainName): \stdClass { - $response = $this->adapter->put('accounts/' . $accountID . '/registrar/domains/' . $domainName, ["locked" => true]); + $response = $this->adapter->put('accounts/' . $accountID . '/registrar/domains/' . $domainName, ['locked' => true]); $this->body = json_decode($response->getBody()); return $this->body; } public function unlockDomain(string $accountID, string $domainName): \stdClass { - $response = $this->adapter->put('accounts/' . $accountID . '/registrar/domains/' . $domainName, ["locked" => false]); + $response = $this->adapter->put('accounts/' . $accountID . '/registrar/domains/' . $domainName, ['locked' => false]); $this->body = json_decode($response->getBody()); return $this->body; } diff --git a/src/Endpoints/Zones.php b/src/Endpoints/Zones.php index bbd3564..19390b1 100644 --- a/src/Endpoints/Zones.php +++ b/src/Endpoints/Zones.php @@ -58,12 +58,9 @@ class Zones implements API return false; } - public function pause(string $zoneID, bool $paused = true): bool + public function pause(string $zoneID): bool { - $options = [ - 'paused' => $paused, - ]; - $user = $this->adapter->patch('zones/' . $zoneID, $options); + $user = $this->adapter->patch('zones/' . $zoneID, ['paused' => true]); $this->body = json_decode($user->getBody()); if (isset($this->body->result->id)) { @@ -72,7 +69,19 @@ class Zones implements API return false; } - + + public function unpause(string $zoneID): bool + { + $user = $this->adapter->patch('zones/' . $zoneID, ['paused' => false]); + $this->body = json_decode($user->getBody()); + + if (isset($this->body->result->id)) { + return true; + } + + return false; + } + public function getZoneById( string $zoneId ): \stdClass { From 6e6a850d623e02fbf2bfe9b5c23eab59ec413c4a Mon Sep 17 00:00:00 2001 From: Kyle Yee Date: Mon, 16 Dec 2019 17:58:04 +0800 Subject: [PATCH 10/11] fix test case --- tests/Endpoints/ZonesTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Endpoints/ZonesTest.php b/tests/Endpoints/ZonesTest.php index 906d01a..d44e169 100644 --- a/tests/Endpoints/ZonesTest.php +++ b/tests/Endpoints/ZonesTest.php @@ -66,7 +66,7 @@ class ZonesTest extends TestCase ); $zones = new \Cloudflare\API\Endpoints\Zones($mock); - $result = $zones->pause('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', true); + $result = $zones->pause('c2547eb745079dac9320b638f5e225cf483cc5cfdda41'); $this->assertTrue($result); $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id); From 9d0df448383f92776ea24552e9b94fefe4a43436 Mon Sep 17 00:00:00 2001 From: Kyle Yee Date: Mon, 16 Dec 2019 18:02:49 +0800 Subject: [PATCH 11/11] remove test --- tests/Endpoints/ZonesTest.php | 23 ----------------------- tests/Fixtures/Endpoints/pauseTest.json | 8 -------- 2 files changed, 31 deletions(-) delete mode 100644 tests/Fixtures/Endpoints/pauseTest.json diff --git a/tests/Endpoints/ZonesTest.php b/tests/Endpoints/ZonesTest.php index d44e169..f6b75fc 100644 --- a/tests/Endpoints/ZonesTest.php +++ b/tests/Endpoints/ZonesTest.php @@ -49,29 +49,6 @@ class ZonesTest extends TestCase $this->assertEquals('9a7806061c88ada191ed06f989cc3dac', $zones->getBody()->result->id); } - public function testPauseTest() - { - $response = $this->getPsr7JsonResponseForFixture('Endpoints/pauseTest.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/c2547eb745079dac9320b638f5e225cf483cc5cfdda41'), - $this->equalTo([ - 'paused' => true, - ]) - ); - - $zones = new \Cloudflare\API\Endpoints\Zones($mock); - $result = $zones->pause('c2547eb745079dac9320b638f5e225cf483cc5cfdda41'); - - $this->assertTrue($result); - $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id); - } - public function testActivationTest() { $response = $this->getPsr7JsonResponseForFixture('Endpoints/activationTest.json'); diff --git a/tests/Fixtures/Endpoints/pauseTest.json b/tests/Fixtures/Endpoints/pauseTest.json deleted file mode 100644 index bedd2f5..0000000 --- a/tests/Fixtures/Endpoints/pauseTest.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "success": true, - "errors": [], - "messages": [], - "result": { - "id": "023e105f4ecef8ad9ca31a8372d0c353" - } -}