diff --git a/README.md b/README.md index f07db941..63274f6a 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ Each API call is provided via a similarly named function within various classes - [x] Crypto - [x] Load Balancers - [x] Firewall Settings +- [x] [Images](https://www.cloudflare.com/products/cloudflare-images/) Note that this repository is currently under development, additional classes and endpoints being actively added. @@ -42,6 +43,16 @@ $user = new Cloudflare\API\Endpoints\User($adapter); echo $user->getUserID(); ``` +## Getting started with images + +```php +$key = new Cloudflare\API\Auth\APIToken('apiToken'); +$adapter = new Cloudflare\API\Adapter\Guzzle($key); +$images = new Cloudflare\API\Endpoints\Images($adapter); + +var_dump($images->listImages('accountId')); +``` + ## Contributions We welcome community contribution to this repository. [CONTRIBUTING.md](CONTRIBUTING.md) will help you start contributing. diff --git a/src/Adapter/Adapter.php b/src/Adapter/Adapter.php index 44d83c51..2cd8f3f6 100644 --- a/src/Adapter/Adapter.php +++ b/src/Adapter/Adapter.php @@ -45,7 +45,7 @@ public function get(string $uri, array $data = [], array $headers = []): Respons * * @return mixed */ - public function post(string $uri, array $data = [], array $headers = []): ResponseInterface; + public function post(string $uri, array $data = [], array $headers = [], bool $upload = false): ResponseInterface; /** * @param string $uri diff --git a/src/Adapter/Guzzle.php b/src/Adapter/Guzzle.php index 71e7dd75..4a7c8e9a 100644 --- a/src/Adapter/Guzzle.php +++ b/src/Adapter/Guzzle.php @@ -41,9 +41,9 @@ public function get(string $uri, array $data = [], array $headers = []): Respons /** * @inheritDoc */ - public function post(string $uri, array $data = [], array $headers = []): ResponseInterface + public function post(string $uri, array $data = [], array $headers = [], bool $upload = false): ResponseInterface { - return $this->request('post', $uri, $data, $headers); + return $this->request('post', $uri, $data, $headers, $upload); } /** @@ -73,7 +73,7 @@ public function delete(string $uri, array $data = [], array $headers = []): Resp /** * @SuppressWarnings(PHPMD.StaticAccess) */ - public function request(string $method, string $uri, array $data = [], array $headers = []) + public function request(string $method, string $uri, array $data = [], array $headers = [], bool $upload = false) { if (!in_array($method, ['get', 'post', 'put', 'patch', 'delete'])) { throw new \InvalidArgumentException('Request method must be get, post, put, patch, or delete'); @@ -82,7 +82,7 @@ public function request(string $method, string $uri, array $data = [], array $he try { $response = $this->client->$method($uri, [ 'headers' => $headers, - ($method === 'get' ? 'query' : 'json') => $data, + ($method === 'get' ? 'query' : ($upload ? 'multipart' : 'json')) => $data, ]); } catch (RequestException $err) { throw ResponseException::fromRequestException($err); diff --git a/src/Endpoints/CustomHostnames.php b/src/Endpoints/CustomHostnames.php index 7a0d2de3..3fc10e01 100644 --- a/src/Endpoints/CustomHostnames.php +++ b/src/Endpoints/CustomHostnames.php @@ -35,6 +35,7 @@ public function __construct(Adapter $adapter) * @param bool $wildcard * @param string $bundleMethod * @param array $customSsl + * @param string $sslCertificateAuthority * @return \stdClass */ public function addHostname( @@ -46,7 +47,8 @@ public function addHostname( string $customOriginServer = '', bool $wildcard = false, string $bundleMethod = '', - array $customSsl = [] + array $customSsl = [], + string $sslCertificateAuthority = 'lets_encrypt' ): \stdClass { $options = [ 'hostname' => $hostname, @@ -74,6 +76,10 @@ public function addHostname( $options['ssl']['custom_certificate'] = $customSsl['certificate']; } + if (!empty($sslCertificateAuthority)) { + $options['ssl']['certificate_authority'] = $sslCertificateAuthority; + } + $zone = $this->adapter->post('zones/'.$zoneID.'/custom_hostnames', $options); $this->body = json_decode($zone->getBody()); return $this->body->result; @@ -98,7 +104,9 @@ public function listHostnames( int $perPage = 20, string $order = '', string $direction = '', - int $ssl = 0 + int $ssl = 0, + string $hostname_status = '', + string $ssl_status = '' ): \stdClass { $query = [ 'page' => $page, @@ -122,6 +130,14 @@ public function listHostnames( $query['direction'] = $direction; } + if (!empty($hostname_status)) { + $query['hostname_status'] = $hostname_status; + } + + if (!empty($ssl_status)) { + $query['ssl_status'] = $ssl_status; + } + $zone = $this->adapter->get('zones/'.$zoneID.'/custom_hostnames', $query); $this->body = json_decode($zone->getBody()); @@ -155,6 +171,7 @@ public function getHostname(string $zoneID, string $hostnameID) * @param bool|null $wildcard * @param string $bundleMethod * @param array $customSsl + * @param string $sslCertificateAuthority * @return \stdClass */ public function updateHostname( @@ -166,7 +183,8 @@ public function updateHostname( string $customOriginServer = '', bool $wildcard = null, string $bundleMethod = '', - array $customSsl = [] + array $customSsl = [], + string $sslCertificateAuthority = 'lets_encrypt' ): \stdClass { $query = []; $options = []; @@ -199,6 +217,10 @@ public function updateHostname( $query['custom_certificate'] = $customSsl['certificate']; } + if (!empty($sslCertificateAuthority)) { + $query['certificate_authority'] = $sslCertificateAuthority; + } + if (!empty($query)) { $options = [ 'ssl' => $query diff --git a/src/Endpoints/Images.php b/src/Endpoints/Images.php new file mode 100644 index 00000000..99704bd0 --- /dev/null +++ b/src/Endpoints/Images.php @@ -0,0 +1,340 @@ + + * Date: 05/11/2022 + * Time: 22:36 + */ + +namespace Cloudflare\API\Endpoints; + +use Cloudflare\API\Adapter\Adapter; +use Cloudflare\API\Traits\BodyAccessorTrait; + +class Images implements API +{ + use BodyAccessorTrait; + + private $adapter; + + public function __construct(Adapter $adapter) + { + $this->adapter = $adapter; + } + + /** + * @SuppressWarnings(PHPMD.BooleanArgumentFlag) + * + * @param string $accountId + * @param string $fileOrUrl The path to the file or URL to image + * @param string $id + * @param bool $requireSignedURLs + * @param array $metadata + * @return object + */ + public function uploadImage( + string $accountId, + string $fileOrUrl, + string $id = '', + string $filename = '', + bool $requireSignedURLs = false, + array $metadata = [] + ): \stdClass { + if (filter_var($fileOrUrl, FILTER_VALIDATE_URL)) { + $data = [ + [ + 'name' => 'url', + 'contents' => $fileOrUrl, + ] + ]; + } else { + $data = [ + [ + 'name' => 'file', + 'contents' => file_get_contents($fileOrUrl), + 'filename' => !empty($filename) ? $filename : basename($fileOrUrl), + ] + ]; + } + + if (!empty($id)) { + $data[] = [ + 'name' => 'id', + 'contents' => $id, + ]; + } + + if ($requireSignedURLs) { + $data[] = [ + 'name' => 'requireSignedURLs', + 'contents' => $requireSignedURLs, + ]; + } + + if ($metadata) { + $data[] = [ + 'name' => 'metadata', + 'contents' => json_encode($metadata), + ]; + } + + $response = $this->adapter->post('accounts/' . $accountId . '/images/v1', $data, [], true); + + $this->body = json_decode($response->getBody()); + + return $this->body; + } + + /** + * @param string $accountId + * @param integer $page + * @param integer $perPage + * @return object + */ + public function listImages(string $accountId, int $page = 1, int $perPage = 20): \stdClass { + $query = [ + 'page' => $page, + 'per_page' => $perPage, + ]; + + $response = $this->adapter->get('accounts/' . $accountId . '/images/v1', $query); + + $this->body = json_decode($response->getBody()); + + return $this->body; + } + + /** + * @param string $accountId + * @param string $imageId + * @return string + */ + public function getImageBlob(string $accountId, string $imageId): string + { + $response = $this->adapter->get('accounts/' . $accountId . '/images/v1/' . $imageId . '/blob'); + + $this->body = $response->getBody(); + + return $this->body; + } + + /** + * @param string $accountId + * @param string $imageId + * @return object + */ + public function getImageDetails(string $accountId, string $imageId): \stdClass + { + $response = $this->adapter->get('accounts/' . $accountId . '/images/v1/' . $imageId); + + $this->body = json_decode($response->getBody()); + + return $this->body; + } + + /** + * @SuppressWarnings(PHPMD.BooleanArgumentFlag) + * + * @param string $accountId + * @param string $imageId + * @param bool $requireSignedURLs + * @param array $metadata + * @return object + */ + public function updateImage( + string $accountId, + string $imageId, + bool $requireSignedURLs = false, + array $metadata = [] + ): \stdClass { + $data = [ + 'requireSignedURLs' => $requireSignedURLs, + 'metadata' => $metadata, + ]; + + $response = $this->adapter->patch('accounts/' . $accountId . '/images/v1/' . $imageId, $data); + + $this->body = json_decode($response->getBody()); + + return $this->body; + } + + /** + * @param string $accountId + * @param string $imageId + * @return bool + */ + public function deleteImage(string $accountId, string $imageId): bool + { + $response = $this->adapter->delete('accounts/' . $accountId . '/images/v1/' . $imageId); + + $this->body = json_decode($response->getBody()); + + return $this->body->success && empty(get_object_vars($this->body->result)); + } + + /** + * @param string $accountId + * @return object + */ + public function getStats(string $accountId): \stdClass + { + $response = $this->adapter->get('accounts/' . $accountId . '/images/v1/stats'); + + $this->body = json_decode($response->getBody()); + + return $this->body; + } + + /** + * @SuppressWarnings(PHPMD.BooleanArgumentFlag) + * + * @param string $accountId + * @param string $imageId + * @param bool $requireSignedURLs + * @param array $metadata + * @return object + */ + public function addVariant( + string $accountId, + string $variantId, + string $fit, + string $metadata, + int $width, + int $height, + bool $neverRequireSignedURLs = false + ): \stdClass { + $data = [ + 'id' => $variantId, + 'options' => [ + 'fit' => $fit, + 'metadata' => $metadata, + 'width' => $width, + 'height' => $height, + ], + ]; + + if ($neverRequireSignedURLs) { + $data['neverRequireSignedURLs'] = $neverRequireSignedURLs; + } + + $response = $this->adapter->post('accounts/' . $accountId . '/images/v1/variants', $data); + + $this->body = json_decode($response->getBody()); + + return $this->body; + } + + /** + * @param string $accountId + * @return object + */ + public function listVariants(string $accountId): \stdClass + { + $response = $this->adapter->get('accounts/' . $accountId . '/images/v1/variants'); + + $this->body = json_decode($response->getBody()); + + return $this->body; + } + + /** + * @param string $accountId + * @param string $variantId + * @return object + */ + public function getVariantDetails(string $accountId, string $variantId): \stdClass + { + $response = $this->adapter->get('accounts/' . $accountId . '/images/v1/variants/' . $variantId); + + $this->body = json_decode($response->getBody()); + + return $this->body; + } + + /** + * @SuppressWarnings(PHPMD.BooleanArgumentFlag) + * + * @param string $accountId + * @param string $variantId + * @param string $fit + * @param string $metadata + * @param integer $width + * @param integer $height + * @param bool $neverRequireSignedURLs + * @return object + */ + public function updateVariant( + string $accountId, + string $variantId, + string $fit, + string $metadata, + int $width, + int $height, + bool $neverRequireSignedURLs = false + ): \stdClass { + $data = [ + 'options' => [ + 'fit' => $fit, + 'metadata' => $metadata, + 'width' => $width, + 'height' => $height, + ], + ]; + + $data['neverRequireSignedURLs'] = $neverRequireSignedURLs; + + $response = $this->adapter->patch('accounts/' . $accountId . '/images/v1/variants/' . $variantId, $data); + + $this->body = json_decode($response->getBody()); + + return $this->body; + } + + /** + * @param string $accountId + * @param bool $status + * @return object + */ + public function updateFlexibleVariantsStatus( + string $accountId, + bool $status + ): \stdClass { + $data = [ + 'flexible_variants' => $status + ]; + + $response = $this->adapter->patch('accounts/' . $accountId . '/images/v1/config', $data); + + $this->body = json_decode($response->getBody()); + + return $this->body; + } + + /** + * @param string $accountId + * @param string $variantId + * @return bool + */ + public function deleteVariant(string $accountId, string $variantId): bool + { + $response = $this->adapter->delete('accounts/' . $accountId . '/images/v1/variants/' . $variantId); + + $this->body = json_decode($response->getBody()); + + return $this->body->success && empty(get_object_vars($this->body->result)); + } + + /** + * @param string $accountId + * @return object + */ + public function listKeys(string $accountId): \stdClass + { + $response = $this->adapter->get('accounts/' . $accountId . '/images/v1/keys'); + + $this->body = json_decode($response->getBody()); + + return $this->body; + } +} diff --git a/tests/Endpoints/ImagesTest.php b/tests/Endpoints/ImagesTest.php new file mode 100644 index 00000000..f4967086 --- /dev/null +++ b/tests/Endpoints/ImagesTest.php @@ -0,0 +1,448 @@ + + * Date: 06/11/2022 + * Time: 13:00 + */ +class ImagesTest extends TestCase +{ + private $accountId = '65as4d564asd65as1d31a2s3d465asd4'; + + public function testListImages() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/listImages.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('accounts/' . $this->accountId . '/images/v1') + ); + + $images = new \Cloudflare\API\Endpoints\Images($mock); + $result = $images->listImages($accountId = $this->accountId); + + $this->assertObjectHasAttribute('result', $result); + $this->assertObjectHasAttribute('images', $result->result); + + $this->assertEquals('KAQA3231344', $result->result->images[0]->id); + } + + public function testGetImageDetails() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/getImageDetails.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('accounts/' . $this->accountId . '/images/v1/KAQA3231344') + ); + + $images = new \Cloudflare\API\Endpoints\Images($mock); + $result = $images->getImageDetails($accountId = $this->accountId, $imageId = 'KAQA3231344'); + + $this->assertObjectHasAttribute('result', $result); + + $this->assertEquals('KAQA3231344', $result->result->id); + } + + public function testGetImageBlob() + { + $response = file_get_contents(__DIR__ . '/../Fixtures/Cloudflare_Logo.svg.png'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('get')->willReturn(new Psr7\Response(200, ['Content-Type' => 'image/png'], $response)); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('accounts/' . $this->accountId . '/images/v1/KAQA3231344/blob') + ); + + $images = new \Cloudflare\API\Endpoints\Images($mock); + $result = $images->getImageBlob($accountId = $this->accountId, $imageId = 'KAQA3231344'); + + $this->assertEquals($response, $result); + } + + public function testUploadImage() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/uploadImage.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('post')->willReturn($response); + + $mock->expects($this->once()) + ->method('post') + ->with( + $this->equalTo('accounts/' . $this->accountId . '/images/v1'), + $this->equalTo([ + [ + 'name' => 'file', + 'contents' => file_get_contents(__DIR__ . '/../Fixtures/Cloudflare_Logo.svg.png'), + 'filename' => 'Cloudflare_Logo.svg.png', + ], + ]) + ); + + $images = new \Cloudflare\API\Endpoints\Images($mock); + $result = $images->uploadImage( + $accountId = $this->accountId, + $fileOrUrl = __DIR__ . '/../Fixtures/Cloudflare_Logo.svg.png' + ); + + $this->assertObjectHasAttribute('result', $result); + $this->assertObjectHasAttribute('id', $result->result); + + $this->assertEquals('a4e7a418-4a8q-a158-5qap-a4125q789421', $result->result->id); + $this->assertEquals(false, $result->result->requireSignedURLs); + } + + public function testImportImage() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/importImage.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('post')->willReturn($response); + + $mock->expects($this->once()) + ->method('post') + ->with( + $this->equalTo('accounts/' . $this->accountId . '/images/v1'), + $this->equalTo([ + [ + 'name' => 'url', + 'contents' => 'https://upload.wikimedia.org/wikipedia/commons/thumb/4/4b/Cloudflare_Logo.svg/1200px-Cloudflare_Logo.svg.png', + ], + ]) + ); + + $images = new \Cloudflare\API\Endpoints\Images($mock); + $result = $images->uploadImage( + $accountId = $this->accountId, + $fileOrUrl = 'https://upload.wikimedia.org/wikipedia/commons/thumb/4/4b/Cloudflare_Logo.svg/1200px-Cloudflare_Logo.svg.png' + ); + + $this->assertObjectHasAttribute('result', $result); + $this->assertObjectHasAttribute('id', $result->result); + + $this->assertEquals('1a54q565-3f3c-a4q2-5a1q-2a56q61aaawd', $result->result->id); + } + + public function testImportImageWithCustomId() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/importImageWithCustomId.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('post')->willReturn($response); + + $mock->expects($this->once()) + ->method('post') + ->with( + $this->equalTo('accounts/' . $this->accountId . '/images/v1'), + $this->equalTo([ + [ + 'name' => 'url', + 'contents' => 'https://upload.wikimedia.org/wikipedia/commons/thumb/4/4b/Cloudflare_Logo.svg/1200px-Cloudflare_Logo.svg.png', + ], + [ + 'name' => 'id', + 'contents' => '65a4sd23asd56asd654asd564', + ], + ]) + ); + + $images = new \Cloudflare\API\Endpoints\Images($mock); + $result = $images->uploadImage( + $accountId = $this->accountId, + $fileOrUrl = 'https://upload.wikimedia.org/wikipedia/commons/thumb/4/4b/Cloudflare_Logo.svg/1200px-Cloudflare_Logo.svg.png', + $id = '65a4sd23asd56asd654asd564' + ); + + $this->assertObjectHasAttribute('result', $result); + $this->assertObjectHasAttribute('id', $result->result); + + $this->assertEquals('65a4sd23asd56asd654asd564', $result->result->id); + } + + public function testUpdateImage() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/updateImage.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('patch')->willReturn($response); + + $mock->expects($this->once()) + ->method('patch') + ->with( + $this->equalTo('accounts/' . $this->accountId . '/images/v1/1a54q565-3f3c-a4q2-5a1q-2a56q61aaawd'), + $this->equalTo([ + 'requireSignedURLs' => true, + 'metadata' => [], + ]) + ); + + $images = new \Cloudflare\API\Endpoints\Images($mock); + $result = $images->updateImage( + $accountId = $this->accountId, + $imageId = '1a54q565-3f3c-a4q2-5a1q-2a56q61aaawd', + $requireSignedURLs = true + ); + + $this->assertObjectHasAttribute('result', $result); + $this->assertObjectHasAttribute('id', $result->result); + + $this->assertEquals(true, $result->result->requireSignedURLs); + } + + public function testDeleteImage() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/deleteImage.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('delete')->willReturn($response); + + $mock->expects($this->once()) + ->method('delete') + ->with( + $this->equalTo('accounts/' . $this->accountId . '/images/v1/1a54q565-3f3c-a4q2-5a1q-2a56q61aaawd') + ); + + $images = new \Cloudflare\API\Endpoints\Images($mock); + $result = $images->deleteImage( + $accountId = $this->accountId, + $imageId = '1a54q565-3f3c-a4q2-5a1q-2a56q61aaawd' + ); + + $this->assertEquals(true, $result); + } + + public function testGetStats() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/imagesGetStats.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('accounts/' . $this->accountId . '/images/v1/stats') + ); + + $images = new \Cloudflare\API\Endpoints\Images($mock); + $result = $images->getStats($accountId = $this->accountId); + + $this->assertObjectHasAttribute('result', $result); + $this->assertObjectHasAttribute('count', $result->result); + + $this->assertEquals('7', $result->result->count->current); + } + + public function testListVariants() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/imagesListVariants.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('accounts/' . $this->accountId . '/images/v1/variants') + ); + + $images = new \Cloudflare\API\Endpoints\Images($mock); + $result = $images->listVariants($accountId = $this->accountId); + + $this->assertObjectHasAttribute('result', $result); + $this->assertObjectHasAttribute('variants', $result->result); + $this->assertObjectHasAttribute('myVariant', $result->result->variants); + } + + public function testGetVariantDetails() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/imagesGetVariantDetails.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('accounts/' . $this->accountId . '/images/v1/variants/myVariant') + ); + + $images = new \Cloudflare\API\Endpoints\Images($mock); + $result = $images->getVariantDetails($accountId = $this->accountId, $variantId = 'myVariant'); + + $this->assertObjectHasAttribute('result', $result); + $this->assertObjectHasAttribute('variant', $result->result); + + $this->assertEquals('myVariant', $result->result->variant->id); + } + + public function testAddVariant() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/imagesAddVariant.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('post')->willReturn($response); + + $mock->expects($this->once()) + ->method('post') + ->with( + $this->equalTo('accounts/' . $this->accountId . '/images/v1/variants'), + $this->equalTo([ + 'id' => 'myVariant', + 'options' => [ + 'fit' => 'scale-down', + 'metadata' => 'none', + 'width' => 1000, + 'height' => 1000, + ], + ]) + ); + + $images = new \Cloudflare\API\Endpoints\Images($mock); + $result = $images->addVariant( + $accountId = $this->accountId, + $variantId = 'myVariant', + $fit = 'scale-down', + $metadata = 'none', + $width = 1000, + $height = 1000 + ); + + $this->assertObjectHasAttribute('result', $result); + $this->assertObjectHasAttribute('variant', $result->result); + $this->assertObjectHasAttribute('options', $result->result->variant); + + $this->assertEquals(1000, $result->result->variant->options->width); + $this->assertEquals(1000, $result->result->variant->options->height); + $this->assertEquals(false, $result->result->variant->neverRequireSignedURLs); + } + + public function testUpdateVariant() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/imagesUpdateVariant.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('patch')->willReturn($response); + + $mock->expects($this->once()) + ->method('patch') + ->with( + $this->equalTo('accounts/' . $this->accountId . '/images/v1/variants/myVariant'), + $this->equalTo([ + 'options' => [ + 'fit' => 'pad', + 'metadata' => 'none', + 'width' => 900, + 'height' => 900, + ], + 'neverRequireSignedURLs' => true, + ]) + ); + + $images = new \Cloudflare\API\Endpoints\Images($mock); + $result = $images->updateVariant( + $accountId = $this->accountId, + $variantId = 'myVariant', + $fit = 'pad', + $metadata = 'none', + $width = 900, + $height = 900, + $neverRequireSignedURLs = true + ); + + $this->assertObjectHasAttribute('result', $result); + $this->assertObjectHasAttribute('variant', $result->result); + $this->assertObjectHasAttribute('options', $result->result->variant); + + $this->assertEquals(900, $result->result->variant->options->width); + $this->assertEquals(900, $result->result->variant->options->height); + $this->assertEquals(true, $result->result->variant->neverRequireSignedURLs); + } + + public function testUpdateFlexibleVariantsStatus() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/imagesUpdateFlexibleVariantsStatus.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('patch')->willReturn($response); + + $mock->expects($this->once()) + ->method('patch') + ->with( + $this->equalTo('accounts/' . $this->accountId . '/images/v1/config'), + $this->equalTo([ + 'flexible_variants' => true, + ]) + ); + + $images = new \Cloudflare\API\Endpoints\Images($mock); + $result = $images->updateFlexibleVariantsStatus( + $accountId = $this->accountId, + $status = true + ); + + $this->assertObjectHasAttribute('result', $result); + $this->assertObjectHasAttribute('flexible_variants', $result->result); + + $this->assertEquals(true, $result->result->flexible_variants); + } + + public function testDeleteVariant() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/imagesDeleteVariant.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('delete')->willReturn($response); + + $mock->expects($this->once()) + ->method('delete') + ->with( + $this->equalTo('accounts/' . $this->accountId . '/images/v1/variants/myVariant') + ); + + $images = new \Cloudflare\API\Endpoints\Images($mock); + $result = $images->deleteVariant( + $accountId = $this->accountId, + $variantId = 'myVariant' + ); + + $this->assertEquals(true, $result); + } + + public function testListKeys() + { + $response = $this->getPsr7JsonResponseForFixture('Endpoints/imagesListKeys.json'); + + $mock = $this->getMockBuilder(\Cloudflare\API\Adapter\Adapter::class)->getMock(); + $mock->method('get')->willReturn($response); + + $mock->expects($this->once()) + ->method('get') + ->with( + $this->equalTo('accounts/' . $this->accountId . '/images/v1/keys') + ); + + $images = new \Cloudflare\API\Endpoints\Images($mock); + $result = $images->listKeys($accountId = $this->accountId); + + $this->assertObjectHasAttribute('result', $result); + $this->assertObjectHasAttribute('keys', $result->result); + + $this->assertEquals('default', $result->result->keys[0]->name); + } +} diff --git a/tests/Fixtures/Cloudflare_Logo.svg.png b/tests/Fixtures/Cloudflare_Logo.svg.png new file mode 100644 index 00000000..dddd255c Binary files /dev/null and b/tests/Fixtures/Cloudflare_Logo.svg.png differ diff --git a/tests/Fixtures/Endpoints/deleteImage.json b/tests/Fixtures/Endpoints/deleteImage.json new file mode 100644 index 00000000..78b99680 --- /dev/null +++ b/tests/Fixtures/Endpoints/deleteImage.json @@ -0,0 +1,6 @@ +{ + "result": {}, + "success": true, + "errors": [], + "messages": [] +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/getImageDetails.json b/tests/Fixtures/Endpoints/getImageDetails.json new file mode 100644 index 00000000..b102e80d --- /dev/null +++ b/tests/Fixtures/Endpoints/getImageDetails.json @@ -0,0 +1,15 @@ +{ + "result": { + "id": "KAQA3231344", + "filename": "D_NQ_NP_656209-KAQA3231344_092019-O.webp", + "meta": [], + "uploaded": "2022-11-06T05:25:47.011Z", + "requireSignedURLs": false, + "variants": [ + "https:\/\/imagedelivery.net\/65a4sd64564asd56a4d654\/KAQA3231344\/public" + ] + }, + "success": true, + "errors": [], + "messages": [] +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/imagesAddVariant.json b/tests/Fixtures/Endpoints/imagesAddVariant.json new file mode 100644 index 00000000..9b937c70 --- /dev/null +++ b/tests/Fixtures/Endpoints/imagesAddVariant.json @@ -0,0 +1,17 @@ +{ + "result": { + "variant": { + "id": "myVariant", + "options": { + "fit": "scale-down", + "metadata": "none", + "width": 1000, + "height": 1000 + }, + "neverRequireSignedURLs": false + } + }, + "success": true, + "errors": [], + "messages": [] +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/imagesDeleteVariant.json b/tests/Fixtures/Endpoints/imagesDeleteVariant.json new file mode 100644 index 00000000..78b99680 --- /dev/null +++ b/tests/Fixtures/Endpoints/imagesDeleteVariant.json @@ -0,0 +1,6 @@ +{ + "result": {}, + "success": true, + "errors": [], + "messages": [] +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/imagesGetStats.json b/tests/Fixtures/Endpoints/imagesGetStats.json new file mode 100644 index 00000000..98132403 --- /dev/null +++ b/tests/Fixtures/Endpoints/imagesGetStats.json @@ -0,0 +1,11 @@ +{ + "result": { + "count": { + "current": 7, + "allowed": 100000 + } + }, + "success": true, + "errors": [], + "messages": [] +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/imagesGetVariantDetails.json b/tests/Fixtures/Endpoints/imagesGetVariantDetails.json new file mode 100644 index 00000000..9b937c70 --- /dev/null +++ b/tests/Fixtures/Endpoints/imagesGetVariantDetails.json @@ -0,0 +1,17 @@ +{ + "result": { + "variant": { + "id": "myVariant", + "options": { + "fit": "scale-down", + "metadata": "none", + "width": 1000, + "height": 1000 + }, + "neverRequireSignedURLs": false + } + }, + "success": true, + "errors": [], + "messages": [] +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/imagesListKeys.json b/tests/Fixtures/Endpoints/imagesListKeys.json new file mode 100644 index 00000000..4ee7b66f --- /dev/null +++ b/tests/Fixtures/Endpoints/imagesListKeys.json @@ -0,0 +1,13 @@ +{ + "result": { + "keys": [ + { + "name": "default", + "value": "ad6465ad456as4d65sa4d665a4sd56sa" + } + ] + }, + "success": true, + "errors": [], + "messages": [] +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/imagesListVariants.json b/tests/Fixtures/Endpoints/imagesListVariants.json new file mode 100644 index 00000000..fc29a42b --- /dev/null +++ b/tests/Fixtures/Endpoints/imagesListVariants.json @@ -0,0 +1,29 @@ +{ + "result": { + "variants": { + "public": { + "id": "public", + "options": { + "fit": "scale-down", + "height": 768, + "metadata": "none", + "width": 1366 + }, + "neverRequireSignedURLs": false + }, + "myVariant": { + "id": "myVariant", + "options": { + "fit": "scale-down", + "metadata": "none", + "width": 1000, + "height": 1000 + }, + "neverRequireSignedURLs": false + } + } + }, + "success": true, + "errors": [], + "messages": [] +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/imagesUpdateFlexibleVariantsStatus.json b/tests/Fixtures/Endpoints/imagesUpdateFlexibleVariantsStatus.json new file mode 100644 index 00000000..63e6c439 --- /dev/null +++ b/tests/Fixtures/Endpoints/imagesUpdateFlexibleVariantsStatus.json @@ -0,0 +1,8 @@ +{ + "result": { + "flexible_variants": true + }, + "success": true, + "errors": [], + "messages": [] +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/imagesUpdateVariant.json b/tests/Fixtures/Endpoints/imagesUpdateVariant.json new file mode 100644 index 00000000..905f45f6 --- /dev/null +++ b/tests/Fixtures/Endpoints/imagesUpdateVariant.json @@ -0,0 +1,17 @@ +{ + "result": { + "variant": { + "id": "myVariant", + "options": { + "fit": "pad", + "metadata": "none", + "width": 900, + "height": 900 + }, + "neverRequireSignedURLs": true + } + }, + "success": true, + "errors": [], + "messages": [] +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/importImage.json b/tests/Fixtures/Endpoints/importImage.json new file mode 100644 index 00000000..ec67eb16 --- /dev/null +++ b/tests/Fixtures/Endpoints/importImage.json @@ -0,0 +1,15 @@ +{ + "result": { + "id": "1a54q565-3f3c-a4q2-5a1q-2a56q61aaawd", + "filename": "1200px-Cloudflare_Logo.svg.png", + "uploaded": "2022-11-06T18:23:31.303Z", + "requireSignedURLs": false, + "variants": [ + "https:\/\/imagedelivery.net\/65a4sd64564asd56a4d654\/1a54q565-3f3c-a4q2-5a1q-2a56q61aaawd\/public", + "https:\/\/imagedelivery.net\/65a4sd64564asd56a4d654\/1a54q565-3f3c-a4q2-5a1q-2a56q61aaawd\/myVariant" + ] + }, + "success": true, + "errors": [], + "messages": [] +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/importImageWithCustomId.json b/tests/Fixtures/Endpoints/importImageWithCustomId.json new file mode 100644 index 00000000..030bdefb --- /dev/null +++ b/tests/Fixtures/Endpoints/importImageWithCustomId.json @@ -0,0 +1,15 @@ +{ + "result": { + "id": "65a4sd23asd56asd654asd564", + "filename": "1200px-Cloudflare_Logo.svg.png", + "uploaded": "2022-11-06T17:31:46.047Z", + "requireSignedURLs": false, + "variants": [ + "https:\/\/imagedelivery.net\/65a4sd64564asd56a4d654\/65a4sd23asd56asd654asd564\/myVariant", + "https:\/\/imagedelivery.net\/65a4sd64564asd56a4d654\/65a4sd23asd56asd654asd564\/public" + ] + }, + "success": true, + "errors": [], + "messages": [] +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/listImages.json b/tests/Fixtures/Endpoints/listImages.json new file mode 100644 index 00000000..2a691d2d --- /dev/null +++ b/tests/Fixtures/Endpoints/listImages.json @@ -0,0 +1,28 @@ +{ + "result": { + "images": [ + { + "id": "KAQA3231344", + "filename": "D_NQ_NP_656209-KAQA3231344_092019-O.webp", + "meta": [], + "uploaded": "2022-11-06T05:25:47.011Z", + "requireSignedURLs": false, + "variants": [ + "https:\/\/imagedelivery.net\/65a4sd64564asd56a4d654\/KAQA3231344.webp\/public" + ] + }, + { + "id": "0dcd32af9f7b2e4cc7ce9f48a9a5e\/products\/test-sdk.jpg", + "filename": "D_NQ_NP_2X_895679-AD5654SDAS.webp", + "uploaded": "2022-11-06T05:49:20.738Z", + "requireSignedURLs": false, + "variants": [ + "https:\/\/imagedelivery.net\/65a4sd64564asd56a4d654\/0dcd32af9f7b2e4cc7ce9f48a9a5e\/products\/test-sdk.jpg\/public" + ] + } + ] + }, + "success": true, + "errors": [], + "messages": [] +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/updateImage.json b/tests/Fixtures/Endpoints/updateImage.json new file mode 100644 index 00000000..05ebd65e --- /dev/null +++ b/tests/Fixtures/Endpoints/updateImage.json @@ -0,0 +1,16 @@ +{ + "result": { + "id": "1a54q565-3f3c-a4q2-5a1q-2a56q61aaawd", + "filename": "1200px-Cloudflare_Logo.svg.png", + "meta": [], + "uploaded": "2022-11-06T18:23:30.233Z", + "requireSignedURLs": true, + "variants": [ + "https:\/\/imagedelivery.net\/65a4sd64564asd56a4d654\/1a54q565-3f3c-a4q2-5a1q-2a56q61aaawd\/public", + "https:\/\/imagedelivery.net\/65a4sd64564asd56a4d654\/1a54q565-3f3c-a4q2-5a1q-2a56q61aaawd\/myVariant" + ] + }, + "success": true, + "errors": [], + "messages": [] +} \ No newline at end of file diff --git a/tests/Fixtures/Endpoints/uploadImage.json b/tests/Fixtures/Endpoints/uploadImage.json new file mode 100644 index 00000000..f39f9b2f --- /dev/null +++ b/tests/Fixtures/Endpoints/uploadImage.json @@ -0,0 +1,15 @@ +{ + "result": { + "id": "a4e7a418-4a8q-a158-5qap-a4125q789421", + "filename": "Cloudflare_Logo.svg.png", + "uploaded": "2022-11-07T01:51:00.968Z", + "requireSignedURLs": false, + "variants": [ + "https:\/\/imagedelivery.net\/65a4sd64564asd56a4d654\/a4e7a418-4a8q-a158-5qap-a4125q789421\/myVariant", + "https:\/\/imagedelivery.net\/65a4sd64564asd56a4d654\/a4e7a418-4a8q-a158-5qap-a4125q789421\/public" + ] + }, + "success": true, + "errors": [], + "messages": [] +} \ No newline at end of file