diff --git a/src/Endpoints/RulesLists.php b/src/Endpoints/RulesLists.php index 6ef83d1..b019b95 100755 --- a/src/Endpoints/RulesLists.php +++ b/src/Endpoints/RulesLists.php @@ -18,15 +18,15 @@ class RulesLists implements API public function getLists(string $accountId) { - $response = $this->adapter->get('/accounts/' . $accountId . '/rules/lists'); + $response = $this->adapter->get('accounts/' . $accountId . '/rules/lists'); $this->body = json_decode($response->getBody()); - return (object)['result' => $this->body->result]; + return $this->body->result; } public function getListDetails(string $accountId, string $listId) { - $response = $this->adapter->get('/accounts/' . $accountId . '/rules/lists/' . $listId); + $response = $this->adapter->get('accounts/' . $accountId . '/rules/lists/' . $listId); $this->body = json_decode($response->getBody()); return $this->body->result; @@ -47,10 +47,10 @@ class RulesLists implements API $options['cursor'] = $cursor; } - $response = $this->adapter->get('/accounts/' . $accountId . '/rules/lists/' . $listId . '/items', $options); + $response = $this->adapter->get('accounts/' . $accountId . '/rules/lists/' . $listId . '/items', $options); $this->body = json_decode($response->getBody()); - return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info]; + return (object)['result' => $this->body->result, 'result_info' => $this->body->result_info ?? null]; } public function createList(string $accountId, string $kind, string $name, string $description = '') @@ -64,7 +64,16 @@ class RulesLists implements API $options['description'] = $description; } - $response = $this->adapter->post('/accounts/' . $accountId . '/rules/lists', $options); + $response = $this->adapter->post('accounts/' . $accountId . '/rules/lists', $options); + $this->body = json_decode($response->getBody()); + + return $this->body->result; + } + + public function deleteList(string $accountId, string $listId) + { + + $response = $this->adapter->delete('accounts/' . $accountId . '/rules/lists/' . $listId); $this->body = json_decode($response->getBody()); return $this->body->result; @@ -74,19 +83,24 @@ class RulesLists implements API { $options = []; foreach ($ip as $ipAddress) { - $options['ip'] = $ipAddress; + $options[] = ['ip' => $ipAddress]; } - $response = $this->adapter->post('/accounts/' . $accountId . '/rules/lists/' . $listId . '/items', $options); + $response = $this->adapter->post('accounts/' . $accountId . '/rules/lists/' . $listId . '/items', $options); $this->body = json_decode($response->getBody()); return $this->body->result; } - public function deleteListItem(string $accountId, string $listId, string $item = '') + public function deleteListItem(string $accountId, string $listId, array $itemIds) { - $response = $this->adapter->delete('/accounts/' . $accountId . '/rules/lists/' . $listId . '/items' . ($item ? '/' . $item : '')); + $options = ['items' => []]; + foreach ($itemIds as $itemId) { + $options['items'][] = ['id' => $itemId]; + } + + $response = $this->adapter->delete('accounts/' . $accountId . '/rules/lists/' . $listId . '/items', $options); $this->body = json_decode($response->getBody()); return $this->body->result; @@ -94,7 +108,7 @@ class RulesLists implements API public function getOperationStatus(string $accountId, string $operationId) { - $response = $this->adapter->get('/accounts/' . $accountId . '/rules/lists/bulk_operations/' . $operationId); + $response = $this->adapter->get('accounts/' . $accountId . '/rules/lists/bulk_operations/' . $operationId); $this->body = json_decode($response->getBody()); return $this->body->result; diff --git a/tests/Endpoints/RulesListsTest.php b/tests/Endpoints/RulesListsTest.php index 34a0884..74755d6 100755 --- a/tests/Endpoints/RulesListsTest.php +++ b/tests/Endpoints/RulesListsTest.php @@ -18,7 +18,7 @@ class RulesListsTest extends TestCase $mock->expects($this->once()) ->method('post') ->with( - $this->equalTo('/accounts/01a7362d577a6c3019a474fd6f485823/rules/lists'), + $this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823/rules/lists'), $this->equalTo([ 'kind' => 'ip', 'name' => 'ip-allowlist', @@ -35,6 +35,27 @@ class RulesListsTest extends TestCase $this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $rulesLists->getBody()->result->id); } + public function testDeleteRulesList() + { + + $response = $this->getPsr7JsonResponseForFixture('Endpoints/deleteRulesList.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('delete')->willReturn($response); + + $mock->expects($this->once()) + ->method('delete') + ->with( + $this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823/rules/lists/2c0fc9fa937b11eaa1b71c4d701ab86e') + ); + + $rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock); + $result = $rulesLists->deleteList('01a7362d577a6c3019a474fd6f485823', '2c0fc9fa937b11eaa1b71c4d701ab86e'); + + $this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $result->id); + $this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $rulesLists->getBody()->result->id); + } + public function testGetRulesLists() { $response = $this->getPsr7JsonResponseForFixture('Endpoints/listRulesLists.json'); @@ -45,15 +66,13 @@ class RulesListsTest extends TestCase $mock->expects($this->once()) ->method('get') ->with( - $this->equalTo('/accounts/01a7362d577a6c3019a474fd6f485823/rules/lists') + $this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823/rules/lists') ); $rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock); $result = $rulesLists->getLists('01a7362d577a6c3019a474fd6f485823'); - $this->assertObjectHasAttribute('result', $result); - $this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $result->result[0]->id); - + $this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $result[0]->id); $this->assertEquals('2c0fc9fa937b11eaa1b71c4d701ab86e', $rulesLists->getBody()->result[0]->id); } @@ -68,7 +87,7 @@ class RulesListsTest extends TestCase $mock->expects($this->once()) ->method('get') ->with( - $this->equalTo('/accounts/01a7362d577a6c3019a474fd6f485823/rules/lists/2c0fc9fa937b11eaa1b71c4d701ab86e') + $this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823/rules/lists/2c0fc9fa937b11eaa1b71c4d701ab86e') ); $rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock); @@ -91,7 +110,7 @@ class RulesListsTest extends TestCase $mock->expects($this->once()) ->method('get') ->with( - $this->equalTo('/accounts/01a7362d577a6c3019a474fd6f485823/rules/lists/2c0fc9fa937b11eaa1b71c4d701ab86e/items'), + $this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823/rules/lists/2c0fc9fa937b11eaa1b71c4d701ab86e/items'), $this->equalTo([ 'per_page' => 20, ]) @@ -117,7 +136,7 @@ class RulesListsTest extends TestCase $mock->expects($this->once()) ->method('post') ->with( - $this->equalTo('/accounts/01a7362d577a6c3019a474fd6f485823/rules/lists/2c0fc9fa937b11eaa1b71c4d701ab86e/items') + $this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823/rules/lists/2c0fc9fa937b11eaa1b71c4d701ab86e/items') ); $rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock); @@ -139,11 +158,11 @@ class RulesListsTest extends TestCase $mock->expects($this->once()) ->method('delete') ->with( - $this->equalTo('/accounts/01a7362d577a6c3019a474fd6f485823/rules/lists/2c0fc9fa937b11eaa1b71c4d701ab86e/items') + $this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823/rules/lists/2c0fc9fa937b11eaa1b71c4d701ab86e/items') ); $rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock); - $result = $rulesLists->deleteListItem('01a7362d577a6c3019a474fd6f485823', '2c0fc9fa937b11eaa1b71c4d701ab86e'); + $result = $rulesLists->deleteListItem('01a7362d577a6c3019a474fd6f485823', '2c0fc9fa937b11eaa1b71c4d701ab86e', ['6as9450mma215q6so7p79dd981r4ee09']); $this->assertEquals('4da8780eeb215e6cb7f48dd981c4ea02', $result->operation_id); $this->assertEquals('4da8780eeb215e6cb7f48dd981c4ea02', $rulesLists->getBody()->result->operation_id); @@ -159,7 +178,7 @@ class RulesListsTest extends TestCase $mock->expects($this->once()) ->method('get') ->with( - $this->equalTo('/accounts/01a7362d577a6c3019a474fd6f485823/rules/lists/bulk_operations/4da8780eeb215e6cb7f48dd981c4ea02') + $this->equalTo('accounts/01a7362d577a6c3019a474fd6f485823/rules/lists/bulk_operations/4da8780eeb215e6cb7f48dd981c4ea02') ); $rulesLists = new \Cloudflare\API\Endpoints\RulesLists($mock); diff --git a/tests/Fixtures/Endpoints/deleteRulesList.json b/tests/Fixtures/Endpoints/deleteRulesList.json new file mode 100755 index 0000000..cf72665 --- /dev/null +++ b/tests/Fixtures/Endpoints/deleteRulesList.json @@ -0,0 +1,18 @@ +{ + "errors": [ + { + "code": 1000, + "message": "message" + } + ], + "messages": [ + { + "code": 1000, + "message": "message" + } + ], + "result": { + "id": "2c0fc9fa937b11eaa1b71c4d701ab86e" + }, + "success": true +} \ No newline at end of file