COM-40 :: General refactoring/fixes.

This commit is contained in:
Junade Ali
2017-09-04 21:14:34 +01:00
parent 53cbd9d578
commit 7d0c0c86dc
6 changed files with 44 additions and 52 deletions

View File

@@ -19,8 +19,12 @@ class Guzzle implements Adapter
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function __construct(Auth $auth, String $baseURI = "https://api.cloudflare.com/client/v4/") public function __construct(Auth $auth, String $baseURI = null)
{ {
if ($baseURI === null) {
$baseURI = "https://api.cloudflare.com/client/v4/";
}
$headers = $auth->getHeaders(); $headers = $auth->getHeaders();
$this->client = new Client([ $this->client = new Client([

View File

@@ -19,8 +19,14 @@ class DNS implements API
$this->adapter = $adapter; $this->adapter = $adapter;
} }
public function addRecord(string $zoneID, string $type, string $name, string $content, int $ttl = 0, bool $proxied = true): bool public function addRecord(
{ string $zoneID,
string $type,
string $name,
string $content,
int $ttl = 0,
bool $proxied = true
): bool {
$options = [ $options = [
'type' => $type, 'type' => $type,
'name' => $name, 'name' => $name,
@@ -46,6 +52,7 @@ class DNS implements API
public function listRecords( public function listRecords(
string $zoneID, string $zoneID,
string $type = "", string $type = "",
string $name = "",
string $content = "", string $content = "",
int $page = 1, int $page = 1,
int $perPage = 20, int $perPage = 20,
@@ -79,7 +86,9 @@ class DNS implements API
$options['direction'] = $direction; $options['direction'] = $direction;
} }
$user = $this->adapter->get('zones/'.$zoneID.'/dns_records', [], $options); $query = http_build_query($options);
$user = $this->adapter->get('zones/' . $zoneID . '/dns_records?' . $query, []);
$body = json_decode($user->getBody()); $body = json_decode($user->getBody());
$result = new \stdClass(); $result = new \stdClass();
@@ -91,20 +100,20 @@ class DNS implements API
public function getRecordDetails(string $zoneID, string $recordID): \stdClass public function getRecordDetails(string $zoneID, string $recordID): \stdClass
{ {
$user = $this->adapter->get('zones/'.$zoneID.'/dns_records/'.$recordID, []); $user = $this->adapter->get('zones/' . $zoneID . '/dns_records/' . $recordID, []);
$body = json_decode($user->getBody()); $body = json_decode($user->getBody());
return $body->result; return $body->result;
} }
public function updateRecordDetails(string $zoneID, string $recordID, array $details): \stdClass public function updateRecordDetails(string $zoneID, string $recordID, array $details): \stdClass
{ {
$response = $this->adapter->put('zones/'.$zoneID.'/dns_records/'.$recordID, [], $details); $response = $this->adapter->put('zones/' . $zoneID . '/dns_records/' . $recordID, [], $details);
return json_decode($response->getBody()); return json_decode($response->getBody());
} }
public function deleteRecord(string $zoneID, string $recordID): bool public function deleteRecord(string $zoneID, string $recordID): bool
{ {
$user = $this->adapter->delete('zones/'.$zoneID.'/dns_records/'.$recordID, [], []); $user = $this->adapter->delete('zones/' . $zoneID . '/dns_records/' . $recordID, [], []);
$body = json_decode($user->getBody()); $body = json_decode($user->getBody());

View File

@@ -26,5 +26,4 @@ class IPs implements API
return $body->result; return $body->result;
} }
} }

View File

@@ -80,7 +80,9 @@ class Zones implements API
$options['direction'] = $direction; $options['direction'] = $direction;
} }
$user = $this->adapter->get('zones', [], $options); $query = http_build_query($options);
$user = $this->adapter->get('zones?' . $query, []);
$body = json_decode($user->getBody()); $body = json_decode($user->getBody());
$result = new \stdClass(); $result = new \stdClass();

View File

@@ -1,13 +1,11 @@
<?php <?php
/** /**
* Created by PhpStorm. * Created by PhpStorm.
* User: junade * User: junade
* Date: 09/06/2017 * Date: 09/06/2017
* Time: 15:31 * Time: 15:31
*/ */
use Cloudflare\API\Endpoints\DNS;
class DNSTest extends PHPUnit_Framework_TestCase class DNSTest extends PHPUnit_Framework_TestCase
{ {
public function testAddRecord() public function testAddRecord()
@@ -40,7 +38,13 @@ class DNSTest extends PHPUnit_Framework_TestCase
$mock->expects($this->once()) $mock->expects($this->once())
->method('post') ->method('post')
->with($this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records'), $this->equalTo([]), ->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]) $this->equalTo([
'type' => 'A',
'name' => 'example.com',
'content' => '127.0.0.1',
'ttl' => 120,
'proxied' => false
])
); );
$dns = new \Cloudflare\API\Endpoints\DNS($mock); $dns = new \Cloudflare\API\Endpoints\DNS($mock);
@@ -77,28 +81,19 @@ class DNSTest extends PHPUnit_Framework_TestCase
"total_count": 2000 "total_count": 2000
} }
}'); }');
$response = new GuzzleHttp\Psr7\Response(200, ['Content-Type' => 'application/json'], $stream); $response = new GuzzleHttp\Psr7\Response(200, ['Content-Type' => 'application/json'], $stream);
$mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock();
$mock->method('get')->willReturn($response); $mock->method('get')->willReturn($response);
$mock->expects($this->once()) $mock->expects($this->once())
->method('get') ->method('get')
->with($this->equalTo('zones'), ->with($this->equalTo('zones/023e105f4ecef8ad9ca31a8372d0c353/dns_records?page=1&per_page=20&match=all&type=A&name=example.com&content=127.0.0.1&order=type&direction=desc'),
$this->equalTo([]), $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); $zones = new \Cloudflare\API\Endpoints\DNS($mock);
$result = $zones->listZones("example.com", "active", 1, 20, "status", "desc", "all"); $result = $zones->listRecords("023e105f4ecef8ad9ca31a8372d0c353","A", "example.com", "127.0.0.1", 1, 20, "type", "desc", "all");
$this->assertObjectHasAttribute('result', $result); $this->assertObjectHasAttribute('result', $result);
$this->assertObjectHasAttribute('result_info', $result); $this->assertObjectHasAttribute('result_info', $result);

View File

@@ -1,13 +1,11 @@
<?php <?php
/** /**
* Created by PhpStorm. * Created by PhpStorm.
* User: junade * User: junade
* Date: 06/06/2017 * Date: 06/06/2017
* Time: 16:01 * Time: 16:01
*/ */
use Cloudflare\API\Endpoints\Zones;
class ZonesTest extends PHPUnit_Framework_TestCase class ZonesTest extends PHPUnit_Framework_TestCase
{ {
public function testAddZone() public function testAddZone()
@@ -198,18 +196,9 @@ class ZonesTest extends PHPUnit_Framework_TestCase
$mock->expects($this->once()) $mock->expects($this->once())
->method('get') ->method('get')
->with($this->equalTo('zones'), ->with($this->equalTo('zones?page=1&per_page=20&match=all&name=example.com&status=active&order=status&direction=desc'),
$this->equalTo([]), $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); $zones = new \Cloudflare\API\Endpoints\Zones($mock);
$result = $zones->listZones("example.com", "active", 1, 20, "status", "desc", "all"); $result = $zones->listZones("example.com", "active", 1, 20, "status", "desc", "all");
@@ -291,15 +280,9 @@ class ZonesTest extends PHPUnit_Framework_TestCase
$mock->expects($this->once()) $mock->expects($this->once())
->method('get') ->method('get')
->with($this->equalTo('zones'), ->with($this->equalTo('zones?page=1&per_page=20&match=all&name=example.com'),
$this->equalTo([]), $this->equalTo([])
$this->equalTo([ );
'name' => 'example.com',
'page' => 1,
'per_page' => 20,
'match' => 'all'
]
));
$zones = new \Cloudflare\API\Endpoints\Zones($mock); $zones = new \Cloudflare\API\Endpoints\Zones($mock);
$result = $zones->getZoneID("example.com"); $result = $zones->getZoneID("example.com");