RailgunTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: junade
  5. * Date: 23/10/2017
  6. * Time: 11:20
  7. */
  8. class RailgunTest extends TestCase
  9. {
  10. public function testcreate()
  11. {
  12. $details = [
  13. 'name' => 'My Railgun',
  14. ];
  15. $response = $this->getPsr7JsonResponseForFixture('Endpoints/createRailgun.json');
  16. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  17. $mock->method('post')->willReturn($response);
  18. $mock->expects($this->once())
  19. ->method('post')
  20. ->with(
  21. $this->equalTo('railguns'),
  22. $this->equalTo(['name' => $details['name']])
  23. );
  24. $railgun = new \Cloudflare\API\Endpoints\Railgun($mock);
  25. $result = $railgun->create($details['name']);
  26. $this->assertObjectHasAttribute('result', $result);
  27. foreach ($details as $property => $value) {
  28. $this->assertEquals($result->result->{ $property }, $value);
  29. }
  30. $this->assertEquals('e928d310693a83094309acf9ead50448', $railgun->getBody()->result->id);
  31. }
  32. public function testlist()
  33. {
  34. $response = $this->getPsr7JsonResponseForFixture('Endpoints/listRailguns.json');
  35. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  36. $mock->method('get')->willReturn($response);
  37. $mock->expects($this->once())
  38. ->method('get')
  39. ->with(
  40. $this->equalTo('railguns'),
  41. $this->equalTo([
  42. 'page' => 1,
  43. 'per_page' => 20,
  44. 'direction' => 'desc'
  45. ])
  46. );
  47. $railgun = new \Cloudflare\API\Endpoints\Railgun($mock);
  48. $result = $railgun->list(1, 20, 'desc');
  49. $this->assertObjectHasAttribute('result', $result);
  50. $this->assertObjectHasAttribute('result_info', $result);
  51. $this->assertEquals('e928d310693a83094309acf9ead50448', $railgun->getBody()->result[0]->id);
  52. }
  53. public function testget()
  54. {
  55. $response = $this->getPsr7JsonResponseForFixture('Endpoints/getRailgun.json');
  56. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  57. $mock->method('get')->willReturn($response);
  58. $mock->expects($this->once())
  59. ->method('get')
  60. ->with(
  61. $this->equalTo('railguns/e928d310693a83094309acf9ead50448')
  62. );
  63. $railgun = new \Cloudflare\API\Endpoints\Railgun($mock);
  64. $result = $railgun->get('e928d310693a83094309acf9ead50448');
  65. $this->assertEquals('e928d310693a83094309acf9ead50448', $result->id);
  66. $this->assertEquals('e928d310693a83094309acf9ead50448', $railgun->getBody()->result->id);
  67. }
  68. public function testgetZones()
  69. {
  70. $response = $this->getPsr7JsonResponseForFixture('Endpoints/listRailgunZones.json');
  71. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  72. $mock->method('get')->willReturn($response);
  73. $mock->expects($this->once())
  74. ->method('get')
  75. ->with(
  76. $this->equalTo('railguns/e928d310693a83094309acf9ead50448/zones')
  77. );
  78. $railgun = new \Cloudflare\API\Endpoints\Railgun($mock);
  79. $result = $railgun->getZones('e928d310693a83094309acf9ead50448');
  80. $this->assertObjectHasAttribute('result', $result);
  81. $this->assertObjectHasAttribute('result_info', $result);
  82. $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $railgun->getBody()->result[0]->id);
  83. }
  84. public function testupdate()
  85. {
  86. $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateRailgun.json');
  87. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  88. $mock->method('patch')->willReturn($response);
  89. $details = [
  90. 'enabled' => true,
  91. ];
  92. $mock->expects($this->once())
  93. ->method('patch')
  94. ->with(
  95. $this->equalTo('railguns/e928d310693a83094309acf9ead50448'),
  96. $this->equalTo($details)
  97. );
  98. $waf = new \Cloudflare\API\Endpoints\Railgun($mock);
  99. $result = $waf->update('e928d310693a83094309acf9ead50448', true);
  100. $this->assertEquals('e928d310693a83094309acf9ead50448', $result->id);
  101. $this->assertEquals('e928d310693a83094309acf9ead50448', $waf->getBody()->result->id);
  102. }
  103. public function testdelete()
  104. {
  105. $response = $this->getPsr7JsonResponseForFixture('Endpoints/deleteRailgun.json');
  106. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  107. $mock->method('delete')->willReturn($response);
  108. $mock->expects($this->once())
  109. ->method('delete')
  110. ->with(
  111. $this->equalTo('railguns/e928d310693a83094309acf9ead50448')
  112. );
  113. $waf = new \Cloudflare\API\Endpoints\Railgun($mock);
  114. $waf->delete('e928d310693a83094309acf9ead50448');
  115. $this->assertEquals('e928d310693a83094309acf9ead50448', $waf->getBody()->result->id);
  116. }
  117. }