From 7f72427fa1dbea4ecbe94132de9952755c89708b Mon Sep 17 00:00:00 2001 From: Phil Young Date: Fri, 1 Oct 2021 01:16:27 +0100 Subject: [PATCH] Add Account Creation (#191) This is used on the tenant API but is currently undocumented on the main documentation --- src/Endpoints/Accounts.php | 13 +++++ tests/Endpoints/AccountsTest.php | 51 ++++++++++++++++++- .../Endpoints/createEnterpriseAccount.json | 13 +++++ .../Endpoints/createStandardAccount.json | 13 +++++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 tests/Fixtures/Endpoints/createEnterpriseAccount.json create mode 100644 tests/Fixtures/Endpoints/createStandardAccount.json diff --git a/src/Endpoints/Accounts.php b/src/Endpoints/Accounts.php index 4d4d80a..838afb9 100644 --- a/src/Endpoints/Accounts.php +++ b/src/Endpoints/Accounts.php @@ -21,6 +21,19 @@ class Accounts implements API $this->adapter = $adapter; } + public function addAccount(string $name, string $type = 'standard'): \stdClass + { + $options = [ + 'name' => $name, + 'type' => $type, + ]; + + $account = $this->adapter->post('accounts', $options); + $this->body = json_decode($account->getBody()); + + return $this->body->result; + } + public function listAccounts( int $page = 1, int $perPage = 20, diff --git a/tests/Endpoints/AccountsTest.php b/tests/Endpoints/AccountsTest.php index db2b1be..24f1f3c 100644 --- a/tests/Endpoints/AccountsTest.php +++ b/tests/Endpoints/AccountsTest.php @@ -1,4 +1,7 @@ listAccounts(1, 20, 'desc'); $this->assertObjectHasAttribute('result', $result); @@ -34,4 +37,50 @@ class AccountsTest extends TestCase $this->assertEquals(1, $result->result_info->page); $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $accounts->getBody()->result[0]->id); } + + public function testAddAccountWithDefaultType() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/createStandardAccount.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('post')->willReturn($response); + + $mock->expects($this->once()) + ->method('post') + ->with( + $this->equalTo('accounts'), + $this->equalTo([ + 'name' => 'Foo Bar', + 'type' => 'standard', + ]) + ); + + $accounts = new Accounts($mock); + + $accounts->addAccount('Foo Bar'); + $this->assertEquals('2bab6ace8c72ed3f09b9eca6db1396bb', $accounts->getBody()->result->id); + } + + public function testAddAccountWithCustomType() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/createEnterpriseAccount.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('post')->willReturn($response); + + $mock->expects($this->once()) + ->method('post') + ->with( + $this->equalTo('accounts'), + $this->equalTo([ + 'name' => 'Foo Bar', + 'type' => 'enterprise', + ]) + ); + + $accounts = new Accounts($mock); + + $accounts->addAccount('Foo Bar', 'enterprise'); + $this->assertEquals('2bab6ace8c72ed3f09b9eca6db1396bb', $accounts->getBody()->result->id); + } } diff --git a/tests/Fixtures/Endpoints/createEnterpriseAccount.json b/tests/Fixtures/Endpoints/createEnterpriseAccount.json new file mode 100644 index 0000000..8f41a7a --- /dev/null +++ b/tests/Fixtures/Endpoints/createEnterpriseAccount.json @@ -0,0 +1,13 @@ +{ + "result": { + "id": "2bab6ace8c72ed3f09b9eca6db1396bb", + "name": "Foo Bar", + "type": "enterprise", + "settings": { + "enforce_twofactor": false + } + }, + "success": true, + "errors": [], + "messages": [] +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/createStandardAccount.json b/tests/Fixtures/Endpoints/createStandardAccount.json new file mode 100644 index 0000000..e970a50 --- /dev/null +++ b/tests/Fixtures/Endpoints/createStandardAccount.json @@ -0,0 +1,13 @@ +{ + "result": { + "id": "2bab6ace8c72ed3f09b9eca6db1396bb", + "name": "Foo Bar", + "type": "standard", + "settings": { + "enforce_twofactor": false + } + }, + "success": true, + "errors": [], + "messages": [] +} \ No newline at end of file