diff --git a/src/Api/Admin/AssetsTrait.php b/src/Api/Admin/AssetsTrait.php index 08cf8a17..1c4539b0 100644 --- a/src/Api/Admin/AssetsTrait.php +++ b/src/Api/Admin/AssetsTrait.php @@ -237,6 +237,8 @@ public function assetsByAssetFolder($assetFolder, $options = []) * * @return ApiResponse * + * @throws ApiError + * * @see https://cloudinary.com/documentation/admin_api#visual_search_for_resources */ public function visualSearch($options = []) @@ -245,7 +247,15 @@ public function visualSearch($options = []) $params = ArrayUtils::whitelist($options, ['image_url', 'image_asset_id', 'text']); - return $this->apiClient->get($uri, $params); + // Special handling for file inside Admin API. + if (array_key_exists('image_file', $options)) { + $options['file_field'] = 'image_file'; + $options['unsigned'] = true; + + return $this->apiClient->postFile($uri, $options['image_file'], $params, $options); + } + + return $this->apiClient->postForm($uri, $params); } /** diff --git a/src/Api/ApiClient.php b/src/Api/ApiClient.php index fc2b24b7..35d3bb52 100644 --- a/src/Api/ApiClient.php +++ b/src/Api/ApiClient.php @@ -380,7 +380,7 @@ private function postLargeFileAsync($endPoint, $fileHandle, $parameters, $option protected function postSingleChunkAsync($endPoint, $singleChunk, $parameters, $options = []) { $filePart = [ - 'name' => 'file', + 'name' => ArrayUtils::get($options, 'file_field', 'file'), 'contents' => $singleChunk, ]; diff --git a/tests/Unit/Admin/AssetsTest.php b/tests/Unit/Admin/AssetsTest.php index c8801d25..f004bbb9 100644 --- a/tests/Unit/Admin/AssetsTest.php +++ b/tests/Unit/Admin/AssetsTest.php @@ -10,8 +10,11 @@ namespace Cloudinary\Test\Unit\Admin; +use Cloudinary\Api\Exception\ApiError; use Cloudinary\Test\Helpers\MockAdminApi; use Cloudinary\Test\Helpers\RequestAssertionsTrait; +use Cloudinary\Test\Integration\IntegrationTestCase; +use Cloudinary\Test\Unit\Asset\AssetTestCase; use Cloudinary\Test\Unit\UnitTestCase; /** @@ -21,6 +24,9 @@ final class AssetsTest extends UnitTestCase { use RequestAssertionsTrait; + /** + * @return array[] + */ public function restoreDeletedAssetSpecificVersionDataProvider() { return [ @@ -185,4 +191,80 @@ public function testRelatedAssetsByAssetIds() ] ); } + + + /** + * @return array[] + */ + public function visualSearchDataProvider() + { + return [ + [ + 'options' => [ + 'image_url' => AssetTestCase::FETCH_IMAGE_URL, + ], + 'url' => '/resources/visual_search', + 'bodyFields' => [ + 'image_url' => AssetTestCase::FETCH_IMAGE_URL, + ], + ], + [ + 'options' => [ + 'image_asset_id' => self::API_TEST_ASSET_ID, + ], + 'url' => '/resources/visual_search', + 'bodyFields' => [ + 'image_asset_id' => self::API_TEST_ASSET_ID, + ], + ], + [ + 'options' => [ + 'text' => 'sample image', + ], + 'url' => '/resources/visual_search', + 'bodyFields' => [ + 'text' => 'sample image', + ], + ], + ]; + } + + /** + * Test Visual search. + * + * @dataProvider visualSearchDataProvider + * + * @param array $options + * @param string $url + * @param array $bodyFields + * + * @throws ApiError + */ + public function testVisualSearch($options, $url, $bodyFields) + { + $mockAdminApi = new MockAdminApi(); + $mockAdminApi->visualSearch($options); + $lastRequest = $mockAdminApi->getMockHandler()->getLastRequest(); + + self::assertRequestUrl($lastRequest, $url); + self::assertRequestBodySubset($lastRequest, $bodyFields); + } + + /** + * Test Visual search using image file. + * + * @throws ApiError + */ + public function testVisualSearchImageFile() + { + $mockAdminApi = new MockAdminApi(); + $mockAdminApi->visualSearch(['image_file' => IntegrationTestCase::TEST_IMAGE_PATH]); + $lastRequest = $mockAdminApi->getMockHandler()->getLastRequest(); + + self::assertRequestUrl($lastRequest, '/resources/visual_search'); + $body = $lastRequest->getBody()->getContents(); + + self::assertStringContainsString('image_file', $body); + self::assertStringContainsString('Content-Length: 5110', $body); + } }