Skip to content
Merged
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
36 changes: 25 additions & 11 deletions src/Endpoints/RulesLists.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ public function __construct(Adapter $adapter)

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;
Expand All @@ -47,10 +47,10 @@ public function getListItems(string $accountId, string $listId, string $search =
$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 = '')
Expand All @@ -64,7 +64,16 @@ public function createList(string $accountId, string $kind, string $name, string
$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;
Expand All @@ -74,27 +83,32 @@ public function createListItem(string $accountId, string $listId, array $ip)
{
$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;
}

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;
Expand Down
41 changes: 30 additions & 11 deletions tests/Endpoints/RulesListsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function testCreateRulesList()
$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',
Expand All @@ -35,6 +35,27 @@ public function testCreateRulesList()
$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');
Expand All @@ -45,15 +66,13 @@ public function testGetRulesLists()
$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);
}

Expand All @@ -68,7 +87,7 @@ public function testGetRulesListDetails()
$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);
Expand All @@ -91,7 +110,7 @@ public function testGetRulesListItems()
$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,
])
Expand All @@ -117,7 +136,7 @@ public function testCreateRulesListItem() {
$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);
Expand All @@ -139,11 +158,11 @@ public function testDeleteRulesListItem() {
$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);
Expand All @@ -159,7 +178,7 @@ public function testGetOperationStatus() {
$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);
Expand Down
18 changes: 18 additions & 0 deletions tests/Fixtures/Endpoints/deleteRulesList.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"errors": [
{
"code": 1000,
"message": "message"
}
],
"messages": [
{
"code": 1000,
"message": "message"
}
],
"result": {
"id": "2c0fc9fa937b11eaa1b71c4d701ab86e"
},
"success": true
}
Loading