From 3b028041a6b9d832d97399ef274b1510d107cf0b Mon Sep 17 00:00:00 2001 From: Junade Ali Date: Tue, 22 Aug 2017 16:28:05 +0100 Subject: [PATCH] Additional DNS API coverage, PHPUnit whitelist and license added. --- LICENSE | 26 +++++++ README.md | 8 ++- phpunit.xml | 7 +- src/Endpoints/DNS.php | 72 +++++++++++++++++++ tests/Endpoints/DNSTest.php | 134 ++++++++++++++++++++++++++++++++++++ 5 files changed, 244 insertions(+), 3 deletions(-) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..534ca686 --- /dev/null +++ b/LICENSE @@ -0,0 +1,26 @@ +Copyright (c) 2015-2016, Cloudflare. All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this +list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, +this list of conditions and the following disclaimer in the documentation and/or +other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors +may be used to endorse or promote products derived from this software without +specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON +ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/README.md b/README.md index 3cb210a0..d250f2f2 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Cloudflare v4 API Binding for PHP 7 +# Cloudflare SDK (v4 API Binding for PHP 7) ## Installation @@ -23,6 +23,8 @@ Each API call is provided via a similarly named function within various classes - [ ] [Keyless SSL](https://blog.cloudflare.com/keyless-ssl-the-nitty-gritty-technical-details/) - [ ] [Origin CA](https://blog.cloudflare.com/universal-ssl-encryption-all-the-way-to-the-origin-for-free/) +Note that this repository is currently under development, additional classes and endpoints being actively added. + ## Getting Started ```php @@ -33,4 +35,6 @@ $user = new \Cloudflare\API\Endpoints\User($adapter); echo $user->getUserID(); ``` -## Licensing \ No newline at end of file +## Licensing + +Licensed under the 3-clause BSD license. See the [LICENSE](LICENSE) file for details. \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml index 12141818..65002f75 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -5,4 +5,9 @@ ./tests/ - \ No newline at end of file + + + ./src/ + + + diff --git a/src/Endpoints/DNS.php b/src/Endpoints/DNS.php index 3b8d2e92..7b3cb6a3 100644 --- a/src/Endpoints/DNS.php +++ b/src/Endpoints/DNS.php @@ -43,4 +43,76 @@ public function addRecord(string $zoneID, string $type, string $name, string $co return false; } + public function listRecords( + string $zoneID, + string $type = "", + string $content = "", + int $page = 1, + int $perPage = 20, + string $order = "", + string $direction = "", + string $match = "all" + ): \stdClass { + $options = [ + 'page' => $page, + 'per_page' => $perPage, + 'match' => $match + ]; + + if (!empty($type)) { + $options['type'] = $type; + } + + if (!empty($name)) { + $options['name'] = $name; + } + + if (!empty($content)) { + $options['content'] = $content; + } + + if (!empty($order)) { + $options['order'] = $order; + } + + if (!empty($direction)) { + $options['direction'] = $direction; + } + + $user = $this->adapter->get('zones/'.$zoneID.'/dns_records', [], $options); + $body = json_decode($user->getBody()); + + $result = new \stdClass(); + $result->result = $body->result; + $result->result_info = $body->result_info; + + return $result; + } + + public function getRecordDetails(string $zoneID, string $recordID): \stdClass + { + $user = $this->adapter->get('zones/'.$zoneID.'/dns_records/'.$recordID, []); + $body = json_decode($user->getBody()); + return $body->result; + } + + public function updateRecordDetails(string $zoneID, string $recordID, array $details): \stdClass + { + $response = $this->adapter->put('zones/'.$zoneID.'/dns_records/'.$recordID, [], $details); + return json_decode($response->getBody()); + } + + public function deleteRecord(string $zoneID, string $recordID): bool + { + $user = $this->adapter->delete('zones/'.$zoneID.'/dns_records/'.$recordID, [], []); + + $body = json_decode($user->getBody()); + + if (isset($body->result->id)) { + return true; + } + + return false; + } + } \ No newline at end of file diff --git a/tests/Endpoints/DNSTest.php b/tests/Endpoints/DNSTest.php index 121d2320..697ae3be 100644 --- a/tests/Endpoints/DNSTest.php +++ b/tests/Endpoints/DNSTest.php @@ -10,5 +10,139 @@ class DNSTest extends PHPUnit_Framework_TestCase { + public function testAddRecord() + { + $stream = GuzzleHttp\Psr7\stream_for(' +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "372e67954025e0ba6aaa6d586b9e0b59", + "type": "A", + "name": "example.com", + "content": "1.2.3.4", + "proxiable": true, + "proxied": false, + "ttl": 120, + "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": {} + } +}'); + $response = new GuzzleHttp\Psr7\Response(200, ['Content-Type' => 'application/json'], $stream); + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('post')->willReturn($response); + + $mock->expects($this->once()) + ->method('post') + ->with($this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records'), $this->equalTo([]), + $this->equalTo(['type' => 'A', 'name' => 'example.com', 'content' => '127.0.0.1', 'ttl' => 120, 'proxied' => false]) + ); + + $dns = new \Cloudflare\API\Endpoints\DNS($mock); + $dns->addRecord('023e105f4ecef8ad9ca31a8372d0c353', 'A', 'example.com', '127.0.0.1', '120', false); + } + + public function testListRecords() + { + $stream = GuzzleHttp\Psr7\stream_for('{ + "success": true, + "errors": [], + "messages": [], + "result": [ + { + "id": "372e67954025e0ba6aaa6d586b9e0b59", + "type": "A", + "name": "example.com", + "content": "1.2.3.4", + "proxiable": true, + "proxied": false, + "ttl": 120, + "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, + "count": 1, + "total_count": 2000 + } +}'); + + $response = new GuzzleHttp\Psr7\Response(200, ['Content-Type' => 'application/json'], $stream); + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with($this->equalTo('zones'), + $this->equalTo([]), + $this->equalTo([ + 'page' => 1, + 'per_page' => 20, + 'match' => 'all', + 'name' => 'example.com', + 'status' => 'active', + 'order' => 'status', + 'direction' => 'desc' + ] + )); + + $zones = new \Cloudflare\API\Endpoints\Zones($mock); + $result = $zones->listZones("example.com", "active", 1, 20, "status", "desc", "all"); + + $this->assertObjectHasAttribute('result', $result); + $this->assertObjectHasAttribute('result_info', $result); + + $this->assertEquals("372e67954025e0ba6aaa6d586b9e0b59", $result->result[0]->id); + $this->assertEquals(1, $result->result_info->page); + } + + public function testGetRecordDetails() + { + $stream = GuzzleHttp\Psr7\stream_for('{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "372e67954025e0ba6aaa6d586b9e0b59", + "type": "A", + "name": "example.com", + "content": "1.2.3.4", + "proxiable": true, + "proxied": false, + "ttl": 120, + "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": {} + } +}'); + + $response = new GuzzleHttp\Psr7\Response(200, ['Content-Type' => 'application/json'], $stream); + $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/372e67954025e0ba6aaa6d586b9e0b59'), + $this->equalTo([]) + ); + + $dns = new \Cloudflare\API\Endpoints\DNS($mock); + $result = $dns->getRecordDetails("023e105f4ecef8ad9ca31a8372d0c353", "372e67954025e0ba6aaa6d586b9e0b59"); + $this->assertEquals("372e67954025e0ba6aaa6d586b9e0b59", $result->id); + } }