DNSTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: junade
  5. * Date: 09/06/2017
  6. * Time: 15:31
  7. */
  8. class DNSTest extends TestCase
  9. {
  10. public function testAddRecord()
  11. {
  12. $response = $this->getPsr7JsonResponseForFixture('Endpoints/addRecord.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/023e105f4ecef8ad9ca31a8372d0c353/dns_records'),
  19. $this->equalTo([
  20. 'type' => 'A',
  21. 'name' => 'example.com',
  22. 'content' => '127.0.0.1',
  23. 'ttl' => 120,
  24. 'proxied' => false
  25. ])
  26. );
  27. $dns = new \Cloudflare\API\Endpoints\DNS($mock);
  28. $dns->addRecord('023e105f4ecef8ad9ca31a8372d0c353', 'A', 'example.com', '127.0.0.1', '120', false);
  29. }
  30. public function testAddMXRecordPriority10()
  31. {
  32. $response = $this->getPsr7JsonResponseForFixture('Endpoints/addRecord.json');
  33. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  34. $mock->method('post')->willReturn($response);
  35. $mock->expects($this->once())
  36. ->method('post')
  37. ->with(
  38. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records'),
  39. $this->equalTo([
  40. 'type' => 'MX',
  41. 'name' => 'example.com',
  42. 'content' => '127.0.0.1',
  43. 'ttl' => 120,
  44. 'proxied' => false,
  45. 'priority' => 10,
  46. ])
  47. );
  48. $dns = new \Cloudflare\API\Endpoints\DNS($mock);
  49. $dns->addRecord('023e105f4ecef8ad9ca31a8372d0c353', 'MX', 'example.com', '127.0.0.1', '120', false, 10);
  50. }
  51. public function testAddMXRecordPriority0()
  52. {
  53. $response = $this->getPsr7JsonResponseForFixture('Endpoints/addRecord.json');
  54. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  55. $mock->method('post')->willReturn($response);
  56. $mock->expects($this->once())
  57. ->method('post')
  58. ->with(
  59. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records'),
  60. $this->equalTo([
  61. 'type' => 'MX',
  62. 'name' => 'example.com',
  63. 'content' => '127.0.0.1',
  64. 'ttl' => 120,
  65. 'proxied' => false,
  66. 'priority' => 0,
  67. ])
  68. );
  69. $dns = new \Cloudflare\API\Endpoints\DNS($mock);
  70. $dns->addRecord('023e105f4ecef8ad9ca31a8372d0c353', 'MX', 'example.com', '127.0.0.1', '120', false, 0);
  71. }
  72. public function testListRecords()
  73. {
  74. $response = $this->getPsr7JsonResponseForFixture('Endpoints/listRecords.json');
  75. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  76. $mock->method('get')->willReturn($response);
  77. $mock->expects($this->once())
  78. ->method('get')
  79. ->with(
  80. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records'),
  81. $this->equalTo([
  82. 'page' => 1,
  83. 'per_page' => 20,
  84. 'match' => 'all',
  85. 'type' => 'A',
  86. 'name' => 'example.com',
  87. 'content' => '127.0.0.1',
  88. 'order' => 'type',
  89. 'direction' => 'desc',
  90. ])
  91. );
  92. $zones = new \Cloudflare\API\Endpoints\DNS($mock);
  93. $result = $zones->listRecords('023e105f4ecef8ad9ca31a8372d0c353', 'A', 'example.com', '127.0.0.1', 1, 20, 'type', 'desc');
  94. $this->assertObjectHasAttribute('result', $result);
  95. $this->assertObjectHasAttribute('result_info', $result);
  96. $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $result->result[0]->id);
  97. $this->assertEquals(1, $result->result_info->page);
  98. $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $zones->getBody()->result[0]->id);
  99. }
  100. public function testGetDNSRecordDetails()
  101. {
  102. $response = $this->getPsr7JsonResponseForFixture('Endpoints/getDNSRecordDetails.json');
  103. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  104. $mock->method('get')->willReturn($response);
  105. $mock->expects($this->once())
  106. ->method('get')
  107. ->with(
  108. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records/372e67954025e0ba6aaa6d586b9e0b59')
  109. );
  110. $dns = new \Cloudflare\API\Endpoints\DNS($mock);
  111. $result = $dns->getRecordDetails('023e105f4ecef8ad9ca31a8372d0c353', '372e67954025e0ba6aaa6d586b9e0b59');
  112. $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $result->id);
  113. $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $dns->getBody()->result->id);
  114. }
  115. public function testGetRecordID()
  116. {
  117. $response = $this->getPsr7JsonResponseForFixture('Endpoints/getRecordId.json');
  118. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  119. $mock->method('get')->willReturn($response);
  120. $mock->expects($this->once())
  121. ->method('get')
  122. ->with(
  123. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records')
  124. );
  125. $dns = new \Cloudflare\API\Endpoints\DNS($mock);
  126. $result = $dns->getRecordID('023e105f4ecef8ad9ca31a8372d0c353', 'A', 'example.com');
  127. $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $result);
  128. }
  129. public function testUpdateDNSRecord()
  130. {
  131. $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateDNSRecord.json');
  132. $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
  133. $mock->method('put')->willReturn($response);
  134. $details = [
  135. 'type' => 'A',
  136. 'name' => 'example.com',
  137. 'content' => '1.2.3.4',
  138. 'ttl' => 120,
  139. 'proxied' => false,
  140. ];
  141. $mock->expects($this->once())
  142. ->method('put')
  143. ->with(
  144. $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records/372e67954025e0ba6aaa6d586b9e0b59'),
  145. $this->equalTo($details)
  146. );
  147. $dns = new \Cloudflare\API\Endpoints\DNS($mock);
  148. $result = $dns->updateRecordDetails('023e105f4ecef8ad9ca31a8372d0c353', '372e67954025e0ba6aaa6d586b9e0b59', $details);
  149. $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $result->result->id);
  150. $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $dns->getBody()->result->id);
  151. foreach ($details as $property => $value) {
  152. $this->assertEquals($result->result->{ $property }, $value);
  153. }
  154. }
  155. }