DomainsTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. <?php
  2. /**
  3. * This file is part of the DigitalOcean library.
  4. *
  5. * (c) Antoine Corcy <contact@sbin.dk>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace DigitalOcean\Tests\Domains;
  11. use DigitalOcean\Tests\TestCase;
  12. use DigitalOcean\Domains\Domains;
  13. use DigitalOcean\Domains\DomainsActions;
  14. use DigitalOcean\Domains\RecordsActions;
  15. /**
  16. * @author Antoine Corcy <contact@sbin.dk>
  17. */
  18. class DomainsTest extends TestCase
  19. {
  20. protected $domainId;
  21. protected $domainName;
  22. protected $recordId;
  23. protected $domains;
  24. protected $domainsBuildQueryMethod;
  25. protected function setUp()
  26. {
  27. $this->domainId = 123;
  28. $this->domainName = 'foo.org';
  29. $this->recordId = 456;
  30. $this->domains = new Domains($this->getMockCredentials(), $this->getMockAdapter($this->never()));
  31. $this->domainsBuildQueryMethod = new \ReflectionMethod(
  32. $this->domains, 'buildQuery'
  33. );
  34. $this->domainsBuildQueryMethod->setAccessible(true);
  35. $this->recordsBuildQueryMethod = new \ReflectionMethod(
  36. $this->domains, 'buildRecordsQuery'
  37. );
  38. $this->recordsBuildQueryMethod->setAccessible(true);
  39. }
  40. /**
  41. * @expectedException \RuntimeException
  42. * @expectedExceptionMEssage Impossible to process this query: https://api.digitalocean.com/domains/?client_id=foo&api_key=bar
  43. */
  44. public function testProcessQuery()
  45. {
  46. $sshKeys = new Domains($this->getMockCredentials(), $this->getMockAdapterReturns(null));
  47. $sshKeys->getAll();
  48. }
  49. public function testGetAllUrl()
  50. {
  51. $this->assertEquals(
  52. 'https://api.digitalocean.com/domains/?client_id=foo&api_key=bar',
  53. $this->domainsBuildQueryMethod->invoke($this->domains)
  54. );
  55. }
  56. public function testGetAllReturnsNoDomains()
  57. {
  58. $response = <<<JSON
  59. {"status":"OK","domains":[]}
  60. JSON
  61. ;
  62. $domains = new Domains($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  63. $domains = $domains->getAll();
  64. $this->assertTrue(is_object($domains));
  65. $this->assertEquals('OK', $domains->status);
  66. $this->assertEmpty($domains->domains);
  67. }
  68. public function testGetAll()
  69. {
  70. $response = <<<'JSON'
  71. {
  72. "status": "OK",
  73. "domains": [
  74. {
  75. "id": 100,
  76. "name": "example.com",
  77. "ttl": 1800,
  78. "live_zone_file": "$TTL\\t600\\n@\\t\\tIN\\tSOA\\tNS1.DIGITALOCEAN.COM.\\thostmaster.example.com. (\\n\\t\\t\\t1369261882 ; last update: 2013-05-22 22:31:22 UTC\\n\\t\\t\\t3600 ; refresh\\n\\t\\t\\t900 ; retry\\n\\t\\t\\t1209600 ; expire\\n\\t\\t\\t10800 ; 3 hours ttl\\n\\t\\t\\t)\\n IN NS NS1.DIGITALOCEAN.COM.\\n @\\tIN A\\t8.8.8.8\\n",
  79. "error": null,
  80. "zone_file_with_error": null
  81. }
  82. ]
  83. }
  84. JSON
  85. ;
  86. $domains = new Domains($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  87. $domains = $domains->getAll();
  88. $this->assertTrue(is_object($domains));
  89. $this->assertEquals('OK', $domains->status);
  90. $this->assertTrue(is_array($domains->domains));
  91. $domains = $domains->domains[0];
  92. $this->assertTrue(is_object($domains));
  93. $this->assertSame(100, $domains->id);
  94. $this->assertSame('example.com', $domains->name);
  95. $this->assertSame(1800, $domains->ttl);
  96. $this->assertSame('$TTL\\t600\\n@\\t\\tIN\\tSOA\\tNS1.DIGITALOCEAN.COM.\\thostmaster.example.com. (\\n\\t\\t\\t1369261882 ; last update: 2013-05-22 22:31:22 UTC\\n\\t\\t\\t3600 ; refresh\\n\\t\\t\\t900 ; retry\\n\\t\\t\\t1209600 ; expire\\n\\t\\t\\t10800 ; 3 hours ttl\\n\\t\\t\\t)\\n IN NS NS1.DIGITALOCEAN.COM.\\n @\\tIN A\\t8.8.8.8\\n', $domains->live_zone_file);
  97. $this->assertNull($domains->error);
  98. $this->assertNull($domains->zone_file_with_error);
  99. }
  100. public function testShowUrlWithDomainId()
  101. {
  102. $this->assertEquals(
  103. 'https://api.digitalocean.com/domains/123/?client_id=foo&api_key=bar',
  104. $this->domainsBuildQueryMethod->invoke($this->domains, $this->domainId)
  105. );
  106. }
  107. public function testShowUrlWithDomainName()
  108. {
  109. $this->assertEquals(
  110. 'https://api.digitalocean.com/domains/foo.org/?client_id=foo&api_key=bar',
  111. $this->domainsBuildQueryMethod->invoke($this->domains, $this->domainName)
  112. );
  113. }
  114. public function testShow()
  115. {
  116. $response = <<<'JSON'
  117. {
  118. "status": "OK",
  119. "domain": {
  120. "id": 100,
  121. "name": "example.com",
  122. "ttl": 1800,
  123. "live_zone_file": "$TTL\\t600\\n@\\t\\tIN\\tSOA\\tNS1.DIGITALOCEAN.COM.\\thostmaster.example.com. (\\n\\t\\t\\t1369261882 ; last update: 2013-05-22 22:31:22 UTC\\n\\t\\t\\t3600 ; refresh\\n\\t\\t\\t900 ; retry\\n\\t\\t\\t1209600 ; expire\\n\\t\\t\\t10800 ; 3 hours ttl\\n\\t\\t\\t)\\n IN NS NS1.DIGITALOCEAN.COM.\\n @\\tIN A\\t8.8.8.8\\n",
  124. "error": null,
  125. "zone_file_with_error": null
  126. }
  127. }
  128. JSON
  129. ;
  130. $domains = new Domains($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  131. $domain = $domains->show($this->domainId);
  132. $this->assertTrue(is_object($domain));
  133. $this->assertEquals('OK', $domain->status);
  134. $domain = $domain->domain;
  135. $this->assertTrue(is_object($domain));
  136. $this->assertSame(100, $domain->id);
  137. $this->assertSame('example.com', $domain->name);
  138. $this->assertSame(1800, $domain->ttl);
  139. $this->assertSame('$TTL\\t600\\n@\\t\\tIN\\tSOA\\tNS1.DIGITALOCEAN.COM.\\thostmaster.example.com. (\\n\\t\\t\\t1369261882 ; last update: 2013-05-22 22:31:22 UTC\\n\\t\\t\\t3600 ; refresh\\n\\t\\t\\t900 ; retry\\n\\t\\t\\t1209600 ; expire\\n\\t\\t\\t10800 ; 3 hours ttl\\n\\t\\t\\t)\\n IN NS NS1.DIGITALOCEAN.COM.\\n @\\tIN A\\t8.8.8.8\\n', $domain->live_zone_file);
  140. $this->assertNull($domain->error);
  141. $this->assertNull($domain->zone_file_with_error);
  142. }
  143. public function testAddUrl()
  144. {
  145. $newDomain = array(
  146. 'name' => 'bar.org',
  147. 'ip_address' => '127.0.0.1',
  148. );
  149. $this->assertEquals(
  150. 'https://api.digitalocean.com/domains/new/?name=bar.org&ip_address=127.0.0.1&client_id=foo&api_key=bar',
  151. $this->domainsBuildQueryMethod->invoke($this->domains, null, DomainsActions::ACTION_ADD, $newDomain)
  152. );
  153. }
  154. public function testAdd()
  155. {
  156. $response = <<<JSON
  157. {
  158. "status": "OK",
  159. "domain": {
  160. "id": 101,
  161. "name": "newdomain.com"
  162. }
  163. }
  164. JSON
  165. ;
  166. $domains = new Domains($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  167. $domains = $domains->add(array('name' => 'newdomain.com', 'ip_address' => '127.0.0.1'));
  168. $this->assertTrue(is_object($domains));
  169. $this->assertEquals('OK', $domains->status);
  170. $this->assertTrue(is_object($domains->domain));
  171. $this->assertSame(101, $domains->domain->id);
  172. $this->assertSame('newdomain.com', $domains->domain->name);
  173. }
  174. /**
  175. * @expectedException \InvalidArgumentException
  176. * @expectedExceptionMessage You need to provide the name of the domain.
  177. */
  178. public function testAddThrowsNameInvalidArgumentException()
  179. {
  180. $this->domains->add(array());
  181. }
  182. /**
  183. * @expectedException \InvalidArgumentException
  184. * @expectedExceptionMessage You need to provide the IP address for the domain's initial A record.
  185. */
  186. public function testAddThrowsIpAddressInvalidArgumentException()
  187. {
  188. $this->domains->add(array('name' => 'bar.org'));
  189. }
  190. public function testDestroyUrlWithDomainId()
  191. {
  192. $this->assertEquals(
  193. 'https://api.digitalocean.com/domains/123/destroy/?client_id=foo&api_key=bar',
  194. $this->domainsBuildQueryMethod->invoke($this->domains, $this->domainId, DomainsActions::ACTION_DESTROY)
  195. );
  196. }
  197. public function testDestroyUrlWithDomainName()
  198. {
  199. $this->assertEquals(
  200. 'https://api.digitalocean.com/domains/foo.org/destroy/?client_id=foo&api_key=bar',
  201. $this->domainsBuildQueryMethod->invoke($this->domains, $this->domainName, DomainsActions::ACTION_DESTROY)
  202. );
  203. }
  204. public function testDestroy()
  205. {
  206. $response = <<<JSON
  207. {"status":"OK"}
  208. JSON
  209. ;
  210. $domains = new Domains($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  211. $destroy = $domains->destroy($this->domainId);
  212. $this->assertTrue(is_object($destroy));
  213. $this->assertEquals('OK', $destroy->status);
  214. }
  215. public function testGetRecordsUrlWithDomaineId()
  216. {
  217. $this->assertEquals(
  218. 'https://api.digitalocean.com/domains/123/records/?client_id=foo&api_key=bar',
  219. $this->domainsBuildQueryMethod->invoke($this->domains, $this->domainId, DomainsActions::ACTION_RECORDS)
  220. );
  221. }
  222. public function testGetRecordsUrlWithDomaineName()
  223. {
  224. $this->assertEquals(
  225. 'https://api.digitalocean.com/domains/foo.org/records/?client_id=foo&api_key=bar',
  226. $this->domainsBuildQueryMethod->invoke($this->domains, $this->domainName, DomainsActions::ACTION_RECORDS)
  227. );
  228. }
  229. public function testGetRecords()
  230. {
  231. $response = <<<JSON
  232. {
  233. "status": "OK",
  234. "records": [
  235. {
  236. "id": 49,
  237. "domain_id": "100",
  238. "record_type": "A",
  239. "name": "example.com",
  240. "data": "8.8.8.8",
  241. "priority": null,
  242. "port": null,
  243. "weight": null
  244. },
  245. {
  246. "id": 50,
  247. "domain_id": "100",
  248. "record_type": "CNAME",
  249. "name": "www",
  250. "data": "@",
  251. "priority": null,
  252. "port": null,
  253. "weight": null
  254. }
  255. ]
  256. }
  257. JSON
  258. ;
  259. $domains = new Domains($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  260. $records = $domains->getRecords($this->domainId);
  261. $this->assertTrue(is_object($records));
  262. $this->assertEquals('OK', $records->status);
  263. $this->assertTrue(is_array($records->records));
  264. $record1 = $records->records[0];
  265. $this->assertTrue(is_object($record1));
  266. $this->assertSame(49, $record1->id);
  267. $this->assertSame('100', $record1->domain_id);
  268. $this->assertSame('A', $record1->record_type);
  269. $this->assertSame('example.com', $record1->name);
  270. $this->assertSame('8.8.8.8', $record1->data);
  271. $this->assertNull($record1->priority);
  272. $this->assertNull($record1->port);
  273. $this->assertNull($record1->weight);
  274. $record2 = $records->records[1];
  275. $this->assertSame(50, $record2->id);
  276. $this->assertSame('100', $record2->domain_id);
  277. $this->assertSame('CNAME', $record2->record_type);
  278. $this->assertSame('www', $record2->name);
  279. $this->assertSame('@', $record2->data);
  280. $this->assertNull($record2->priority);
  281. $this->assertNull($record2->port);
  282. $this->assertNull($record2->weight);
  283. }
  284. public function testNewRecordUrlWithDomaineId()
  285. {
  286. $this->assertEquals(
  287. 'https://api.digitalocean.com/domains/123/records/new/?client_id=foo&api_key=bar',
  288. $this->recordsBuildQueryMethod->invoke($this->domains, $this->domainId, RecordsActions::ACTION_ADD)
  289. );
  290. }
  291. public function testNewRecordUrlWithDomaineName()
  292. {
  293. $this->assertEquals(
  294. 'https://api.digitalocean.com/domains/foo.org/records/new/?client_id=foo&api_key=bar',
  295. $this->recordsBuildQueryMethod->invoke($this->domains, $this->domainName, RecordsActions::ACTION_ADD)
  296. );
  297. }
  298. /**
  299. * @expectedException \InvalidArgumentException
  300. * @expectedExceptionMessage You need to provide the record_type.
  301. */
  302. public function testNewRecordWithEmptyArray()
  303. {
  304. $this->domains->newRecord($this->domainId, array());
  305. }
  306. /**
  307. * @expectedException \InvalidArgumentException
  308. * @expectedExceptionMessage The record_type can only be A, CNAME, NS, TXT, MX or SRV
  309. */
  310. public function testNewRecordWithWrongRecordTypeValueType()
  311. {
  312. $this->domains->newRecord($this->domainId, array(
  313. 'record_type' => null,
  314. ));
  315. }
  316. /**
  317. * @expectedException \InvalidArgumentException
  318. * @expectedExceptionMessage You need to provide the data value of the record.
  319. */
  320. public function testNewRecordWithCorrectRecordTypeValue()
  321. {
  322. $this->domains->newRecord($this->domainId, array(
  323. 'record_type' => 'A',
  324. ));
  325. }
  326. /**
  327. * @expectedException \InvalidArgumentException
  328. * @expectedExceptionMessage You need to provide the data value of the record.
  329. */
  330. public function testNewRecordWithWrongDataAndWrongDataType()
  331. {
  332. $this->domains->newRecord($this->domainId, array(
  333. 'record_type' => 'A',
  334. 'data' => null
  335. ));
  336. }
  337. /**
  338. * @expectedException \InvalidArgumentException
  339. * @expectedExceptionMessage You need to provide the name string if the record_type is A, CNAME, TXT or SRV.
  340. */
  341. public function testNewRecordTypeAWithoutNameString()
  342. {
  343. $this->domains->newRecord($this->domainId, array(
  344. 'record_type' => 'A',
  345. 'data' => 'data',
  346. ));
  347. }
  348. /**
  349. * @expectedException \InvalidArgumentException
  350. * @expectedExceptionMessage You need to provide the priority integer if the record_type is SRV or MX.
  351. */
  352. public function testNewRecordTypeSRVWithoutPriorityNumber()
  353. {
  354. $this->domains->newRecord($this->domainId, array(
  355. 'record_type' => 'SRV',
  356. 'data' => 'data',
  357. 'name' => 'foo',
  358. ));
  359. }
  360. /**
  361. * @expectedException \InvalidArgumentException
  362. * @expectedExceptionMessage You need to provide the port integer if the record_type is SRV.
  363. */
  364. public function testNewRecordTypeSRVWithoutPortNumber()
  365. {
  366. $this->domains->newRecord($this->domainId, array(
  367. 'record_type' => 'SRV',
  368. 'data' => 'data',
  369. 'name' => 'foo',
  370. 'priority' => 1,
  371. ));
  372. }
  373. /**
  374. * @expectedException \InvalidArgumentException
  375. * @expectedExceptionMessage You need to provide the weight integer if the record_type is SRV.
  376. */
  377. public function testNewRecordTypeSRVWithoutWeightNumber()
  378. {
  379. $this->domains->newRecord($this->domainId, array(
  380. 'record_type' => 'SRV',
  381. 'data' => 'data',
  382. 'name' => 'foo',
  383. 'priority' => 1,
  384. 'port' => 2,
  385. ));
  386. }
  387. public function testNewRecordTypeA()
  388. {
  389. $response = <<<JSON
  390. {"status":"OK","domain_record":{"id": 51,"domain_id":"100","record_type":"A","name":"foo","data":"data","priority":null,"port":null,"weight":null}}
  391. JSON
  392. ;
  393. $parameters = array(
  394. 'record_type' => 'A',
  395. 'data' => 'data',
  396. 'name' => 'foo',
  397. );
  398. $domains = new Domains($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  399. $newRecord = $domains->newRecord($this->domainId, $parameters);
  400. $this->assertTrue(is_object($newRecord));
  401. $this->assertEquals('OK', $newRecord->status);
  402. $record = $newRecord->domain_record;
  403. $this->assertTrue(is_object($record));
  404. $this->assertSame(51, $record->id);
  405. $this->assertSame('100', $record->domain_id);
  406. $this->assertSame('A', $record->record_type);
  407. $this->assertSame('foo', $record->name);
  408. $this->assertSame('data', $record->data);
  409. $this->assertNull($record->priority);
  410. $this->assertNull($record->port);
  411. $this->assertNull($record->weight);
  412. }
  413. public function testNewRecordTypeCNAME()
  414. {
  415. $response = <<<JSON
  416. {"status":"OK","domain_record":{"id": 52,"domain_id":"100","record_type":"CNAME","name":"foo","data":"data","priority":null,"port":null,"weight":null}}
  417. JSON
  418. ;
  419. $parameters = array(
  420. 'record_type' => 'CNAME',
  421. 'data' => 'data',
  422. 'name' => 'foo',
  423. );
  424. $domains = new Domains($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  425. $newRecord = $domains->newRecord($this->domainId, $parameters);
  426. $this->assertTrue(is_object($newRecord));
  427. $this->assertEquals('OK', $newRecord->status);
  428. $record = $newRecord->domain_record;
  429. $this->assertTrue(is_object($record));
  430. $this->assertSame(52, $record->id);
  431. $this->assertSame('100', $record->domain_id);
  432. $this->assertSame('CNAME', $record->record_type);
  433. $this->assertSame('foo', $record->name);
  434. $this->assertSame('data', $record->data);
  435. $this->assertNull($record->priority);
  436. $this->assertNull($record->port);
  437. $this->assertNull($record->weight);
  438. }
  439. public function testNewRecordTypeNS()
  440. {
  441. $response = <<<JSON
  442. {"status":"OK","domain_record":{"id": 53,"domain_id":"100","record_type":"NS","name":"bar","data":"data","priority":null,"port":null,"weight":null}}
  443. JSON
  444. ;
  445. $parameters = array(
  446. 'record_type' => 'NS',
  447. 'data' => 'data',
  448. );
  449. $domains = new Domains($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  450. $newRecord = $domains->newRecord($this->domainId, $parameters);
  451. $this->assertTrue(is_object($newRecord));
  452. $this->assertEquals('OK', $newRecord->status);
  453. $record = $newRecord->domain_record;
  454. $this->assertTrue(is_object($record));
  455. $this->assertSame(53, $record->id);
  456. $this->assertSame('100', $record->domain_id);
  457. $this->assertSame('NS', $record->record_type);
  458. $this->assertSame('bar', $record->name);
  459. $this->assertSame('data', $record->data);
  460. $this->assertNull($record->priority);
  461. $this->assertNull($record->port);
  462. $this->assertNull($record->weight);
  463. }
  464. public function testNewRecordTypeTXT()
  465. {
  466. $response = <<<JSON
  467. {"status":"OK","domain_record":{"id": 54,"domain_id":"100","record_type":"TXT","name":"foo","data":"data","priority":null,"port":null,"weight":null}}
  468. JSON
  469. ;
  470. $parameters = array(
  471. 'record_type' => 'TXT',
  472. 'data' => 'data',
  473. 'name' => 'foo'
  474. );
  475. $domains = new Domains($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  476. $newRecord = $domains->newRecord($this->domainId, $parameters);
  477. $this->assertTrue(is_object($newRecord));
  478. $this->assertEquals('OK', $newRecord->status);
  479. $record = $newRecord->domain_record;
  480. $this->assertTrue(is_object($record));
  481. $this->assertSame(54, $record->id);
  482. $this->assertSame('100', $record->domain_id);
  483. $this->assertSame('TXT', $record->record_type);
  484. $this->assertSame('foo', $record->name);
  485. $this->assertSame('data', $record->data);
  486. $this->assertNull($record->priority);
  487. $this->assertNull($record->port);
  488. $this->assertNull($record->weight);
  489. }
  490. public function testNewRecordTypeMX()
  491. {
  492. $response = <<<JSON
  493. {"status":"OK","domain_record":{"id": 55,"domain_id":"100","record_type":"MX","name":"baz","data":"data","priority":1,"port":null,"weight":null}}
  494. JSON
  495. ;
  496. $parameters = array(
  497. 'record_type' => 'MX',
  498. 'data' => 'data',
  499. 'priority' => 1,
  500. );
  501. $domains = new Domains($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  502. $newRecord = $domains->newRecord($this->domainId, $parameters);
  503. $this->assertTrue(is_object($newRecord));
  504. $this->assertEquals('OK', $newRecord->status);
  505. $record = $newRecord->domain_record;
  506. $this->assertTrue(is_object($record));
  507. $this->assertSame(55, $record->id);
  508. $this->assertSame('100', $record->domain_id);
  509. $this->assertSame('MX', $record->record_type);
  510. $this->assertSame('baz', $record->name);
  511. $this->assertSame('data', $record->data);
  512. $this->assertSame(1, $record->priority);
  513. $this->assertNull($record->port);
  514. $this->assertNull($record->weight);
  515. }
  516. public function testNewRecordTypeSRV()
  517. {
  518. $response = <<<JSON
  519. {"status":"OK","domain_record":{"id": 56,"domain_id":"100","record_type":"SRV","name":"foo","data":"data","priority":1,"port":88,"weight":2}}
  520. JSON
  521. ;
  522. $parameters = array(
  523. 'record_type' => 'SRV',
  524. 'data' => 'data',
  525. 'name' => 'foo',
  526. 'priority' => 1,
  527. 'port' => 88,
  528. 'weight' => 2,
  529. );
  530. $domains = new Domains($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  531. $newRecord = $domains->newRecord($this->domainId, $parameters);
  532. $this->assertTrue(is_object($newRecord));
  533. $this->assertEquals('OK', $newRecord->status);
  534. $record = $newRecord->domain_record;
  535. $this->assertTrue(is_object($record));
  536. $this->assertSame(56, $record->id);
  537. $this->assertSame('100', $record->domain_id);
  538. $this->assertSame('SRV', $record->record_type);
  539. $this->assertSame('foo', $record->name);
  540. $this->assertSame('data', $record->data);
  541. $this->assertSame(1, $record->priority);
  542. $this->assertSame(88, $record->port);
  543. $this->assertSame(2, $record->weight);
  544. }
  545. public function testGetRecordUrl()
  546. {
  547. $this->assertEquals(
  548. 'https://api.digitalocean.com/domains/123/records/456/?client_id=foo&api_key=bar',
  549. $this->recordsBuildQueryMethod->invoke($this->domains, $this->domainId, $this->recordId)
  550. );
  551. }
  552. public function testGetRecord()
  553. {
  554. $response = <<<JSON
  555. {"status":"OK","record":{"id":456,"domain_id":"123","record_type":"CNAME","name":"www","data":"@","priority":null,"port":null,"weight":null}}
  556. JSON
  557. ;
  558. $domains = new Domains($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  559. $record = $domains->getRecord($this->domainId, $this->recordId);
  560. $this->assertTrue(is_object($record));
  561. $this->assertEquals('OK', $record->status);
  562. $record = $record->record;
  563. $this->assertTrue(is_object($record));
  564. $this->assertSame(456, $record->id);
  565. $this->assertSame('123', $record->domain_id);
  566. $this->assertSame('CNAME', $record->record_type);
  567. $this->assertSame('www', $record->name);
  568. $this->assertSame('@', $record->data);
  569. $this->assertNull($record->priority);
  570. $this->assertNull($record->port);
  571. $this->assertNull($record->weight);
  572. }
  573. public function testEditRecordUrl()
  574. {
  575. $this->assertEquals(
  576. 'https://api.digitalocean.com/domains/123/records/456/edit/?client_id=foo&api_key=bar',
  577. $this->recordsBuildQueryMethod->invoke($this->domains, $this->domainId, $this->recordId, RecordsActions::ACTION_EDIT)
  578. );
  579. }
  580. public function testEditRecord()
  581. {
  582. $response = <<<JSON
  583. {"status":"OK","record":{"id":50,"domain_id":"100","record_type":"CNAME","name":"www2","data":"@","priority":null,"port":null,"weight":null}}
  584. JSON
  585. ;
  586. $parameters = array(
  587. 'record_type' => 'CNAME',
  588. 'data' => '@',
  589. 'name' => 'www2'
  590. );
  591. $domains = new Domains($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  592. $editedRecord = $domains->editRecord($this->domainId, $this->recordId, $parameters);
  593. $this->assertTrue(is_object($editedRecord));
  594. $this->assertEquals('OK', $editedRecord->status);
  595. $record = $editedRecord->record;
  596. $this->assertTrue(is_object($record));
  597. $this->assertSame(50, $record->id);
  598. $this->assertSame('100', $record->domain_id);
  599. $this->assertSame('CNAME', $record->record_type);
  600. $this->assertSame('www2', $record->name);
  601. $this->assertSame('@', $record->data);
  602. $this->assertNull($record->priority);
  603. $this->assertNull($record->port);
  604. $this->assertNull($record->weight);
  605. }
  606. // testEditRecord* are close to testNewRecord* because we use the protected method
  607. // Domains::checkParameters in both Domains::newRecord and Domains::editRecord.
  608. public function testDestroyRecordUrl()
  609. {
  610. $this->assertEquals(
  611. 'https://api.digitalocean.com/domains/123/records/456/destroy/?client_id=foo&api_key=bar',
  612. $this->recordsBuildQueryMethod->invoke($this->domains, $this->domainId, $this->recordId, RecordsActions::ACTION_DESTROY)
  613. );
  614. }
  615. public function testDestroyRecord()
  616. {
  617. $response = <<<JSON
  618. {"status":"OK"}
  619. JSON
  620. ;
  621. $domains = new Domains($this->getMockCredentials(), $this->getMockAdapterReturns($response));
  622. $destroy = $domains->destroyRecord($this->domainId, $this->recordId);
  623. $this->assertTrue(is_object($destroy));
  624. $this->assertEquals('OK', $destroy->status);
  625. }
  626. }