From 9311583f43242925640631ceb065ab1a46453e04 Mon Sep 17 00:00:00 2001 From: Zaher Ghaibeh Date: Sun, 13 Jan 2019 10:21:37 +0300 Subject: [PATCH 1/2] adding the .idea file --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 42cd73d9..3aa184ce 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +/.idea /vendor/ \ No newline at end of file From 6f1ac605f5bb1e7c42a3d99b79f46bd23c73f75c Mon Sep 17 00:00:00 2001 From: Zaher Ghaibeh Date: Sun, 13 Jan 2019 12:31:18 +0300 Subject: [PATCH 2/2] adding User's Account Memberships --- composer.json | 8 +- src/Endpoints/Membership.php | 83 +++++++++++++++++ tests/Endpoints/MembershipTest.php | 93 +++++++++++++++++++ .../Fixtures/Endpoints/deleteMembership.json | 8 ++ .../Endpoints/getMembershipDetails.json | 70 ++++++++++++++ tests/Fixtures/Endpoints/listMemberships.json | 78 ++++++++++++++++ .../Endpoints/updateMembershipStatus.json | 70 ++++++++++++++ 7 files changed, 409 insertions(+), 1 deletion(-) create mode 100644 src/Endpoints/Membership.php create mode 100644 tests/Endpoints/MembershipTest.php create mode 100644 tests/Fixtures/Endpoints/deleteMembership.json create mode 100644 tests/Fixtures/Endpoints/getMembershipDetails.json create mode 100644 tests/Fixtures/Endpoints/listMemberships.json create mode 100644 tests/Fixtures/Endpoints/updateMembershipStatus.json diff --git a/composer.json b/composer.json index a6dbaddb..642dab3f 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,8 @@ "require": { "guzzlehttp/guzzle": "^6.2.2", "php": ">=7.0.0", - "psr/http-message": "~1.0" + "psr/http-message": "~1.0", + "ext-json": "*" }, "require-dev": { "phpunit/phpunit": "5.7.5", @@ -28,5 +29,10 @@ "classmap": [ "tests/" ] + }, + "config": { + "preferred-install": "dist", + "sort-packages": true, + "optimize-autoloader": true } } diff --git a/src/Endpoints/Membership.php b/src/Endpoints/Membership.php new file mode 100644 index 00000000..6a783cb8 --- /dev/null +++ b/src/Endpoints/Membership.php @@ -0,0 +1,83 @@ +adapter = $adapter; + } + + + public function listMemberships( + string $name = '', + string $status = '', + int $page = 1, + int $perPage = 20, + string $order = '', + string $direction = '' + ): \stdClass { + $query = [ + 'page' => $page, + 'per_page' => $perPage + ]; + + if (!empty($name)) { + $query['account.name'] = $name; + } + + if (!empty($status) && in_array($status, ['accepted', 'pending', 'rejected'], true)) { + $query['status'] = $status; + } + + if (!empty($order) && in_array($order, ['id', 'account.name', 'status'], true)) { + $query['order'] = $order; + } + + if (!empty($direction) && in_array($direction, ['asc', 'desc'], true)) { + $query['direction'] = $direction; + } + + $memberships = $this->adapter->get('memberships', $query); + $this->body = json_decode($memberships->getBody()); + + return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; + } + + public function getMembershipDetails(string $membershipId): \stdClass + { + $membership = $this->adapter->get(sprintf('memberships/%s', $membershipId)); + $this->body = json_decode($membership->getBody()); + return $this->body->result; + } + + public function updateMembershipStatus(string $membershipId, string $status): \stdClass + { + $response = $this->adapter->put(sprintf('memberships/%s', $membershipId), ['status' => $status]); + $this->body = json_decode($response->getBody()); + return $this->body; + } + + public function deleteMembership(string $membershipId): bool + { + $response = $this->adapter->delete(sprintf('memberships/%s', $membershipId)); + + $this->body = json_decode($response->getBody()); + + if (isset($this->body->result->id)) { + return true; + } + + return false; + } +} diff --git a/tests/Endpoints/MembershipTest.php b/tests/Endpoints/MembershipTest.php new file mode 100644 index 00000000..3f93c1e4 --- /dev/null +++ b/tests/Endpoints/MembershipTest.php @@ -0,0 +1,93 @@ +getPsr7JsonResponseForFixture('Endpoints/listMemberships.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('memberships'), + $this->equalTo([ + 'page' => 1, + 'per_page' => 20, + 'account.name' => 'Demo Account', + 'status' => 'accepted', + 'order' => 'status', + 'direction' => 'desc', + ]) + ); + + $zones = new \Cloudflare\API\Endpoints\Membership($mock); + $result = $zones->listMemberships('Demo Account', 'accepted', 1, 20, 'status', 'desc'); + + $this->assertObjectHasAttribute('result', $result); + $this->assertObjectHasAttribute('result_info', $result); + + $this->assertEquals('4536bcfad5faccb111b47003c79917fa', $result->result[0]->id); + $this->assertEquals(1, $result->result_info->page); + $this->assertEquals('4536bcfad5faccb111b47003c79917fa', $zones->getBody()->result[0]->id); + } + + public function testGetMembershipDetails() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/getMembershipDetails.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $membership = new \Cloudflare\API\Endpoints\Membership($mock); + $details = $membership->getMembershipDetails('4536bcfad5faccb111b47003c79917fa'); + + $this->assertObjectHasAttribute('id', $details); + $this->assertEquals('4536bcfad5faccb111b47003c79917fa', $details->id); + $this->assertObjectHasAttribute('code', $details); + $this->assertEquals('05dd05cce12bbed97c0d87cd78e89bc2fd41a6cee72f27f6fc84af2e45c0fac0', $details->code); + $this->assertEquals('4536bcfad5faccb111b47003c79917fa', $membership->getBody()->result->id); + } + + public function testUpdateMembershipDetails() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateMembershipStatus.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('put')->willReturn($response); + + $mock->expects($this->once()) + ->method('put') + ->with( + $this->equalTo('memberships/4536bcfad5faccb111b47003c79917fa'), + $this->equalTo([ + 'status' => 'accepted' + ]) + ); + + $membership = new \Cloudflare\API\Endpoints\Membership($mock); + $membership->updateMembershipStatus('4536bcfad5faccb111b47003c79917fa', 'accepted'); + $this->assertEquals('4536bcfad5faccb111b47003c79917fa', $membership->getBody()->result->id); + } + + public function testDeleteMembership() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/deleteMembership.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('delete')->willReturn($response); + + $mock->expects($this->once()) + ->method('delete') + ->with($this->equalTo('memberships/4536bcfad5faccb111b47003c79917fa')); + + $membership = new \Cloudflare\API\Endpoints\Membership($mock); + + $membership->deleteMembership('4536bcfad5faccb111b47003c79917fa'); + + $this->assertEquals('4536bcfad5faccb111b47003c79917fa', $membership->getBody()->result->id); + } +} diff --git a/tests/Fixtures/Endpoints/deleteMembership.json b/tests/Fixtures/Endpoints/deleteMembership.json new file mode 100644 index 00000000..02111aa8 --- /dev/null +++ b/tests/Fixtures/Endpoints/deleteMembership.json @@ -0,0 +1,8 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "4536bcfad5faccb111b47003c79917fa" + } +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/getMembershipDetails.json b/tests/Fixtures/Endpoints/getMembershipDetails.json new file mode 100644 index 00000000..b3d65f36 --- /dev/null +++ b/tests/Fixtures/Endpoints/getMembershipDetails.json @@ -0,0 +1,70 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "4536bcfad5faccb111b47003c79917fa", + "code": "05dd05cce12bbed97c0d87cd78e89bc2fd41a6cee72f27f6fc84af2e45c0fac0", + "status": "accepted", + "account": { + "id": "01a7362d577a6c3019a474fd6f485823", + "name": "Demo Account", + "settings": { + "enforce_twofactor": false + } + }, + "roles": [ + "Account Administrator" + ], + "permissions": { + "analytics": { + "read": true, + "write": true + }, + "billing": { + "read": true, + "write": true + }, + "cache_purge": { + "read": true, + "write": true + }, + "dns": { + "read": true, + "write": true + }, + "dns_records": { + "read": true, + "write": true + }, + "lb": { + "read": true, + "write": true + }, + "logs": { + "read": true, + "write": true + }, + "organization": { + "read": true, + "write": true + }, + "ssl": { + "read": true, + "write": true + }, + "waf": { + "read": true, + "write": true + }, + "zones": { + "read": true, + "write": true + }, + "zone_settings": { + "read": true, + "write": true + } + } +} +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/listMemberships.json b/tests/Fixtures/Endpoints/listMemberships.json new file mode 100644 index 00000000..0e209d38 --- /dev/null +++ b/tests/Fixtures/Endpoints/listMemberships.json @@ -0,0 +1,78 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": [ + { + "id": "4536bcfad5faccb111b47003c79917fa", + "code": "05dd05cce12bbed97c0d87cd78e89bc2fd41a6cee72f27f6fc84af2e45c0fac0", + "status": "accepted", + "account": { + "id": "01a7362d577a6c3019a474fd6f485823", + "name": "Demo Account", + "settings": { + "enforce_twofactor": false + } + }, + "roles": [ + "Account Administrator" + ], + "permissions": { + "analytics": { + "read": true, + "write": true + }, + "billing": { + "read": true, + "write": true + }, + "cache_purge": { + "read": true, + "write": true + }, + "dns": { + "read": true, + "write": true + }, + "dns_records": { + "read": true, + "write": true + }, + "lb": { + "read": true, + "write": true + }, + "logs": { + "read": true, + "write": true + }, + "organization": { + "read": true, + "write": true + }, + "ssl": { + "read": true, + "write": true + }, + "waf": { + "read": true, + "write": true + }, + "zones": { + "read": true, + "write": true + }, + "zone_settings": { + "read": true, + "write": true + } + } + } + ], + "result_info": { + "page": 1, + "per_page": 20, + "count": 1, + "total_count": 2000 + } +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/updateMembershipStatus.json b/tests/Fixtures/Endpoints/updateMembershipStatus.json new file mode 100644 index 00000000..978f58f3 --- /dev/null +++ b/tests/Fixtures/Endpoints/updateMembershipStatus.json @@ -0,0 +1,70 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "4536bcfad5faccb111b47003c79917fa", + "code": "05dd05cce12bbed97c0d87cd78e89bc2fd41a6cee72f27f6fc84af2e45c0fac0", + "status": "accepted", + "account": { + "id": "01a7362d577a6c3019a474fd6f485823", + "name": "Demo Account", + "settings": { + "enforce_twofactor": false + } + }, + "roles": [ + "Account Administrator" + ], + "permissions": { + "analytics": { + "read": true, + "write": true + }, + "billing": { + "read": true, + "write": true + }, + "cache_purge": { + "read": true, + "write": true + }, + "dns": { + "read": true, + "write": true + }, + "dns_records": { + "read": true, + "write": true + }, + "lb": { + "read": true, + "write": true + }, + "logs": { + "read": true, + "write": true + }, + "organization": { + "read": true, + "write": true + }, + "ssl": { + "read": true, + "write": true + }, + "waf": { + "read": true, + "write": true + }, + "zones": { + "read": true, + "write": true + }, + "zone_settings": { + "read": true, + "write": true + } + } + } +} \ No newline at end of file