ZonesTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: junade
  5. * Date: 06/06/2017
  6. * Time: 16:01
  7. */
  8. class ZonesTest extends TestCase
  9. {
  10. public function testAddZone()
  11. {
  12. $response = $this->getPsr7JsonResponseForFixture('Endpoints/addZone.json');
  13. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  14. $mock->method('post')->willReturn($response);
  15. $mock->expects($this->once())
  16. ->method('post')
  17. ->with(
  18. $this->equalTo('zones'),
  19. $this->equalTo(['name' => 'example.com', 'jump_start' => false])
  20. );
  21. $zones = new \Cloudflare\API\Endpoints\Zones($mock);
  22. $result = $zones->addZone('example.com');
  23. $this->assertObjectHasAttribute('id', $result);
  24. $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $result->id);
  25. $response = $this->getPsr7JsonResponseForFixture('Endpoints/createPageRule.json');
  26. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  27. $mock->method('post')->willReturn($response);
  28. $mock->expects($this->once())
  29. ->method('post')
  30. ->with(
  31. $this->equalTo('zones'),
  32. $this->equalTo([
  33. 'name' => 'example.com',
  34. 'jump_start' => true,
  35. 'account' => [
  36. 'id' => '01a7362d577a6c3019a474fd6f485823',
  37. ],
  38. ])
  39. );
  40. $zones = new \Cloudflare\API\Endpoints\Zones($mock);
  41. $zones->addZone('example.com', true, '01a7362d577a6c3019a474fd6f485823');
  42. $this->assertEquals('9a7806061c88ada191ed06f989cc3dac', $zones->getBody()->result->id);
  43. }
  44. public function testAddZoneWithAccountId()
  45. {
  46. $response = $this->getPsr7JsonResponseForFixture('Endpoints/addZone.json');
  47. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  48. $mock->method('post')->willReturn($response);
  49. $mock->expects($this->once())
  50. ->method('post')
  51. ->with(
  52. $this->equalTo('zones'),
  53. $this->equalTo([
  54. 'name' => 'example.com',
  55. 'jump_start' => false,
  56. 'account' => [
  57. 'id' => '023e105f4ecef8ad9ca31a8372d0c353',
  58. ],
  59. ])
  60. );
  61. $zones = new \Cloudflare\API\Endpoints\Zones($mock);
  62. $result = $zones->addZone('example.com', false, '023e105f4ecef8ad9ca31a8372d0c353');
  63. $this->assertObjectHasAttribute('id', $result);
  64. $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $result->account->id);
  65. }
  66. public function testActivationTest()
  67. {
  68. $response = $this->getPsr7JsonResponseForFixture('Endpoints/activationTest.json');
  69. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  70. $mock->method('put')->willReturn($response);
  71. $mock->expects($this->once())
  72. ->method('put')
  73. ->with(
  74. $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/activation_check')
  75. );
  76. $zones = new \Cloudflare\API\Endpoints\Zones($mock);
  77. $result = $zones->activationCheck('c2547eb745079dac9320b638f5e225cf483cc5cfdda41');
  78. $this->assertTrue($result);
  79. $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id);
  80. }
  81. public function testListZones()
  82. {
  83. $response = $this->getPsr7JsonResponseForFixture('Endpoints/listZones.json');
  84. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  85. $mock->method('get')->willReturn($response);
  86. $mock->expects($this->once())
  87. ->method('get')
  88. ->with(
  89. $this->equalTo('zones'),
  90. $this->equalTo([
  91. 'page' => 1,
  92. 'per_page' => 20,
  93. 'match' => 'all',
  94. 'name' => 'example.com',
  95. 'status' => 'active',
  96. 'order' => 'status',
  97. 'direction' => 'desc',
  98. ])
  99. );
  100. $zones = new \Cloudflare\API\Endpoints\Zones($mock);
  101. $result = $zones->listZones('example.com', 'active', 1, 20, 'status', 'desc');
  102. $this->assertObjectHasAttribute('result', $result);
  103. $this->assertObjectHasAttribute('result_info', $result);
  104. $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $result->result[0]->id);
  105. $this->assertEquals(1, $result->result_info->page);
  106. $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result[0]->id);
  107. }
  108. public function testGetZoneByID()
  109. {
  110. $response = $this->getPsr7JsonResponseForFixture('Endpoints/getZoneById.json');
  111. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  112. $mock->method('get')->willReturn($response);
  113. $mock->expects($this->once())
  114. ->method('get')
  115. ->with($this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353'));
  116. $zones = new \Cloudflare\API\Endpoints\Zones($mock);
  117. $result = $zones->getZoneById('023e105f4ecef8ad9ca31a8372d0c353');
  118. $this->assertInstanceOf(\stdClass::class, $result);
  119. $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result->id);
  120. $this->assertEquals('example.com', $zones->getBody()->result->name);
  121. }
  122. public function testGetZoneID()
  123. {
  124. $response = $this->getPsr7JsonResponseForFixture('Endpoints/getZoneId.json');
  125. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  126. $mock->method('get')->willReturn($response);
  127. $mock->expects($this->once())
  128. ->method('get')
  129. ->with(
  130. $this->equalTo('zones'),
  131. $this->equalTo([
  132. 'page' => 1,
  133. 'per_page' => 20,
  134. 'match' => 'all',
  135. 'name' => 'example.com',
  136. ])
  137. );
  138. $zones = new \Cloudflare\API\Endpoints\Zones($mock);
  139. $result = $zones->getZoneID('example.com');
  140. $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $result);
  141. $this->assertEquals('023e105f4ecef8ad9ca31a8372d0c353', $zones->getBody()->result[0]->id);
  142. }
  143. public function testGetAnalyticsDashboard()
  144. {
  145. $response = $this->getPsr7JsonResponseForFixture('Endpoints/getAnalyticsDashboard.json');
  146. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  147. $mock->method('get')->willReturn($response);
  148. $mock->expects($this->once())
  149. ->method('get')
  150. ->with(
  151. $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/analytics/dashboard'),
  152. $this->equalTo(['since' => '-10080', 'until' => '0', 'continuous' => var_export(true, true)])
  153. );
  154. $zones = new \Cloudflare\API\Endpoints\Zones($mock);
  155. $analytics = $zones->getAnalyticsDashboard('c2547eb745079dac9320b638f5e225cf483cc5cfdda41');
  156. $this->assertObjectHasAttribute('since', $analytics->totals);
  157. $this->assertObjectHasAttribute('since', $analytics->timeseries[0]);
  158. }
  159. public function testChangeDevelopmentMode()
  160. {
  161. $response = $this->getPsr7JsonResponseForFixture('Endpoints/changeDevelopmentMode.json');
  162. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  163. $mock->method('patch')->willReturn($response);
  164. $mock->expects($this->once())
  165. ->method('patch')
  166. ->with(
  167. $this->equalTo('zones/c2547eb745079dac9320b638f5e225cf483cc5cfdda41/settings/development_mode'),
  168. $this->equalTo(['value' => 'on'])
  169. );
  170. $zones = new \Cloudflare\API\Endpoints\Zones($mock);
  171. $result = $zones->changeDevelopmentMode('c2547eb745079dac9320b638f5e225cf483cc5cfdda41', true);
  172. $this->assertTrue($result);
  173. $this->assertEquals('development_mode', $zones->getBody()->result->id);
  174. }
  175. }