99use GuzzleHttp \Psr7 \Request ;
1010use GuzzleHttp \Psr7 \Utils ;
1111use ImageKit \ImageKit ;
12+ use ImageKit \Utils \Transformation ;
1213use ImageKit \Resource \GuzzleHttpWrapper ;
1314use GuzzleHttp \Handler \MockHandler ;
1415use PHPUnit \Framework \TestCase ;
@@ -29,7 +30,9 @@ final class UploadTest extends TestCase
2930 /**
3031 * @var ImageKit
3132 */
33+
3234 private $ client ;
35+ private $ mockClient ;
3336
3437 private $ uploadSuccessResponseObj = [
3538 "fileId " => "598821f949c0a938d57563bd " ,
@@ -153,6 +156,7 @@ public function testFileUploadIfSuccessful()
153156
154157 $ mockBodyResponse = Utils::streamFor (json_encode ($ this ->uploadSuccessResponseObj ));
155158
159+
156160 $ mock = new MockHandler ([
157161 new Response (200 , ['X-Foo ' => 'Bar ' ], $ mockBodyResponse )
158162 ]);
@@ -164,13 +168,16 @@ public function testFileUploadIfSuccessful()
164168
165169 $ handlerStack ->push ($ history );
166170
167- $ client = new Client (['handler ' => $ handlerStack ]);
168-
169- $ requestMultiPart = GuzzleHttpWrapper::getMultipartData ($ fileOptions );
170- $ client ->request ('POST ' , Endpoints::getUploadFileEndpoint (), [
171- 'multipart ' =>$ requestMultiPart
172- ]);
173-
171+ $ mockClient = new ImageKit (
172+ 'testing_public_key ' ,
173+ 'testing_private_key ' ,
174+ 'https://ik.imagekit.io/demo ' ,
175+ Transformation::DEFAULT_TRANSFORMATION_POSITION ,
176+ $ handlerStack
177+ );
178+
179+ $ response = $ mockClient ->uploadFile ($ fileOptions );
180+
174181 $ requestBody = $ container [0 ]['request ' ]->getBody ();
175182 $ requestHeaders = $ container [0 ]['request ' ]->getHeaders ();
176183 $ boundary = str_replace ("multipart/form-data; boundary= " ,"" ,$ requestHeaders ["Content-Type " ][0 ]);
@@ -187,17 +194,12 @@ public function testFileUploadIfSuccessful()
187194 $ this ->checkFormData ($ stream ,$ boundary ,"isPrivateFile " ,"true " );
188195 $ this ->checkFormData ($ stream ,$ boundary ,"useUniqueFileName " ,"false " );
189196 $ this ->checkFormData ($ stream ,$ boundary ,"responseFields " ,implode (", " , ["tags " , "customMetadata " ]));
190- $ this ->checkFormData ($ stream ,$ boundary ,"extensions[0][name] " , " remove-bg " );
197+ $ this ->checkFormData ($ stream ,$ boundary ,"extensions " , json_encode ( $ fileOptions [ ' extensions ' ]) );
191198 $ this ->checkFormData ($ stream ,$ boundary ,"webhookUrl " ,"https://example.com/webhook " );
192199 $ this ->checkFormData ($ stream ,$ boundary ,"overwriteFile " ,"true " );
193200 $ this ->checkFormData ($ stream ,$ boundary ,"overwriteAITags " ,"false " );
194201 $ this ->checkFormData ($ stream ,$ boundary ,"overwriteCustomMetadata " ,"true " );
195- $ this ->checkFormData ($ stream ,$ boundary ,"customMetadata[SKU] " ,"VS882HJ2JD " );
196- $ this ->checkFormData ($ stream ,$ boundary ,"customMetadata[price] " ,"599.99 " );
197-
198- $ this ->stubHttpClient (new Response (200 , ['X-Foo ' => 'Bar ' ], $ mockBodyResponse ));
199-
200- $ response = $ this ->client ->uploadFile ($ fileOptions );
202+ $ this ->checkFormData ($ stream ,$ boundary ,"customMetadata " ,json_encode ($ fileOptions ['customMetadata ' ]));
201203
202204 // Response Check
203205 UploadTest::assertEquals (json_encode ($ this ->uploadSuccessResponseObj ), json_encode ($ response ->result ));
@@ -227,13 +229,16 @@ public function testFileUploadMissingUseUniqueFileName()
227229
228230 $ handlerStack ->push ($ history );
229231
230- $ client = new Client (['handler ' => $ handlerStack ]);
231-
232- $ requestMultiPart = GuzzleHttpWrapper::getMultipartData ($ fileOptions );
233- $ client ->request ('POST ' , Endpoints::getUploadFileEndpoint (), [
234- 'multipart ' =>$ requestMultiPart
235- ]);
236-
232+ $ mockClient = new ImageKit (
233+ 'testing_public_key ' ,
234+ 'testing_private_key ' ,
235+ 'https://ik.imagekit.io/demo ' ,
236+ Transformation::DEFAULT_TRANSFORMATION_POSITION ,
237+ $ handlerStack
238+ );
239+
240+ $ response = $ mockClient ->uploadFile ($ fileOptions );
241+
237242 $ requestBody = $ container [0 ]['request ' ]->getBody ();
238243 $ requestHeaders = $ container [0 ]['request ' ]->getHeaders ();
239244 $ boundary = str_replace ("multipart/form-data; boundary= " ,"" ,$ requestHeaders ["Content-Type " ][0 ]);
@@ -248,12 +253,6 @@ public function testFileUploadMissingUseUniqueFileName()
248253 $ this ->checkFormData ($ stream ,$ boundary ,"fileName " ,$ fileOptions ['fileName ' ]);
249254 $ this ->checkFormData ($ stream ,$ boundary ,"isPrivateFile " ,"true " );
250255 UploadTest::assertStringNotContainsString ("useUniqueFileName " ,$ stream );
251-
252- $ mockBodyResponse = Utils::streamFor (json_encode ($ fileOptions ));
253-
254- $ this ->stubHttpClient (new Response (200 , ['X-Foo ' => 'Bar ' ], $ mockBodyResponse ));
255-
256- $ response = $ this ->client ->uploadFile ($ fileOptions );
257256 }
258257
259258 /**
@@ -280,12 +279,15 @@ public function testFileUploadMissingIsPrivateFileUseUniqueFileName()
280279
281280 $ handlerStack ->push ($ history );
282281
283- $ client = new Client (['handler ' => $ handlerStack ]);
284-
285- $ requestMultiPart = GuzzleHttpWrapper::getMultipartData ($ fileOptions );
286- $ client ->request ('POST ' , Endpoints::getUploadFileEndpoint (), [
287- 'multipart ' =>$ requestMultiPart
288- ]);
282+ $ mockClient = new ImageKit (
283+ 'testing_public_key ' ,
284+ 'testing_private_key ' ,
285+ 'https://ik.imagekit.io/demo ' ,
286+ Transformation::DEFAULT_TRANSFORMATION_POSITION ,
287+ $ handlerStack
288+ );
289+
290+ $ response = $ mockClient ->uploadFile ($ fileOptions );
289291
290292 $ requestBody = $ container [0 ]['request ' ]->getBody ();
291293 $ requestHeaders = $ container [0 ]['request ' ]->getHeaders ();
@@ -302,10 +304,6 @@ public function testFileUploadMissingIsPrivateFileUseUniqueFileName()
302304 $ this ->checkFormData ($ stream ,$ boundary ,"tags " ,"abd,def " );
303305 UploadTest::assertStringNotContainsString ("isPrivateFile " ,$ stream );
304306 UploadTest::assertStringNotContainsString ("useUniqueFileName " ,$ stream );
305-
306- $ this ->stubHttpClient (new Response (200 , ['X-Foo ' => 'Bar ' ], $ mockBodyResponse ));
307-
308- $ response = $ this ->client ->uploadFile ($ fileOptions );
309307 }
310308
311309 /**
@@ -331,12 +329,15 @@ public function testFileUploadBareMinimumRequest()
331329
332330 $ handlerStack ->push ($ history );
333331
334- $ client = new Client (['handler ' => $ handlerStack ]);
335-
336- $ requestMultiPart = GuzzleHttpWrapper::getMultipartData ($ fileOptions );
337- $ client ->request ('POST ' , Endpoints::getUploadFileEndpoint (), [
338- 'multipart ' =>$ requestMultiPart
339- ]);
332+ $ mockClient = new ImageKit (
333+ 'testing_public_key ' ,
334+ 'testing_private_key ' ,
335+ 'https://ik.imagekit.io/demo ' ,
336+ Transformation::DEFAULT_TRANSFORMATION_POSITION ,
337+ $ handlerStack
338+ );
339+
340+ $ response = $ mockClient ->uploadFile ($ fileOptions );
340341
341342 $ requestBody = $ container [0 ]['request ' ]->getBody ();
342343 $ requestHeaders = $ container [0 ]['request ' ]->getHeaders ();
@@ -355,10 +356,6 @@ public function testFileUploadBareMinimumRequest()
355356 UploadTest::assertStringNotContainsString ("useUniqueFileName " ,$ stream );
356357 UploadTest::assertStringNotContainsString ("customCoordinates " ,$ stream );
357358 UploadTest::assertStringNotContainsString ("responseFields " ,$ stream );
358-
359- $ this ->stubHttpClient (new Response (200 , ['X-Foo ' => 'Bar ' ], $ mockBodyResponse ));
360-
361- $ response = $ this ->client ->uploadFile ($ fileOptions );
362359 }
363360
364361 /**
0 commit comments