AccessRulesTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. class AccessRulesTest extends TestCase
  3. {
  4. public function testListRules()
  5. {
  6. $response = $this->getPsr7JsonResponseForFixture('Endpoints/listAccessRules.json');
  7. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  8. $mock->method('get')->willReturn($response);
  9. $mock->expects($this->once())
  10. ->method('get')
  11. ->with(
  12. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/access_rules/rules'),
  13. $this->equalTo([
  14. 'page' => 1,
  15. 'per_page' => 50,
  16. 'match' => 'all'
  17. ])
  18. );
  19. $zones = new \Cloudflare\API\Endpoints\AccessRules($mock);
  20. $result = $zones->listRules('023e105f4ecef8ad9ca31a8372d0c353');
  21. $this->assertObjectHasAttribute('result', $result);
  22. $this->assertObjectHasAttribute('result_info', $result);
  23. $this->assertEquals('92f17202ed8bd63d69a66b86a49a8f6b', $result->result[0]->id);
  24. $this->assertEquals(1, $result->result_info->page);
  25. $this->assertEquals('92f17202ed8bd63d69a66b86a49a8f6b', $zones->getBody()->result[0]->id);
  26. }
  27. public function testCreateRule()
  28. {
  29. $config = new \Cloudflare\API\Configurations\AccessRules();
  30. $config->setIP('1.2.3.4');
  31. $response = $this->getPsr7JsonResponseForFixture('Endpoints/createAccessRule.json');
  32. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  33. $mock->method('post')->willReturn($response);
  34. $mock->expects($this->once())
  35. ->method('post')
  36. ->with(
  37. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/access_rules/rules'),
  38. $this->equalTo([
  39. 'mode' => 'challenge',
  40. 'configuration' => $config->getArray(),
  41. 'notes' => 'This rule is on because of an event that occured on date X',
  42. ])
  43. );
  44. $rules = new \Cloudflare\API\Endpoints\AccessRules($mock);
  45. $rules->createRule(
  46. '023e105f4ecef8ad9ca31a8372d0c353',
  47. 'challenge',
  48. $config,
  49. 'This rule is on because of an event that occured on date X'
  50. );
  51. $this->assertEquals('92f17202ed8bd63d69a66b86a49a8f6b', $rules->getBody()->result->id);
  52. }
  53. public function testUpdateRule()
  54. {
  55. $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateAccessRule.json');
  56. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  57. $mock->method('patch')->willReturn($response);
  58. $mock->expects($this->once())
  59. ->method('patch')
  60. ->with(
  61. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/access_rules/rules/92f17202ed8bd63d69a66b86a49a8f6b'),
  62. $this->equalTo([
  63. 'mode' => 'challenge',
  64. 'notes' => 'This rule is on because of an event that occured on date X',
  65. ])
  66. );
  67. $rules = new \Cloudflare\API\Endpoints\AccessRules($mock);
  68. $rules->updateRule(
  69. '023e105f4ecef8ad9ca31a8372d0c353',
  70. '92f17202ed8bd63d69a66b86a49a8f6b',
  71. 'challenge',
  72. 'This rule is on because of an event that occured on date X'
  73. );
  74. $this->assertEquals('92f17202ed8bd63d69a66b86a49a8f6b', $rules->getBody()->result->id);
  75. }
  76. public function testDeleteRule()
  77. {
  78. $response = $this->getPsr7JsonResponseForFixture('Endpoints/deleteAccessRule.json');
  79. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  80. $mock->method('delete')->willReturn($response);
  81. $mock->expects($this->once())
  82. ->method('delete')
  83. ->with(
  84. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/access_rules/rules/92f17202ed8bd63d69a66b86a49a8f6b'),
  85. $this->equalTo([
  86. 'cascade' => 'none'
  87. ])
  88. );
  89. $rules = new \Cloudflare\API\Endpoints\AccessRules($mock);
  90. $rules->deleteRule('023e105f4ecef8ad9ca31a8372d0c353', '92f17202ed8bd63d69a66b86a49a8f6b');
  91. $this->assertEquals('92f17202ed8bd63d69a66b86a49a8f6b', $rules->getBody()->result->id);
  92. }
  93. }