Skip to content

Commit

Permalink
Merge pull request #199 from phily245/ddosx-ssl-create
Browse files Browse the repository at this point in the history
Add DDoSX SSL Creation
  • Loading branch information
phily245 authored Jan 27, 2020
2 parents 715da12 + 6585446 commit c0065e9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/DDoSX/SslClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

class SslClient extends BaseClient
{
Expand Down Expand Up @@ -40,4 +41,23 @@ public function getPage($page = 1, $perPage = 20, $filters = [])

return $page;
}

/**
* Send the request to the API to store a new job
* @param Ssl $ssl
* @return SelfResponse
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function create(Ssl $ssl)
{
$data = $this->friendlyToApi($ssl, static::CERTIFICATE_MAP);
$response = $this->post('v1/ssls', json_encode($data));
$body = $this->decodeJson($response->getBody()->getContents());

return (new SelfResponse($body))
->setClient($this)
->serializeWith(function ($body) {
return new Ssl($this->apiToFriendly($body->data, self::CERTIFICATE_MAP));
});
}
}
41 changes: 41 additions & 0 deletions tests/DDoSX/SslClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use UKFast\SDK\DDoSX\Client as DdosxClient;
use UKFast\SDK\DDoSX\Entities\Ssl;
use UKFast\SDK\DDoSX\SslClient;
use UKFast\SDK\SelfResponse;

class SslClientTest extends TestCase
{
Expand Down Expand Up @@ -123,4 +124,44 @@ public function gets_ssl_page()
$this->assertInstanceOf(Ssl::class, $ssls[0]);
$this->assertInstanceOf(Ssl::class, $ssls[1]);
}

/**
* @test
*/
public function creates_and_gets_ssl_correctly()
{
$apiData = [
'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),
];

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

$httpClient = new GuzzleClient(['handler' => HandlerStack::create($mockHandler)]);
$client = new SslClient($httpClient);
$createResponse = $client->create(new Ssl($client->apiToFriendly($apiData, SslClient::CERTIFICATE_MAP)));

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

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

0 comments on commit c0065e9

Please sign in to comment.