From 5d65802914ba977a9dfc43e7fb31820c442e5c58 Mon Sep 17 00:00:00 2001 From: selul Date: Tue, 6 Aug 2024 15:01:19 +0300 Subject: [PATCH 1/3] Fix offloading logic --- src/Offload/Manager.php | 13 +++++++------ src/Optimole.php | 3 ++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Offload/Manager.php b/src/Offload/Manager.php index 1a5225c..7758147 100644 --- a/src/Offload/Manager.php +++ b/src/Offload/Manager.php @@ -114,7 +114,7 @@ public function getUsage(): OffloadUsage /** * Update the metadata of the image with the given ID. */ - public function updateImageMetadata(string $imageId, int $fileSize = 0, $height = 'auto', $width = 'auto'): void + public function updateImageMetadata(string $imageId, int $fileSize = 0, $height = 'auto', $width = 'auto', $originalUrl = ''): void { if ('auto' !== $height && !is_int($height)) { throw new InvalidArgumentException('Image height must be "auto" or an integer.'); @@ -127,6 +127,7 @@ public function updateImageMetadata(string $imageId, int $fileSize = 0, $height 'originalFileSize' => $fileSize, 'height' => is_int($height) ? max(0, $height) : $height, 'width' => is_int($width) ? max(0, $width) : $width, + 'originalUrl' => $originalUrl, 'updateDynamo' => 'success', ]); } @@ -179,7 +180,7 @@ public function uploadImage(string $filename, string $imageUrl): string } try { - $this->httpClient->sendRequest('put', $uploadUrl, $image, [ + $this->httpClient->sendRequest('PUT', $uploadUrl, $image, [ 'Content-Type' => $fileMimeType, ]); } catch (BadResponseException $exception) { @@ -188,7 +189,7 @@ public function uploadImage(string $filename, string $imageUrl): string $imagesize = getimagesize($filename); - $this->updateImageMetadata($imageId, filesize($filename) ?: 0, $imagesize && !empty($imagesize[1]) ? $imagesize[1] : 'auto', $imagesize && !empty($imagesize[0]) ? $imagesize[0] : 'auto'); + $this->updateImageMetadata($imageId, filesize($filename) ?: 0, $imagesize && !empty($imagesize[1]) ? $imagesize[1] : 'auto', $imagesize && !empty($imagesize[0]) ? $imagesize[0] : 'auto', $imageUrl); return $imageId; } @@ -237,8 +238,8 @@ private function getUploadApiCredentialsFromDashboardApi(): array */ private function requestToDashboardApi(): array { - return $this->httpClient->sendRequest('post', sprintf('%s/optml/v2/account/details', $this->options['dashboard_api_url']), null, [ - 'Authorization' => sprintf('Bearer %s', $this->key), + return $this->httpClient->sendRequest('POST', sprintf('%s/optml/v2/account/details', $this->options['dashboard_api_url']), null, [ + 'Authorization' => sprintf('Bearer %s', $this->options['dashboard_api_key']), 'Content-Type' => 'application/json', ]); } @@ -252,7 +253,7 @@ private function requestToUploadApi(array $body): array $this->options['upload_api_credentials'] = $this->getUploadApiCredentialsFromDashboardApi(); } - return $this->httpClient->sendRequest('post', $this->options['upload_api_url'], array_merge($this->options['upload_api_credentials'], $body), [ + return $this->httpClient->sendRequest('POST', $this->options['upload_api_url'], array_merge($this->options['upload_api_credentials'], $body), [ 'Content-Type' => 'application/json', ]); } diff --git a/src/Optimole.php b/src/Optimole.php index 00de057..0b082d6 100644 --- a/src/Optimole.php +++ b/src/Optimole.php @@ -33,7 +33,7 @@ final class Optimole /** * The Optimole SDK version. */ - public const VERSION = '1.2.0'; + public const VERSION = '1.2.1'; /** * The Optimole dashboard API URL. @@ -94,6 +94,7 @@ public static function init(string $key, array $options = []): void $options = array_merge([ 'base_domain' => 'i.optimole.com', 'cache_buster' => '', + 'dashboard_api_key' => '', 'dashboard_api_url' => self::DASHBOARD_API_URL, 'upload_api_url' => self::UPLOAD_API_URL, ], $options); From b0749c88c216f1616c007005b8f932d5461b7a16 Mon Sep 17 00:00:00 2001 From: selul Date: Tue, 6 Aug 2024 15:01:19 +0300 Subject: [PATCH 2/3] Fix offloading logic --- README.md | 1 + src/Offload/Manager.php | 7 +--- src/Optimole.php | 2 +- tests/Unit/Offload/ManagerTest.php | 66 +++++++++++++++--------------- 4 files changed, 36 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 5f73e89..1acb6d5 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ The second argument of the `init` method is optional. It allows you to pass opti * `base_domain`: The base domain to connect to Optimole's API. Default is `i.optimole.com`. * `cache_buster`: A string value that will be appended to the URL of the optimized assets to bust Optimole's cache. * `dashboard_api_url`: The URL of the dashboard API. Default is `https://dashboard.optimole.com/api`. + * `dashboard_api_key`: The API key to use for the dashboard API. * `upload_api_credentials`: An array with the credentials to use for the upload API. The array should contain the keys `userKey` and `secret`. The default is empty and the SDK will use the API key provided in the `init` method to fetch them from the dashboard API. * `upload_api_url`: The URL of the upload API. Default is `https://generateurls-prod.i.optimole.com/upload`. diff --git a/src/Offload/Manager.php b/src/Offload/Manager.php index 7758147..1b424b5 100644 --- a/src/Offload/Manager.php +++ b/src/Offload/Manager.php @@ -31,10 +31,6 @@ class Manager */ private ClientInterface $httpClient; - /** - * The Optimole API key. - */ - private string $key; /** * The manager options. @@ -44,7 +40,7 @@ class Manager /** * Constructor. */ - public function __construct(ClientInterface $httpClient, string $key, array $options = []) + public function __construct(ClientInterface $httpClient, array $options = []) { if (empty($options['dashboard_api_url'])) { throw new InvalidArgumentException('Missing "dashboard_api_url" option'); @@ -53,7 +49,6 @@ public function __construct(ClientInterface $httpClient, string $key, array $opt } $this->httpClient = $httpClient; - $this->key = $key; $this->options = array_merge([ 'upload_api_credentials' => [], ], $options); diff --git a/src/Optimole.php b/src/Optimole.php index 0b082d6..a7665b1 100644 --- a/src/Optimole.php +++ b/src/Optimole.php @@ -131,7 +131,7 @@ private function createImage(string $imageUrl, string $cacheBuster = ''): Image */ private function createOffload(array $options = []): Manager { - return new Manager($this->getHttpClient(), $this->key, array_merge($this->options, $options)); + return new Manager($this->getHttpClient(), array_merge($this->options, $options)); } /** diff --git a/tests/Unit/Offload/ManagerTest.php b/tests/Unit/Offload/ManagerTest.php index d92e5f7..0594568 100644 --- a/tests/Unit/Offload/ManagerTest.php +++ b/tests/Unit/Offload/ManagerTest.php @@ -31,7 +31,7 @@ public function testConstructorWithMissingDashboardApiOption() $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Missing "dashboard_api_url" option'); - new Manager($this->createMock(ClientInterface::class), 'optimole_key', ['upload_api_url' => 'https://upload_api_url']); + new Manager($this->createMock(ClientInterface::class), ['upload_api_url' => 'https://upload_api_url']); } public function testConstructorWithMissingUploadApiOption() @@ -39,7 +39,7 @@ public function testConstructorWithMissingUploadApiOption() $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Missing "upload_api_url" option'); - new Manager($this->createMock(ClientInterface::class), 'optimole_key', ['dashboard_api_url' => 'https://dashboard_api_url']); + new Manager($this->createMock(ClientInterface::class), ['dashboard_api_url' => 'https://dashboard_api_url']); } public function testDeleteImage() @@ -50,11 +50,11 @@ public function testDeleteImage() ->willReturnCallback(function (...$args) { static $expected = [ [ - ['post', 'https://dashboard_api_url/optml/v2/account/details', null, ['Authorization' => 'Bearer optimole_key', 'Content-Type' => 'application/json']], + ['POST', 'https://dashboard_api_url/optml/v2/account/details', null, ['Authorization' => 'Bearer optimole_key', 'Content-Type' => 'application/json']], ['data' => ['cdn_key' => 'cdn_key', 'cdn_secret' => 'cdn_secret']], ], [ - ['post', 'https://upload_api_url', ['userKey' => 'cdn_key', 'secret' => 'cdn_secret', 'id' => 'image_id', 'deleteUrl' => 'true'], ['Content-Type' => 'application/json']], + ['POST', 'https://upload_api_url', ['userKey' => 'cdn_key', 'secret' => 'cdn_secret', 'id' => 'image_id', 'deleteUrl' => 'true'], ['Content-Type' => 'application/json']], ['success'], ], ]; @@ -66,7 +66,7 @@ public function testDeleteImage() return $return; }); - (new Manager($httpClient, 'optimole_key', ['dashboard_api_url' => 'https://dashboard_api_url', 'upload_api_url' => 'https://upload_api_url']))->deleteImage('image_id'); + (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key'=>'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->deleteImage('image_id'); } public function testDeleteImageWithBadResponseException() @@ -77,11 +77,11 @@ public function testDeleteImageWithBadResponseException() ->willReturnCallback(function (...$args) { static $expected = [ [ - ['post', 'https://dashboard_api_url/optml/v2/account/details', null, ['Authorization' => 'Bearer optimole_key', 'Content-Type' => 'application/json']], + ['POST', 'https://dashboard_api_url/optml/v2/account/details', null, ['Authorization' => 'Bearer optimole_key', 'Content-Type' => 'application/json']], ['data' => ['cdn_key' => 'cdn_key', 'cdn_secret' => 'cdn_secret']], ], [ - ['post', 'https://upload_api_url', ['userKey' => 'cdn_key', 'secret' => 'cdn_secret', 'id' => 'image_id', 'deleteUrl' => 'true'], ['Content-Type' => 'application/json']], + ['POST', 'https://upload_api_url', ['userKey' => 'cdn_key', 'secret' => 'cdn_secret', 'id' => 'image_id', 'deleteUrl' => 'true'], ['Content-Type' => 'application/json']], BadResponseException::class, ], ]; @@ -97,7 +97,7 @@ public function testDeleteImageWithBadResponseException() return $return; }); - (new Manager($httpClient, 'optimole_key', ['dashboard_api_url' => 'https://dashboard_api_url', 'upload_api_url' => 'https://upload_api_url']))->deleteImage('image_id'); + (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key'=>'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->deleteImage('image_id'); } public function testDeleteImageWithNonBadResponseException() @@ -110,11 +110,11 @@ public function testDeleteImageWithNonBadResponseException() ->willReturnCallback(function (...$args) { static $expected = [ [ - ['post', 'https://dashboard_api_url/optml/v2/account/details', null, ['Authorization' => 'Bearer optimole_key', 'Content-Type' => 'application/json']], + ['POST', 'https://dashboard_api_url/optml/v2/account/details', null, ['Authorization' => 'Bearer optimole_key', 'Content-Type' => 'application/json']], ['data' => ['cdn_key' => 'cdn_key', 'cdn_secret' => 'cdn_secret']], ], [ - ['post', 'https://upload_api_url', ['userKey' => 'cdn_key', 'secret' => 'cdn_secret', 'id' => 'image_id', 'deleteUrl' => 'true'], ['Content-Type' => 'application/json']], + ['POST', 'https://upload_api_url', ['userKey' => 'cdn_key', 'secret' => 'cdn_secret', 'id' => 'image_id', 'deleteUrl' => 'true'], ['Content-Type' => 'application/json']], \RuntimeException::class, ], ]; @@ -130,7 +130,7 @@ public function testDeleteImageWithNonBadResponseException() return $return; }); - (new Manager($httpClient, 'optimole_key', ['dashboard_api_url' => 'https://dashboard_api_url', 'upload_api_url' => 'https://upload_api_url']))->deleteImage('image_id'); + (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key'=>'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->deleteImage('image_id'); } public function testGetImageUrl() @@ -141,11 +141,11 @@ public function testGetImageUrl() ->willReturnCallback(function (...$args) { static $expected = [ [ - ['post', 'https://dashboard_api_url/optml/v2/account/details', null, ['Authorization' => 'Bearer optimole_key', 'Content-Type' => 'application/json']], + ['POST', 'https://dashboard_api_url/optml/v2/account/details', null, ['Authorization' => 'Bearer optimole_key', 'Content-Type' => 'application/json']], ['data' => ['cdn_key' => 'cdn_key', 'cdn_secret' => 'cdn_secret']], ], [ - ['post', 'https://upload_api_url', ['userKey' => 'cdn_key', 'secret' => 'cdn_secret', 'id' => 'image_id', 'getUrl' => 'true'], ['Content-Type' => 'application/json']], + ['POST', 'https://upload_api_url', ['userKey' => 'cdn_key', 'secret' => 'cdn_secret', 'id' => 'image_id', 'getUrl' => 'true'], ['Content-Type' => 'application/json']], ['getUrl' => 'https://cdn.optimole.com/image_id'], ], ]; @@ -157,7 +157,7 @@ public function testGetImageUrl() return $return; }); - $this->assertSame('https://cdn.optimole.com/image_id', (new Manager($httpClient, 'optimole_key', ['dashboard_api_url' => 'https://dashboard_api_url', 'upload_api_url' => 'https://upload_api_url']))->getImageUrl('image_id')); + $this->assertSame('https://cdn.optimole.com/image_id', (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key'=>'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->getImageUrl('image_id')); } public function testGetImageUrlReturnNullWithBadResponseException() @@ -168,11 +168,11 @@ public function testGetImageUrlReturnNullWithBadResponseException() ->willReturnCallback(function (...$args) { static $expected = [ [ - ['post', 'https://dashboard_api_url/optml/v2/account/details', null, ['Authorization' => 'Bearer optimole_key', 'Content-Type' => 'application/json']], + ['POST', 'https://dashboard_api_url/optml/v2/account/details', null, ['Authorization' => 'Bearer optimole_key', 'Content-Type' => 'application/json']], ['data' => ['cdn_key' => 'cdn_key', 'cdn_secret' => 'cdn_secret']], ], [ - ['post', 'https://upload_api_url', ['userKey' => 'cdn_key', 'secret' => 'cdn_secret', 'id' => 'image_id', 'getUrl' => 'true'], ['Content-Type' => 'application/json']], + ['POST', 'https://upload_api_url', ['userKey' => 'cdn_key', 'secret' => 'cdn_secret', 'id' => 'image_id', 'getUrl' => 'true'], ['Content-Type' => 'application/json']], BadResponseException::class, ], ]; @@ -188,7 +188,7 @@ public function testGetImageUrlReturnNullWithBadResponseException() return $return; }); - $this->assertNull((new Manager($httpClient, 'optimole_key', ['dashboard_api_url' => 'https://dashboard_api_url', 'upload_api_url' => 'https://upload_api_url']))->getImageUrl('image_id')); + $this->assertNull((new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key'=>'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->getImageUrl('image_id')); } public function testGetImageUrlWithMissingGetUrlKey() @@ -202,11 +202,11 @@ public function testGetImageUrlWithMissingGetUrlKey() ->willReturnCallback(function (...$args) { static $expected = [ [ - ['post', 'https://dashboard_api_url/optml/v2/account/details', null, ['Authorization' => 'Bearer optimole_key', 'Content-Type' => 'application/json']], + ['POST', 'https://dashboard_api_url/optml/v2/account/details', null, ['Authorization' => 'Bearer optimole_key', 'Content-Type' => 'application/json']], ['data' => ['cdn_key' => 'cdn_key', 'cdn_secret' => 'cdn_secret']], ], [ - ['post', 'https://upload_api_url', ['userKey' => 'cdn_key', 'secret' => 'cdn_secret', 'id' => 'image_id', 'getUrl' => 'true'], ['Content-Type' => 'application/json']], + ['POST', 'https://upload_api_url', ['userKey' => 'cdn_key', 'secret' => 'cdn_secret', 'id' => 'image_id', 'getUrl' => 'true'], ['Content-Type' => 'application/json']], [], ], ]; @@ -218,7 +218,7 @@ public function testGetImageUrlWithMissingGetUrlKey() return $return; }); - (new Manager($httpClient, 'optimole_key', ['dashboard_api_url' => 'https://dashboard_api_url', 'upload_api_url' => 'https://upload_api_url']))->getImageUrl('image_id'); + (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key'=>'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->getImageUrl('image_id'); } public function testGetUsage() @@ -226,10 +226,10 @@ public function testGetUsage() $httpClient = $this->createMock(ClientInterface::class); $httpClient->expects($this->once()) ->method('sendRequest') - ->with($this->identicalTo('post'), $this->identicalTo('https://dashboard_api_url/optml/v2/account/details'), $this->identicalTo(null), $this->identicalTo(['Authorization' => 'Bearer optimole_key', 'Content-Type' => 'application/json'])) + ->with($this->identicalTo('POST'), $this->identicalTo('https://dashboard_api_url/optml/v2/account/details'), $this->identicalTo(null), $this->identicalTo(['Authorization' => 'Bearer optimole_key', 'Content-Type' => 'application/json'])) ->willReturn(['data' => ['offload_limit' => 5000, 'offloaded_images' => 42]]); - $usage = (new Manager($httpClient, 'optimole_key', ['dashboard_api_url' => 'https://dashboard_api_url', 'upload_api_url' => 'https://upload_api_url']))->getUsage(); + $usage = (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key'=>'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->getUsage(); $this->assertSame(42, $usage->getCurrent()); $this->assertSame(5000, $usage->getLimit()); @@ -243,10 +243,10 @@ public function testGetUsageWithMissingOffloadedImages() $httpClient = $this->createMock(ClientInterface::class); $httpClient->expects($this->once()) ->method('sendRequest') - ->with($this->identicalTo('post'), $this->identicalTo('https://dashboard_api_url/optml/v2/account/details'), $this->identicalTo(null), $this->identicalTo(['Authorization' => 'Bearer optimole_key', 'Content-Type' => 'application/json'])) + ->with($this->identicalTo('POST'), $this->identicalTo('https://dashboard_api_url/optml/v2/account/details'), $this->identicalTo(null), $this->identicalTo(['Authorization' => 'Bearer optimole_key', 'Content-Type' => 'application/json'])) ->willReturn(['data' => ['offload_limit' => 5000]]); - (new Manager($httpClient, 'optimole_key', ['dashboard_api_url' => 'https://dashboard_api_url', 'upload_api_url' => 'https://upload_api_url']))->getUsage(); + (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key'=>'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->getUsage(); } public function testGetUsageWithMissingOffloadLimit() @@ -257,10 +257,10 @@ public function testGetUsageWithMissingOffloadLimit() $httpClient = $this->createMock(ClientInterface::class); $httpClient->expects($this->once()) ->method('sendRequest') - ->with($this->identicalTo('post'), $this->identicalTo('https://dashboard_api_url/optml/v2/account/details'), $this->identicalTo(null), $this->identicalTo(['Authorization' => 'Bearer optimole_key', 'Content-Type' => 'application/json'])) + ->with($this->identicalTo('POST'), $this->identicalTo('https://dashboard_api_url/optml/v2/account/details'), $this->identicalTo(null), $this->identicalTo(['Authorization' => 'Bearer optimole_key', 'Content-Type' => 'application/json'])) ->willReturn(['data' => ['offloaded_images' => 42]]); - (new Manager($httpClient, 'optimole_key', ['dashboard_api_url' => 'https://dashboard_api_url', 'upload_api_url' => 'https://upload_api_url']))->getUsage(); + (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key'=>'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->getUsage(); } public function testUpdateImageMetadata() @@ -271,11 +271,11 @@ public function testUpdateImageMetadata() ->willReturnCallback(function (...$args) { static $expected = [ [ - ['post', 'https://dashboard_api_url/optml/v2/account/details', null, ['Authorization' => 'Bearer optimole_key', 'Content-Type' => 'application/json']], + ['POST', 'https://dashboard_api_url/optml/v2/account/details', null, ['Authorization' => 'Bearer optimole_key', 'Content-Type' => 'application/json']], ['data' => ['cdn_key' => 'cdn_key', 'cdn_secret' => 'cdn_secret']], ], [ - ['post', 'https://upload_api_url', ['userKey' => 'cdn_key', 'secret' => 'cdn_secret', 'id' => 'image_id', 'originalFileSize' => 42, 'height' => 100, 'width' => 200, 'updateDynamo' => 'success'], ['Content-Type' => 'application/json']], + ['POST', 'https://upload_api_url', ['userKey' => 'cdn_key', 'secret' => 'cdn_secret', 'id' => 'image_id', 'originalFileSize' => 42, 'height' => 100, 'width' => 200, 'originalUrl'=>'originurl', 'updateDynamo' => 'success'], ['Content-Type' => 'application/json']], ['success'], ], ]; @@ -287,7 +287,7 @@ public function testUpdateImageMetadata() return $return; }); - (new Manager($httpClient, 'optimole_key', ['dashboard_api_url' => 'https://dashboard_api_url', 'upload_api_url' => 'https://upload_api_url']))->updateImageMetadata('image_id', 42, 100, 200); + (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key'=>'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->updateImageMetadata('image_id', 42, 100, 200,'originurl'); } public function testUpdateImageMetadataWithInvalidImageHeight() @@ -295,7 +295,7 @@ public function testUpdateImageMetadataWithInvalidImageHeight() $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Image height must be "auto" or an integer.'); - (new Manager($this->createMock(ClientInterface::class), 'optimole_key', ['dashboard_api_url' => 'https://dashboard_api_url', 'upload_api_url' => 'https://upload_api_url']))->updateImageMetadata('image_id', 42, '', 200); + (new Manager($this->createMock(ClientInterface::class), ['dashboard_api_url' => 'https://dashboard_api_url', 'upload_api_url' => 'https://upload_api_url']))->updateImageMetadata('image_id', 42, '', 200); } public function testUpdateImageMetadataWithInvalidImageWidth() @@ -303,7 +303,7 @@ public function testUpdateImageMetadataWithInvalidImageWidth() $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Image width must be "auto" or an integer.'); - (new Manager($this->createMock(ClientInterface::class), 'optimole_key', ['dashboard_api_url' => 'https://dashboard_api_url', 'upload_api_url' => 'https://upload_api_url']))->updateImageMetadata('image_id', 42, 100, ''); + (new Manager($this->createMock(ClientInterface::class), ['dashboard_api_url' => 'https://dashboard_api_url', 'upload_api_url' => 'https://upload_api_url']))->updateImageMetadata('image_id', 42, 100, ''); } public function testUploadImageWhenFileDoesntExist() @@ -313,7 +313,7 @@ public function testUploadImageWhenFileDoesntExist() $httpClient = $this->createMock(ClientInterface::class); - (new Manager($httpClient, 'optimole_key', ['dashboard_api_url' => 'https://dashboard_api_url', 'upload_api_url' => 'https://upload_api_url']))->uploadImage('non_existent_file', 'image_url'); + (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'upload_api_url' => 'https://upload_api_url']))->uploadImage('non_existent_file', 'image_url'); } /** @@ -331,6 +331,6 @@ public function testUploadImageWhenFileIsntReadable() ->with($this->identicalTo('non_readable_file')) ->willReturn(true); - (new Manager($httpClient, 'optimole_key', ['dashboard_api_url' => 'https://dashboard_api_url', 'upload_api_url' => 'https://upload_api_url']))->uploadImage('non_readable_file', 'image_url'); + (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'upload_api_url' => 'https://upload_api_url']))->uploadImage('non_readable_file', 'image_url'); } } From 78bed51231e285d21881b95b38af80336c533fe3 Mon Sep 17 00:00:00 2001 From: selul Date: Tue, 6 Aug 2024 15:30:27 +0300 Subject: [PATCH 3/3] Fix offloading logic --- src/Offload/Manager.php | 1 - tests/Unit/Offload/ManagerTest.php | 22 +++++++++++----------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Offload/Manager.php b/src/Offload/Manager.php index 1b424b5..e6b3043 100644 --- a/src/Offload/Manager.php +++ b/src/Offload/Manager.php @@ -31,7 +31,6 @@ class Manager */ private ClientInterface $httpClient; - /** * The manager options. */ diff --git a/tests/Unit/Offload/ManagerTest.php b/tests/Unit/Offload/ManagerTest.php index 0594568..44b4334 100644 --- a/tests/Unit/Offload/ManagerTest.php +++ b/tests/Unit/Offload/ManagerTest.php @@ -66,7 +66,7 @@ public function testDeleteImage() return $return; }); - (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key'=>'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->deleteImage('image_id'); + (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key' => 'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->deleteImage('image_id'); } public function testDeleteImageWithBadResponseException() @@ -97,7 +97,7 @@ public function testDeleteImageWithBadResponseException() return $return; }); - (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key'=>'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->deleteImage('image_id'); + (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key' => 'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->deleteImage('image_id'); } public function testDeleteImageWithNonBadResponseException() @@ -130,7 +130,7 @@ public function testDeleteImageWithNonBadResponseException() return $return; }); - (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key'=>'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->deleteImage('image_id'); + (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key' => 'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->deleteImage('image_id'); } public function testGetImageUrl() @@ -157,7 +157,7 @@ public function testGetImageUrl() return $return; }); - $this->assertSame('https://cdn.optimole.com/image_id', (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key'=>'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->getImageUrl('image_id')); + $this->assertSame('https://cdn.optimole.com/image_id', (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key' => 'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->getImageUrl('image_id')); } public function testGetImageUrlReturnNullWithBadResponseException() @@ -188,7 +188,7 @@ public function testGetImageUrlReturnNullWithBadResponseException() return $return; }); - $this->assertNull((new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key'=>'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->getImageUrl('image_id')); + $this->assertNull((new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key' => 'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->getImageUrl('image_id')); } public function testGetImageUrlWithMissingGetUrlKey() @@ -218,7 +218,7 @@ public function testGetImageUrlWithMissingGetUrlKey() return $return; }); - (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key'=>'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->getImageUrl('image_id'); + (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key' => 'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->getImageUrl('image_id'); } public function testGetUsage() @@ -229,7 +229,7 @@ public function testGetUsage() ->with($this->identicalTo('POST'), $this->identicalTo('https://dashboard_api_url/optml/v2/account/details'), $this->identicalTo(null), $this->identicalTo(['Authorization' => 'Bearer optimole_key', 'Content-Type' => 'application/json'])) ->willReturn(['data' => ['offload_limit' => 5000, 'offloaded_images' => 42]]); - $usage = (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key'=>'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->getUsage(); + $usage = (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key' => 'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->getUsage(); $this->assertSame(42, $usage->getCurrent()); $this->assertSame(5000, $usage->getLimit()); @@ -246,7 +246,7 @@ public function testGetUsageWithMissingOffloadedImages() ->with($this->identicalTo('POST'), $this->identicalTo('https://dashboard_api_url/optml/v2/account/details'), $this->identicalTo(null), $this->identicalTo(['Authorization' => 'Bearer optimole_key', 'Content-Type' => 'application/json'])) ->willReturn(['data' => ['offload_limit' => 5000]]); - (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key'=>'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->getUsage(); + (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key' => 'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->getUsage(); } public function testGetUsageWithMissingOffloadLimit() @@ -260,7 +260,7 @@ public function testGetUsageWithMissingOffloadLimit() ->with($this->identicalTo('POST'), $this->identicalTo('https://dashboard_api_url/optml/v2/account/details'), $this->identicalTo(null), $this->identicalTo(['Authorization' => 'Bearer optimole_key', 'Content-Type' => 'application/json'])) ->willReturn(['data' => ['offloaded_images' => 42]]); - (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key'=>'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->getUsage(); + (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key' => 'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->getUsage(); } public function testUpdateImageMetadata() @@ -275,7 +275,7 @@ public function testUpdateImageMetadata() ['data' => ['cdn_key' => 'cdn_key', 'cdn_secret' => 'cdn_secret']], ], [ - ['POST', 'https://upload_api_url', ['userKey' => 'cdn_key', 'secret' => 'cdn_secret', 'id' => 'image_id', 'originalFileSize' => 42, 'height' => 100, 'width' => 200, 'originalUrl'=>'originurl', 'updateDynamo' => 'success'], ['Content-Type' => 'application/json']], + ['POST', 'https://upload_api_url', ['userKey' => 'cdn_key', 'secret' => 'cdn_secret', 'id' => 'image_id', 'originalFileSize' => 42, 'height' => 100, 'width' => 200, 'originalUrl' => 'originurl', 'updateDynamo' => 'success'], ['Content-Type' => 'application/json']], ['success'], ], ]; @@ -287,7 +287,7 @@ public function testUpdateImageMetadata() return $return; }); - (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key'=>'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->updateImageMetadata('image_id', 42, 100, 200,'originurl'); + (new Manager($httpClient, ['dashboard_api_url' => 'https://dashboard_api_url', 'dashboard_api_key' => 'optimole_key', 'upload_api_url' => 'https://upload_api_url']))->updateImageMetadata('image_id', 42, 100, 200, 'originurl'); } public function testUpdateImageMetadataWithInvalidImageHeight()