diff --git a/src/Endpoints/DNS.php b/src/Endpoints/DNS.php index d06116f..65dc4a4 100644 --- a/src/Endpoints/DNS.php +++ b/src/Endpoints/DNS.php @@ -125,6 +125,15 @@ class DNS implements API return $this->body->result; } + public function getRecordID(string $zoneID, string $type = '', string $name = ''): string + { + $records = $this->listRecords($zoneID, $type, $name); + if (isset($records->result{0}->id)) { + return $records->result{0}->id; + } + return false; + } + public function updateRecordDetails(string $zoneID, string $recordID, array $details): \stdClass { $response = $this->adapter->put('zones/' . $zoneID . '/dns_records/' . $recordID, $details); diff --git a/tests/Endpoints/DNSTest.php b/tests/Endpoints/DNSTest.php index 11c21fa..27f6030 100644 --- a/tests/Endpoints/DNSTest.php +++ b/tests/Endpoints/DNSTest.php @@ -86,6 +86,25 @@ class DNSTest extends TestCase $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $dns->getBody()->result->id); } + public function testGetRecordID() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/getRecordId.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records') + ); + + $dns = new \Cloudflare\API\Endpoints\DNS($mock); + $result = $dns->getRecordID('023e105f4ecef8ad9ca31a8372d0c353', 'A', 'example.com'); + + $this->assertEquals('372e67954025e0ba6aaa6d586b9e0b59', $result); + } + public function testUpdateDNSRecord() { $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateDNSRecord.json'); diff --git a/tests/Endpoints/UARulesTest.php b/tests/Endpoints/UARulesTest.php index fec94b5..8406745 100644 --- a/tests/Endpoints/UARulesTest.php +++ b/tests/Endpoints/UARulesTest.php @@ -19,10 +19,10 @@ class UARulesTest extends TestCase ->method('get') ->with( $this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/firewall/ua_rules'), - $this->equalTo([ - 'page' => 1, - 'per_page' => 20 - ]) + $this->equalTo([ + 'page' => 1, + 'per_page' => 20 + ]) ); $zones = new \Cloudflare\API\Endpoints\UARules($mock); diff --git a/tests/Fixtures/Endpoints/getRecordId.json b/tests/Fixtures/Endpoints/getRecordId.json new file mode 100644 index 0000000..63f92c6 --- /dev/null +++ b/tests/Fixtures/Endpoints/getRecordId.json @@ -0,0 +1,29 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": [ + { + "id": "372e67954025e0ba6aaa6d586b9e0b59", + "type": "A", + "name": "example.com", + "content": "198.51.100.4", + "proxiable": true, + "proxied": false, + "ttl": {}, + "locked": false, + "zone_id": "023e105f4ecef8ad9ca31a8372d0c353", + "zone_name": "example.com", + "created_on": "2014-01-01T05:20:00.12345Z", + "modified_on": "2014-01-01T05:20:00.12345Z", + "data": {} + } + ], + "result_info": { + "page": 1, + "per_page": 20, + "total_pages": 1, + "count": 1, + "total_count": 1 + } +} \ No newline at end of file