Skip to content

Commit 7f72427

Browse files
authored
Add Account Creation (cloudflare#191)
This is used on the tenant API but is currently undocumented on the main documentation
1 parent d97cf24 commit 7f72427

4 files changed

Lines changed: 89 additions & 1 deletion

File tree

src/Endpoints/Accounts.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ public function __construct(Adapter $adapter)
2121
$this->adapter = $adapter;
2222
}
2323

24+
public function addAccount(string $name, string $type = 'standard'): \stdClass
25+
{
26+
$options = [
27+
'name' => $name,
28+
'type' => $type,
29+
];
30+
31+
$account = $this->adapter->post('accounts', $options);
32+
$this->body = json_decode($account->getBody());
33+
34+
return $this->body->result;
35+
}
36+
2437
public function listAccounts(
2538
int $page = 1,
2639
int $perPage = 20,

tests/Endpoints/AccountsTest.php

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
3+
use Cloudflare\API\Endpoints\Accounts;
4+
25
/**
36
* User: kanasite
47
* Date: 01/28/2019
@@ -24,7 +27,7 @@ public function testListZones()
2427
])
2528
);
2629

27-
$accounts = new \Cloudflare\API\Endpoints\Accounts($mock);
30+
$accounts = new Accounts($mock);
2831
$result = $accounts->listAccounts(1, 20, 'desc');
2932

3033
$this->assertObjectHasAttribute('result', $result);
@@ -34,4 +37,50 @@ public function testListZones()
3437
$this->assertEquals(1, $result->result_info->page);
3538
$this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $accounts->getBody()->result[0]->id);
3639
}
40+
41+
public function testAddAccountWithDefaultType()
42+
{
43+
$response = $this->getPsr7JsonResponseForFixture('Endpoints/createStandardAccount.json');
44+
45+
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
46+
$mock->method('post')->willReturn($response);
47+
48+
$mock->expects($this->once())
49+
->method('post')
50+
->with(
51+
$this->equalTo('accounts'),
52+
$this->equalTo([
53+
'name' => 'Foo Bar',
54+
'type' => 'standard',
55+
])
56+
);
57+
58+
$accounts = new Accounts($mock);
59+
60+
$accounts->addAccount('Foo Bar');
61+
$this->assertEquals('2bab6ace8c72ed3f09b9eca6db1396bb', $accounts->getBody()->result->id);
62+
}
63+
64+
public function testAddAccountWithCustomType()
65+
{
66+
$response = $this->getPsr7JsonResponseForFixture('Endpoints/createEnterpriseAccount.json');
67+
68+
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
69+
$mock->method('post')->willReturn($response);
70+
71+
$mock->expects($this->once())
72+
->method('post')
73+
->with(
74+
$this->equalTo('accounts'),
75+
$this->equalTo([
76+
'name' => 'Foo Bar',
77+
'type' => 'enterprise',
78+
])
79+
);
80+
81+
$accounts = new Accounts($mock);
82+
83+
$accounts->addAccount('Foo Bar', 'enterprise');
84+
$this->assertEquals('2bab6ace8c72ed3f09b9eca6db1396bb', $accounts->getBody()->result->id);
85+
}
3786
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"result": {
3+
"id": "2bab6ace8c72ed3f09b9eca6db1396bb",
4+
"name": "Foo Bar",
5+
"type": "enterprise",
6+
"settings": {
7+
"enforce_twofactor": false
8+
}
9+
},
10+
"success": true,
11+
"errors": [],
12+
"messages": []
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"result": {
3+
"id": "2bab6ace8c72ed3f09b9eca6db1396bb",
4+
"name": "Foo Bar",
5+
"type": "standard",
6+
"settings": {
7+
"enforce_twofactor": false
8+
}
9+
},
10+
"success": true,
11+
"errors": [],
12+
"messages": []
13+
}

0 commit comments

Comments
 (0)