diff --git a/src/DDoSX/SslClient.php b/src/DDoSX/SslClient.php index f08c7eb6..aa09166b 100644 --- a/src/DDoSX/SslClient.php +++ b/src/DDoSX/SslClient.php @@ -4,6 +4,7 @@ use UKFast\SDK\Client as BaseClient; use UKFast\SDK\DDoSX\Entities\Ssl; +use UKFast\SDK\SelfResponse; class SslClient extends BaseClient { @@ -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)); + }); + } } diff --git a/tests/DDoSX/SslClientTest.php b/tests/DDoSX/SslClientTest.php index 995cd78c..5bcade40 100644 --- a/tests/DDoSX/SslClientTest.php +++ b/tests/DDoSX/SslClientTest.php @@ -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 { @@ -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()); + } }