Skip to content

Commit

Permalink
Merge pull request #78 from linuxjuggler/master
Browse files Browse the repository at this point in the history
Implementing the User's Account Memberships
  • Loading branch information
IcyApril authored May 8, 2019
2 parents ae2c680 + 6f1ac60 commit 7aa3a11
Show file tree
Hide file tree
Showing 8 changed files with 410 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/.idea
/vendor/
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -28,5 +29,10 @@
"classmap": [
"tests/"
]
},
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true
}
}
83 changes: 83 additions & 0 deletions src/Endpoints/Membership.php
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;
}
}
93 changes: 93 additions & 0 deletions tests/Endpoints/MembershipTest.php
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);
}
}
8 changes: 8 additions & 0 deletions tests/Fixtures/Endpoints/deleteMembership.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "4536bcfad5faccb111b47003c79917fa"
}
}
70 changes: 70 additions & 0 deletions tests/Fixtures/Endpoints/getMembershipDetails.json
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
}
}
}
}
78 changes: 78 additions & 0 deletions tests/Fixtures/Endpoints/listMemberships.json
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
}
}
Loading

0 comments on commit 7aa3a11

Please sign in to comment.