-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from linuxjuggler/master
Implementing the User's Account Memberships
- Loading branch information
Showing
8 changed files
with
410 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/.idea | ||
/vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace Cloudflare\API\Endpoints; | ||
|
||
use Cloudflare\API\Adapter\Adapter; | ||
use Cloudflare\API\Traits\BodyAccessorTrait; | ||
|
||
/* A list of memberships of accounts this user can access */ | ||
|
||
class Membership implements API | ||
{ | ||
use BodyAccessorTrait; | ||
|
||
private $adapter; | ||
|
||
public function __construct(Adapter $adapter) | ||
{ | ||
$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
|
||
class MembershipTest extends TestCase | ||
{ | ||
public function testListMemberships() | ||
{ | ||
$response = $this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"success": true, | ||
"errors": [], | ||
"messages": [], | ||
"result": { | ||
"id": "4536bcfad5faccb111b47003c79917fa" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
Oops, something went wrong.