Skip to content

Commit 9ed1d4f

Browse files
committed
updated sdk files, test cases and readme
1 parent bc3bc28 commit 9ed1d4f

File tree

11 files changed

+1223
-262
lines changed

11 files changed

+1223
-262
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ ImageKit is a complete image optimization and transformation solution that comes
2121
* [File Upload](#file-upload)
2222
- [Demo Application](#demo-application)
2323
- [URL Generation](#url-generation-1)
24-
- [Signed URL & Image Transformations](#applying-chained-transformations-common-image-manipulations-signed-url--conditional-transformation)
24+
- [Signed URL & Image Transformations](#applying-chained-transformations-common-image-manipulations--signed-url)
2525
- [Server-side File Upload](#server-side-file-upload)
2626
- [File Management](#file-management)
2727
- [Custom Metadata Fields API](#custom-metadata-fields-api)

sample/file_management/index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"fileType" => "all",
2929
"limit" => 10,
3030
"skip" => 0,
31+
"tags" => implode(",",["tag3","tag4"]),
3132
]);
3233

3334
echo "\n\n";

src/ImageKit/Constants/ErrorMessages.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class ErrorMessages
7777
'For support kindly contact us at [email protected] .'];
7878
public static $COPY_FILE_PARAMETER_NON_ARRAY = ['message' => 'Copy File API accepts an array of parameters, non array value passed', 'help' => 'For support kindly contact us at [email protected] .'];
7979
public static $COPY_FILE_PARAMETER_EMPTY_ARRAY = ['message' => 'Copy File API accepts an array of parameters, empty array passed', 'help' => 'For support kindly contact us at [email protected] .'];
80-
public static $COPY_FILE_DATA_INVALID = ['message' => 'Missing parameter sourceFilePath and/or destinationPath and/or includeVersions for Copy File API', 'help' =>
80+
public static $COPY_FILE_DATA_INVALID = ['message' => 'Missing parameter sourceFilePath and/or destinationPath for Copy File API', 'help' =>
8181
'For support kindly contact us at [email protected] .'];
8282
public static $MOVE_FILE_PARAMETER_MISSING = ['message' => 'Move File API accepts an array, null passed', 'help' =>
8383
'For support kindly contact us at [email protected] .'];
@@ -109,7 +109,7 @@ class ErrorMessages
109109
'For support kindly contact us at [email protected] .'];
110110
public static $COPY_FOLDER_PARAMETER_NON_ARRAY = ['message' => 'Copy Folder API accepts an array of parameters, non array value passed', 'help' => 'For support kindly contact us at [email protected] .'];
111111
public static $COPY_FOLDER_PARAMETER_EMPTY_ARRAY = ['message' => 'Copy Folder API accepts an array of parameters, empty array passed', 'help' => 'For support kindly contact us at [email protected] .'];
112-
public static $COPY_FOLDER_DATA_INVALID = ['message' => 'Missing parameter sourceFolderPath and/or destinationPath and/or includeVersions for Copy Folder API', 'help' =>
112+
public static $COPY_FOLDER_DATA_INVALID = ['message' => 'Missing parameter sourceFolderPath and/or destinationPath for Copy Folder API', 'help' =>
113113
'For support kindly contact us at [email protected] .'];
114114
public static $MOVE_FOLDER_PARAMETER_MISSING = ['message' => 'Move Folder API accepts an array, null passed', 'help' =>
115115
'For support kindly contact us at [email protected] .'];

src/ImageKit/ImageKit.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -701,12 +701,12 @@ public function copy($parameter=null)
701701
if(sizeof($parameter)==0){
702702
return Response::respond(true, ((object)ErrorMessages::$COPY_FILE_PARAMETER_EMPTY_ARRAY));
703703
}
704-
if (empty($parameter['sourceFilePath']) || empty($parameter['destinationPath']) || !isset($parameter['includeFileVersions'])) {
704+
if (empty($parameter['sourceFilePath']) || empty($parameter['destinationPath'])) {
705705
return Response::respond(true, ((object)ErrorMessages::$COPY_FILE_DATA_INVALID));
706706
}
707707

708708
$this->httpClient->setUri(Endpoints::getCopyFileEndpoint());
709-
return Manage\File::copy($parameter['sourceFilePath'], $parameter['destinationPath'], $parameter['includeFileVersions'], $this->httpClient);
709+
return Manage\File::copy($parameter['sourceFilePath'], $parameter['destinationPath'], $parameter['includeFileVersions']??false, $this->httpClient);
710710
}
711711

712712

@@ -942,12 +942,12 @@ public function copyFolder($parameter=null)
942942
if(sizeof($parameter)==0){
943943
return Response::respond(true, ((object)ErrorMessages::$COPY_FOLDER_PARAMETER_EMPTY_ARRAY));
944944
}
945-
if (empty($parameter['sourceFolderPath']) || empty($parameter['destinationPath']) || !isset($parameter['includeFileVersions'])) {
945+
if (empty($parameter['sourceFolderPath']) || empty($parameter['destinationPath'])) {
946946
return Response::respond(true, ((object)ErrorMessages::$COPY_FOLDER_DATA_INVALID));
947947
}
948948

949949
$this->httpClient->setUri(Endpoints::getCopyFolderEndpoint());
950-
return Manage\Folder::copy($parameter['sourceFolderPath'], $parameter['destinationPath'], $parameter['includeFileVersions'], $this->httpClient);
950+
return Manage\Folder::copy($parameter['sourceFolderPath'], $parameter['destinationPath'], $parameter['includeFileVersions']??false, $this->httpClient);
951951
}
952952

953953
/**

src/ImageKit/Manage/File.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class File
2323
public static function listFile(GuzzleHttpWrapper $resource,$parameters=null)
2424
{
2525
if($parameters){
26+
if (isset($parameters['tags']) && is_array($parameters['tags'])) {
27+
$parameters['tags'] = implode(',', $parameters['tags']);
28+
}
2629
$resource->setDatas($parameters);
2730
}
2831
try {
@@ -273,9 +276,9 @@ public static function bulkDeleteByFileIds($fileIds, GuzzleHttpWrapper $resource
273276
*
274277
* @return Response
275278
*/
276-
public static function copy($sourceFilePath, $destinationPath, $includeVersions, GuzzleHttpWrapper $resource)
279+
public static function copy($sourceFilePath, $destinationPath, $includeFileVersions, GuzzleHttpWrapper $resource)
277280
{
278-
$resource->setDatas(['sourceFilePath' => $sourceFilePath, 'destinationPath' => $destinationPath, 'includeVersions' => $includeVersions]);
281+
$resource->setDatas(['sourceFilePath' => $sourceFilePath, 'destinationPath' => $destinationPath, 'includeFileVersions' => $includeFileVersions]);
279282
try {
280283
$res = $resource->post();
281284
} catch (\Throwable $th) {
@@ -539,12 +542,12 @@ public static function updateDetails($fileId, $updateData, GuzzleHttpWrapper $re
539542
{
540543
$obj = (object)$updateData;
541544

542-
if (isset($obj->tags) && ($obj->tags !== null) && ($obj->tags !== 'undefined') && !is_array($obj->tags)) {
543-
return Response::respond(true, ((object)ErrorMessages::$UPDATE_DATA_TAGS_INVALID));
545+
if (isset($obj->tags) && is_string($obj->tags)) {
546+
$updateData['tags'] = explode(',', $obj->tags);
544547
}
545548

546-
if (isset($obj->customCoordinates) && ($obj->customCoordinates !== 'undefined') && is_array($obj->customCoordinates)) {
547-
return Response::respond(true, ((object)ErrorMessages::$UPDATE_DATA_COORDS_INVALID));
549+
if (isset($obj->customCoordinates) && is_array($obj->customCoordinates)) {
550+
$updateData['customCoordinates'] = implode(',', $obj->customCoordinates);
548551
}
549552

550553
$resource->setDatas($updateData);

src/ImageKit/Manage/Folder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ public static function delete($folderPath, GuzzleHttpWrapper $httpClient)
9090
*
9191
* @return Response
9292
*/
93-
public static function copy($sourceFolderPath, $destinationPath, $includeVersions, GuzzleHttpWrapper $httpClient)
93+
public static function copy($sourceFolderPath, $destinationPath, $includeFileVersions, GuzzleHttpWrapper $httpClient)
9494
{
95-
$httpClient->setDatas(['sourceFolderPath' => $sourceFolderPath, 'destinationPath' => $destinationPath, 'includeVersions' => $includeVersions]);
95+
$httpClient->setDatas(['sourceFolderPath' => $sourceFolderPath, 'destinationPath' => $destinationPath, 'includeFileVersions' => $includeFileVersions]);
9696
try {
9797
$res = $httpClient->post();
9898
} catch (\Throwable $th) {

tests/ImageKit/Manage/CacheTest.php

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
use GuzzleHttp\Psr7\Response;
66
use GuzzleHttp\Psr7\Utils;
77
use ImageKit\ImageKit;
8+
use ImageKit\Utils\Transformation;
89
use ImageKit\Manage\Cache;
910
use ImageKit\Resource\GuzzleHttpWrapper;
11+
use GuzzleHttp\Handler\MockHandler;
1012
use PHPUnit\Framework\TestCase;
13+
use GuzzleHttp\Client;
14+
use GuzzleHttp\HandlerStack;
15+
use GuzzleHttp\Middleware;
1116

1217
class CacheTest extends TestCase
1318
{
@@ -28,16 +33,29 @@ public function testPurgeCache()
2833
];
2934
$mockBodyResponse = Utils::streamFor(json_encode($responseBody));
3035

31-
$this->stubHttpClient('post', new Response(201, ['X-Foo' => 'Bar'], $mockBodyResponse));
36+
$mock = new MockHandler([
37+
new Response(200, ['X-Foo' => 'Bar'], $mockBodyResponse)
38+
]);
3239

33-
$response = $this->client->purgeCache($image_url);
40+
$handlerStack = HandlerStack::create($mock);
41+
42+
$container = [];
43+
$history = Middleware::history($container);
44+
45+
$handlerStack->push($history);
3446

47+
$this->createMockClient($handlerStack);
48+
49+
$response = $this->mockClient->purgeCache($image_url);
50+
51+
$request = $container[0]['request'];
52+
$requestPath = $request->getUri()->getPath();
53+
$stream = Utils::streamFor($request->getBody())->getContents();
54+
$stream = json_decode($stream,true);
55+
3556
// Request Check
36-
CacheTest::assertNotNull($image_url);
37-
if (!filter_var($image_url, FILTER_VALIDATE_URL)) {
38-
CacheTest::assertFalse('Invalid URL');
39-
}
40-
CacheTest::assertIsString($image_url);
57+
CacheTest::assertEquals("/v1/files/purge",$requestPath);
58+
CacheTest::assertEquals($stream['url'],$image_url);
4159

4260
// Response Check
4361
CacheTest::assertEquals(json_encode($responseBody), json_encode($response->result));
@@ -80,13 +98,30 @@ public function testPurgeCacheStatus()
8098

8199
$mockBodyResponse = Utils::streamFor(json_encode($responseBody));
82100

83-
$this->stubHttpClient('get', new Response(201, ['X-Foo' => 'Bar'], $mockBodyResponse));
101+
$mock = new MockHandler([
102+
new Response(200, ['X-Foo' => 'Bar'], $mockBodyResponse)
103+
]);
84104

85-
$response = $this->client->purgeCacheStatus($cacheRequestId);
105+
$handlerStack = HandlerStack::create($mock);
106+
107+
$container = [];
108+
$history = Middleware::history($container);
109+
110+
$handlerStack->push($history);
111+
112+
$this->createMockClient($handlerStack);
113+
114+
$response = $this->mockClient->purgeCacheStatus($cacheRequestId);
86115

116+
$request = $container[0]['request'];
117+
$requestPath = $request->getUri()->getPath();
118+
$stream = Utils::streamFor($request->getBody())->getContents();
119+
$stream = json_decode($stream,true);
120+
87121
// Request Check
88-
CacheTest::assertNotNull($cacheRequestId);
89-
CacheTest::assertIsString($cacheRequestId);
122+
CacheTest::assertEquals("/v1/files/purge/{$cacheRequestId}",$requestPath);
123+
CacheTest::assertEmpty($stream);
124+
90125

91126
// Response Check
92127
CacheTest::assertEquals(json_encode($responseBody), json_encode($response->result));
@@ -129,6 +164,20 @@ protected function setUp(): void
129164

130165
}
131166

167+
/**
168+
*
169+
*/
170+
private function createMockClient($handler){
171+
$this->mockClient = new ImageKit(
172+
'testing_public_key',
173+
'testing_private_key',
174+
'https://ik.imagekit.io/demo',
175+
Transformation::DEFAULT_TRANSFORMATION_POSITION,
176+
$handler
177+
);
178+
}
179+
180+
132181
protected function tearDown(): void
133182
{
134183
$this->client = null;

0 commit comments

Comments
 (0)