Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Endpoints/Accounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,34 @@ public function addAccount(string $name, string $type = 'standard'): \stdClass
return $this->body->result;
}

public function updateAccount(
string $accountID,
string $name = null,
bool $enforceTwofactor = null,
bool $useAccountCustomNsByDefault = null
): \stdClass {
$data = [
'id' => $accountID
];

if ($name !== null) {
$data['name'] = $name;
}

if ($enforceTwofactor !== null) {
$data['settings']['enforce_twofactor'] = $enforceTwofactor;
}

if ($useAccountCustomNsByDefault !== null) {
$data['settings']['use_account_custom_ns_by_default'] = $useAccountCustomNsByDefault;
}

$query = $this->adapter->put('accounts/' . $accountID, $data);
$this->body = json_decode($query->getBody());

return $this->body->result;
}

public function listAccounts(
int $page = 1,
int $perPage = 20,
Expand Down
30 changes: 30 additions & 0 deletions tests/Endpoints/AccountsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,34 @@ public function testAddAccountWithCustomType()
$accounts->addAccount('Foo Bar', 'enterprise');
$this->assertEquals('2bab6ace8c72ed3f09b9eca6db1396bb', $accounts->getBody()->result->id);
}

public function testUpdateAccount()
{
$response = $this->getPsr7JsonResponseForFixture('Endpoints/updateAccount.json');

$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('put')->willReturn($response);

$data = [
'id' => '01a7362d577a6c3019a474fd6f485823',
'name' => 'Demo Account',
'settings' => [
'enforce_twofactor' => false,
'use_account_custom_ns_by_default' => false,
],
];

$mock->expects($this->once())
->method('put')
->with(
$this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823'),
$this->equalTo($data)
);

$accounts = new \Cloudflare\API\Endpoints\Accounts($mock);
$result = $accounts->updateAccount('01a7362d577a6c3019a474fd6f485823', 'Demo Account', false, false);

$this->assertEquals('01a7362d577a6c3019a474fd6f485823', $result->id);
$this->assertEquals('01a7362d577a6c3019a474fd6f485823', $accounts->getBody()->result->id);
}
}
18 changes: 18 additions & 0 deletions tests/Fixtures/Endpoints/updateAccount.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"success": true,
"errors": [
{}
],
"messages": [
{}
],
"result": {
"id": "01a7362d577a6c3019a474fd6f485823",
"name": "Demo Account",
"settings": {
"enforce_twofactor": false,
"use_account_custom_ns_by_default": false
},
"created_on": "2014-03-01T12:21:02.0000Z"
}
}