Skip to content

Commit

Permalink
Merge pull request #195 from phily245/master
Browse files Browse the repository at this point in the history
Adds A DDoSX SSL Client
  • Loading branch information
phily245 authored Jan 27, 2020
2 parents 22c303b + 912fba5 commit 7e1cb3f
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/DDoSX/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,14 @@ public function records()
{
return (new RecordClient($this->httpClient))->auth($this->token);
}

/**
* Return a SslClient instance
*
* @return SslClient
*/
public function ssls()
{
return (new SslClient($this->httpClient))->auth($this->token);
}
}
22 changes: 22 additions & 0 deletions src/DDoSX/Entities/Ssl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace UKFast\SDK\DDoSX\Entities;

use UKFast\SDK\Entity;

/**
* @property string $id
* @property int|null $ukfastSslId
* @property string[] $domains
* @property string $friendlyName
* @property string $expiresAt
*/
class Ssl extends Entity
{
/**
* Fields to convert to \DateTime objects
*
* @var array
*/
protected $dates = ['expiresAt'];
}
43 changes: 43 additions & 0 deletions src/DDoSX/SslClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace UKFast\SDK\DDoSX;

use UKFast\SDK\Client as BaseClient;
use UKFast\SDK\DDoSX\Entities\Ssl;

class SslClient extends BaseClient
{
/**
* The API's basepath
*
* @var string $basepath
*/
protected $basePath = 'ddosx/';

const CERTIFICATE_MAP = [
'ukfast_ssl_id' => 'ukfastSslId',
'friendly_name' => 'friendlyName',
'expires_at' => 'expiresAt',
];

/**
* Gets a paginated list of SSLs
*
* @param int $page
* @param int $perPage
* @param array $filters
* @return \UKFast\SDK\Page
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getPage($page = 1, $perPage = 20, $filters = [])
{
$filters = $this->friendlyToApi($filters, self::CERTIFICATE_MAP);
$page = $this->paginatedRequest('v1/ssls', $page, $perPage, $filters);

$page->serializeWith(function ($item) {
return new Ssl($this->apiToFriendly($item, self::CERTIFICATE_MAP));
});

return $page;
}
}
126 changes: 126 additions & 0 deletions tests/DDoSX/SslClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace Tests\DDoSX;

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\DDoSX\Client as DdosxClient;
use UKFast\SDK\DDoSX\Entities\Ssl;
use UKFast\SDK\DDoSX\SslClient;

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

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

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

/**
* @test
*/
public function can_get_from_ddosx_client()
{
$this->assertInstanceOf(SslClient::class, (new DdosxClient())->ssls());
}

/**
* @test
* @throws \Exception
*/
public function constructs_from_response()
{
$response = [
'id' => $this->faker->uuid,
'ukfast_ssl_id' => $this->faker->randomDigit,
'domains' => [
$this->faker->domainName,
$this->faker->domainName,
],
'friendly_name' => $this->faker->word,
'expires_at' => $this->faker->dateTimeBetween('+1 day', '+825 days')->format(DateTime::ATOM),
];

$sslClient = new SslClient;

$ssl = new Ssl($sslClient->apiToFriendly(
$response,
SslClient::CERTIFICATE_MAP
));

$this->assertEquals($response['id'], $ssl->id);
$this->assertEquals($response['ukfast_ssl_id'], $ssl->ukfastSslId);
$this->assertEquals($response['domains'], $ssl->domains);
$this->assertEquals($response['friendly_name'], $ssl->friendlyName);
$this->assertInstanceOf(DateTime::class, $ssl->expiresAt);
$this->assertEquals($response['expires_at'], $ssl->expiresAt->format(\DateTime::ATOM));
}

/**
* @test
* @throws \Exception
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function gets_ssl_page()
{
$mockHandler = new MockHandler([
new Response(200, [], json_encode([
'data' => [
[
'id' => $this->faker->uuid,
'ukfast_ssl_id' => $this->faker->randomDigit,
'domains' => [
$this->faker->domainName,
$this->faker->domainName,
],
'friendly_name' => $this->faker->word,
'expires_at' => $this->faker->dateTimeBetween('+1 day', '+825 days')->format(DateTime::ATOM),
],
[
'id' => $this->faker->uuid,
'ukfast_ssl_id' => $this->faker->randomDigit,
'domains' => [
$this->faker->domainName,
$this->faker->domainName,
],
'friendly_name' => $this->faker->word,
'expires_at' => $this->faker->dateTimeBetween('+1 day', '+825 days')->format(DateTime::ATOM),
],
],
'meta' => [
'pagination' => [
'total' => 2,
'per_page' => 2,
'total_pages' => 1,
'links' => [
'next' => 'http://example.com/next',
'previous' => 'http://example.com/previous',
'first' => 'http://example.com/first',
'last' => 'http://example.com/last',
]
]
]
])),
]);

$httpClient = new GuzzleClient(['handler' => HandlerStack::create($mockHandler)]);
$client = new SslClient($httpClient);
$sslPage = $client->getPage();
$ssls = $sslPage->getItems();

$this->assertEquals(2, count($ssls));
$this->assertInstanceOf(Ssl::class, $ssls[0]);
$this->assertInstanceOf(Ssl::class, $ssls[1]);
}
}

0 comments on commit 7e1cb3f

Please sign in to comment.