From 6eebe39e6891680c84cefc4972316a9b676824bd Mon Sep 17 00:00:00 2001 From: RSL Date: Mon, 28 Feb 2022 13:28:39 +0000 Subject: [PATCH 1/2] Update Accounts I want a method to update accounts. Issue: #205 --- src/Endpoints/Accounts.php | 32 +++++++++++++++++++++ tests/Endpoints/AccountsTest.php | 30 +++++++++++++++++++ tests/Fixtures/Endpoints/updateAccount.json | 18 ++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 tests/Fixtures/Endpoints/updateAccount.json diff --git a/src/Endpoints/Accounts.php b/src/Endpoints/Accounts.php index 838afb9c..939cc6dc 100644 --- a/src/Endpoints/Accounts.php +++ b/src/Endpoints/Accounts.php @@ -34,6 +34,38 @@ 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 + ): bool { + $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()); + + if (isset($this->body->result->id)) { + return true; + } + + return false; + } + public function listAccounts( int $page = 1, int $perPage = 20, diff --git a/tests/Endpoints/AccountsTest.php b/tests/Endpoints/AccountsTest.php index 24f1f3ca..08f23c6b 100644 --- a/tests/Endpoints/AccountsTest.php +++ b/tests/Endpoints/AccountsTest.php @@ -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->assertTrue($result); + $this->assertEquals('01a7362d577a6c3019a474fd6f485823', $accounts->getBody()->result->id); + } } diff --git a/tests/Fixtures/Endpoints/updateAccount.json b/tests/Fixtures/Endpoints/updateAccount.json new file mode 100644 index 00000000..d831edd2 --- /dev/null +++ b/tests/Fixtures/Endpoints/updateAccount.json @@ -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" + } +} From 617c982700300655870860434f1fd54584fb3a76 Mon Sep 17 00:00:00 2001 From: RSL Date: Mon, 28 Feb 2022 14:52:09 +0000 Subject: [PATCH 2/2] Updated response --- src/Endpoints/Accounts.php | 8 ++------ tests/Endpoints/AccountsTest.php | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Endpoints/Accounts.php b/src/Endpoints/Accounts.php index 939cc6dc..0bdbdb38 100644 --- a/src/Endpoints/Accounts.php +++ b/src/Endpoints/Accounts.php @@ -39,7 +39,7 @@ public function updateAccount( string $name = null, bool $enforceTwofactor = null, bool $useAccountCustomNsByDefault = null - ): bool { + ): \stdClass { $data = [ 'id' => $accountID ]; @@ -59,11 +59,7 @@ public function updateAccount( $query = $this->adapter->put('accounts/' . $accountID, $data); $this->body = json_decode($query->getBody()); - if (isset($this->body->result->id)) { - return true; - } - - return false; + return $this->body->result; } public function listAccounts( diff --git a/tests/Endpoints/AccountsTest.php b/tests/Endpoints/AccountsTest.php index 08f23c6b..13553dd1 100644 --- a/tests/Endpoints/AccountsTest.php +++ b/tests/Endpoints/AccountsTest.php @@ -110,7 +110,7 @@ public function testUpdateAccount() $accounts = new \Cloudflare\API\Endpoints\Accounts($mock); $result = $accounts->updateAccount('01a7362d577a6c3019a474fd6f485823', 'Demo Account', false, false); - $this->assertTrue($result); + $this->assertEquals('01a7362d577a6c3019a474fd6f485823', $result->id); $this->assertEquals('01a7362d577a6c3019a474fd6f485823', $accounts->getBody()->result->id); } }