Skip to content

Commit

Permalink
Merge pull request #201 from phily245/update-safedns-record-create
Browse files Browse the repository at this point in the history
Update safedns record create
  • Loading branch information
phily245 authored Jan 28, 2020
2 parents a785579 + ecd5906 commit f31392c
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 22 deletions.
17 changes: 9 additions & 8 deletions src/SafeDNS/Entities/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
use UKFast\SDK\Entity;

/**
* @property integer $id
* @property string $zone
* @property string $name
* @property string $type
* @property string $content
* @property int $ttl
* @property int $priority
* @property integer $id
* @property string $zone
* @property string $name
* @property string $type
* @property string $content
* @property int $ttl
* @property int $priority
* @property \DateTime $updatedAt
*/
class Record extends Entity
{
//
protected $dates = ['updatedAt'];
}
33 changes: 19 additions & 14 deletions src/SafeDNS/RecordClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

namespace UKFast\SDK\SafeDNS;

use UKFast\SDK\Client;
use UKFast\SDK\Client as BaseClient;
use UKFast\SDK\Entities\ClientEntityInterface;
use UKFast\SDK\Page;
use UKFast\SDK\SafeDNS\Entities\Record;
use UKFast\SDK\SelfResponse;

class RecordClient extends Client implements ClientEntityInterface
class RecordClient extends BaseClient implements ClientEntityInterface
{
protected $basePath = 'safedns/';

const RECORD_MAP = [
'updated_at' => 'updatedAt'
];

/**
* Get records by zone name
* @param $zoneName
Expand Down Expand Up @@ -59,21 +64,21 @@ public function getZoneRecordById($zoneName, $id)
*/
public function create(Record $record)
{
$data = [
'name' => $record->name,
'type' => strtoupper($record->type),
'content' => $record->content,
];
$data = $this->friendlyToApi($record, static::RECORD_MAP);
$data['type'] = strtoupper($data['type']);

$response = $this->post("v1/zones/".$record->zone."/records", json_encode($data), [
'Content-Type' => 'application/json'
]);

$body = $this->decodeJson($response->getBody()->getContents());
$response = $this->post('v1/zones/' . $record->zone . '/records', json_encode($data));
$body = $this->decodeJson($response->getBody()->getContents());
$zoneName = $record->zone;

$record->id = $body->data->id;
return (new SelfResponse($body))
->setClient($this)
->serializeWith(function ($body) use ($zoneName) {
$record = new Record($body->data);
$record->zone = $zoneName;

return $record;
return $record;
});
}

/**
Expand Down
115 changes: 115 additions & 0 deletions tests/SafeDns/RecordClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

namespace Tests\SafeDns;

use DateTime;
use Faker\Factory as Faker;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;
use UKFast\SDK\SafeDNS\Client as SafeDnsClient;
use UKFast\SDK\SafeDNS\Entities\Record;
use UKFast\SDK\SafeDNS\RecordClient;
use UKFast\SDK\SelfResponse;

class RecordClientTest extends TestCase
{
/**
* @var Faker $faker
*/
protected $faker;

protected function setUp()
{
parent::setUp();

$this->faker = Faker::create();
}

/**
* @test
*/
public function can_get_from_safedns_client()
{
$this->assertInstanceOf(RecordClient::class, (new SafeDnsClient())->records());
}

/**
* @test
* @throws \Exception
*/
public function constructs_from_response()
{
$response = [
'id' => $this->faker->uuid,
'name' => $this->faker->domainName,
'type' => $this->faker->word,
'content' => $this->faker->ipv4,
'ttl' => $this->faker->numberBetween(300, 86400),
'priority' => null,
'updated_at' => $this->faker->dateTime('now', 'GMT')->format(DateTime::ATOM),
];

$recordClient = new RecordClient;
$record = new Record($recordClient->apiToFriendly(
$response,
RecordClient::RECORD_MAP
));

$this->assertEquals($response['id'], $record->id);
$this->assertEquals($response['name'], $record->name);
$this->assertEquals($response['type'], $record->type);
$this->assertEquals($response['content'], $record->content);
$this->assertEquals($response['ttl'], $record->ttl);
$this->assertEquals($response['priority'], $record->priority);
$this->assertInstanceOf(DateTime::class, $record->updatedAt);
$this->assertEquals($response['updated_at'], $record->updatedAt->format(\DateTime::ATOM));
}

/**
* @test
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function creates_and_gets_ssl_correctly()
{
$domainName = $this->faker->domainName;
$apiData = [
'id' => $this->faker->uuid,
'name' => $this->faker->domainWord . '.' . $domainName,
'type' => $this->faker->word,
'content' => $this->faker->ipv4,
'ttl' => $this->faker->numberBetween(300, 86400),
'priority' => null,
'updated_at' => $this->faker->dateTime('now', 'GMT')->format(DateTime::ATOM),
];

$mockHandler = new MockHandler([
new Response(201, [], json_encode([
'data' => [
'id' => $apiData['id'],
],
'meta' => [
'location' => 'http://localhost/safedns/v1/zones/' . $domainName . '/' . $apiData['id'],
],
])),
new Response(200, [], json_encode([
'data' => $apiData,
'meta' => [],
])),
]);

$httpClient = new GuzzleClient(['handler' => HandlerStack::create($mockHandler)]);
$client = new RecordClient($httpClient);

$inputRecord = new Record($client->apiToFriendly($apiData, RecordClient::RECORD_MAP));
$inputRecord->zone = $domainName;

$createResponse = $client->create($inputRecord);

$this->assertInstanceOf(SelfResponse::class, $createResponse);

$this->assertInstanceOf(Record::class, $createResponse->get());
}
}

0 comments on commit f31392c

Please sign in to comment.