-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #195 from phily245/master
Adds A DDoSX SSL Client
- Loading branch information
Showing
4 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |