@@ -162,7 +162,16 @@ public function testFileUploadIfSuccessful()
162162 "customMetadata " => [ // An array of created custom fields, for more details refer to docs
163163 "SKU " => "VS882HJ2JD " ,
164164 "price " => 599.99 ,
165- ]
165+ ],
166+ 'transformation ' => [
167+ 'pre ' => 'l-text,i-Imagekit,fs-50,l-end ' ,
168+ 'post ' => [
169+ [
170+ 'type ' => 'transformation ' ,
171+ 'value ' => 'h-100 '
172+ ]
173+ ]
174+ ],
166175 ];
167176
168177 $ mockBodyResponse = Utils::streamFor (json_encode ($ this ->uploadSuccessResponseObj ));
@@ -205,6 +214,126 @@ public function testFileUploadIfSuccessful()
205214 $ this ->checkFormData ($ stream ,$ boundary ,"overwriteAITags " ,"false " );
206215 $ this ->checkFormData ($ stream ,$ boundary ,"overwriteCustomMetadata " ,"true " );
207216 $ this ->checkFormData ($ stream ,$ boundary ,"customMetadata " ,json_encode ($ fileOptions ['customMetadata ' ]));
217+ $ this ->checkFormData ($ stream ,$ boundary ,"transformation " ,json_encode ($ fileOptions ['transformation ' ]));
218+
219+ // Assert Method
220+ $ requestMethod = $ container [0 ]['request ' ]->getMethod ();
221+ UploadTest::assertEquals ($ requestMethod ,'POST ' );
222+
223+ // Response Check
224+ UploadTest::assertEquals (json_encode ($ this ->uploadSuccessResponseObj ), json_encode ($ response ->result ));
225+ }
226+
227+ /**
228+ *
229+ */
230+ public function testFileUploadWithOnlyPreTransformationIfSuccessful ()
231+ {
232+ $ fileOptions = [
233+ 'file ' => 'http://lorempixel.com/640/480/ ' ,
234+ 'fileName ' => 'test_file_name ' ,
235+ "useUniqueFileName " => true , // true|false
236+ "responseFields " => implode (", " , ["tags " , "customMetadata " ]), // Comma Separated, check docs for more responseFields
237+ 'transformation ' => [
238+ 'pre ' => 'l-text,i-Imagekit,fs-50,l-end ' ,
239+ ],
240+ ];
241+
242+ $ mockBodyResponse = Utils::streamFor (json_encode ($ this ->uploadSuccessResponseObj ));
243+
244+
245+ $ mock = new MockHandler ([
246+ new Response (200 , ['X-Foo ' => 'Bar ' ], $ mockBodyResponse )
247+ ]);
248+
249+ $ handlerStack = HandlerStack::create ($ mock );
250+
251+ $ container = [];
252+ $ history = Middleware::history ($ container );
253+
254+ $ handlerStack ->push ($ history );
255+
256+ $ this ->createMockClient ($ handlerStack );
257+
258+ $ response = $ this ->mockClient ->uploadFile ($ fileOptions );
259+
260+ $ requestBody = $ container [0 ]['request ' ]->getBody ();
261+ $ requestHeaders = $ container [0 ]['request ' ]->getHeaders ();
262+ $ boundary = str_replace ("multipart/form-data; boundary= " ,"" ,$ requestHeaders ["Content-Type " ][0 ]);
263+
264+ UploadTest::assertArrayHasKey ("Content-Type " ,$ requestHeaders );
265+ UploadTest::assertStringStartsWith ("multipart/form-data; boundary= " ,$ requestHeaders ['Content-Type ' ][0 ]);
266+
267+ $ stream = Utils::streamFor ($ requestBody );
268+ $ stream = str_replace ('\r\n ' ,' ' ,json_encode ($ stream ->getContents ()));
269+
270+ $ this ->checkFormData ($ stream ,$ boundary ,"file " ,$ fileOptions ['file ' ]);
271+ $ this ->checkFormData ($ stream ,$ boundary ,"fileName " ,$ fileOptions ['fileName ' ]);
272+ $ this ->checkFormData ($ stream ,$ boundary ,"useUniqueFileName " ,"true " );
273+ $ this ->checkFormData ($ stream ,$ boundary ,"responseFields " ,implode (", " , ["tags " , "customMetadata " ]));
274+ $ this ->checkFormData ($ stream ,$ boundary ,"transformation " ,json_encode ($ fileOptions ['transformation ' ]));
275+
276+ // Assert Method
277+ $ requestMethod = $ container [0 ]['request ' ]->getMethod ();
278+ UploadTest::assertEquals ($ requestMethod ,'POST ' );
279+
280+ // Response Check
281+ UploadTest::assertEquals (json_encode ($ this ->uploadSuccessResponseObj ), json_encode ($ response ->result ));
282+ }
283+
284+ /**
285+ *
286+ */
287+ public function testFileUploadWithOnlyPostTransformationIfSuccessful ()
288+ {
289+ $ fileOptions = [
290+ 'file ' => 'http://lorempixel.com/640/480/ ' ,
291+ 'fileName ' => 'test_file_name ' ,
292+ "useUniqueFileName " => true , // true|false
293+ "responseFields " => implode (", " , ["tags " , "customMetadata " ]), // Comma Separated, check docs for more responseFields
294+ 'transformation ' => [
295+ 'post ' => [
296+ [
297+ 'type ' => 'transformation ' ,
298+ 'value ' => 'h-100 '
299+ ]
300+ ]
301+ ],
302+ ];
303+
304+ $ mockBodyResponse = Utils::streamFor (json_encode ($ this ->uploadSuccessResponseObj ));
305+
306+
307+ $ mock = new MockHandler ([
308+ new Response (200 , ['X-Foo ' => 'Bar ' ], $ mockBodyResponse )
309+ ]);
310+
311+ $ handlerStack = HandlerStack::create ($ mock );
312+
313+ $ container = [];
314+ $ history = Middleware::history ($ container );
315+
316+ $ handlerStack ->push ($ history );
317+
318+ $ this ->createMockClient ($ handlerStack );
319+
320+ $ response = $ this ->mockClient ->uploadFile ($ fileOptions );
321+
322+ $ requestBody = $ container [0 ]['request ' ]->getBody ();
323+ $ requestHeaders = $ container [0 ]['request ' ]->getHeaders ();
324+ $ boundary = str_replace ("multipart/form-data; boundary= " ,"" ,$ requestHeaders ["Content-Type " ][0 ]);
325+
326+ UploadTest::assertArrayHasKey ("Content-Type " ,$ requestHeaders );
327+ UploadTest::assertStringStartsWith ("multipart/form-data; boundary= " ,$ requestHeaders ['Content-Type ' ][0 ]);
328+
329+ $ stream = Utils::streamFor ($ requestBody );
330+ $ stream = str_replace ('\r\n ' ,' ' ,json_encode ($ stream ->getContents ()));
331+
332+ $ this ->checkFormData ($ stream ,$ boundary ,"file " ,$ fileOptions ['file ' ]);
333+ $ this ->checkFormData ($ stream ,$ boundary ,"fileName " ,$ fileOptions ['fileName ' ]);
334+ $ this ->checkFormData ($ stream ,$ boundary ,"useUniqueFileName " ,"true " );
335+ $ this ->checkFormData ($ stream ,$ boundary ,"responseFields " ,implode (", " , ["tags " , "customMetadata " ]));
336+ $ this ->checkFormData ($ stream ,$ boundary ,"transformation " ,json_encode ($ fileOptions ['transformation ' ]));
208337
209338 // Assert Method
210339 $ requestMethod = $ container [0 ]['request ' ]->getMethod ();
@@ -527,6 +656,135 @@ public function testServerSideError()
527656 // Request Body Check
528657 UploadTest::assertEquals (json_encode ($ error ),json_encode ($ response ->error ));
529658 }
659+
660+ /**
661+ *
662+ */
663+ public function testFileUploadWithInvalidTransformation ()
664+ {
665+ $ fileOptions = [
666+ 'file ' => 'http://lorempixel.com/640/480/ ' ,
667+ 'fileName ' => 'test_file_name ' ,
668+ "useUniqueFileName " => true , // true|false
669+ "responseFields " => implode (", " , ["tags " , "customMetadata " ]), // Comma Separated, check docs for more responseFields
670+ 'transformation ' => [],
671+ ];
672+
673+ $ error = [
674+ "message " => "Invalid transformation parameter. Please include at least pre, post, or both. " ,
675+ "help " =>
"For support kindly contact us at [email protected] . " 676+ ];
677+
678+ $ mockBodyResponse = Utils::streamFor (json_encode ($ fileOptions ));
679+
680+ $ this ->stubHttpClient (new Response (403 , ['X-Foo ' => 'Bar ' ], json_encode ($ error )));
681+
682+ $ response = $ this ->client ->uploadFile ($ fileOptions );
683+
684+ // Request Body Check
685+ UploadTest::assertEquals (json_encode ($ error ),json_encode ($ response ->error ));
686+ }
687+
688+ /**
689+ *
690+ */
691+ public function testFileUploadWithInvalidPreTransformation ()
692+ {
693+ $ fileOptions = [
694+ 'file ' => 'http://lorempixel.com/640/480/ ' ,
695+ 'fileName ' => 'test_file_name ' ,
696+ "useUniqueFileName " => true , // true|false
697+ "responseFields " => implode (", " , ["tags " , "customMetadata " ]), // Comma Separated, check docs for more responseFields
698+ 'transformation ' => [
699+ 'pre ' => '' ,
700+ ],
701+ ];
702+
703+ $ error = [
704+ "message " => "Invalid pre transformation parameter. " ,
705+ "help " =>
"For support kindly contact us at [email protected] . " 706+ ];
707+
708+ $ mockBodyResponse = Utils::streamFor (json_encode ($ fileOptions ));
709+
710+ $ this ->stubHttpClient (new Response (403 , ['X-Foo ' => 'Bar ' ], json_encode ($ error )));
711+
712+ $ response = $ this ->client ->uploadFile ($ fileOptions );
713+
714+ // Request Body Check
715+ UploadTest::assertEquals (json_encode ($ error ),json_encode ($ response ->error ));
716+ }
717+
718+ /**
719+ *
720+ */
721+ public function testFileUploadWithInvalidAbsTypePostTransformation ()
722+ {
723+ $ fileOptions = [
724+ 'file ' => 'http://lorempixel.com/640/480/ ' ,
725+ 'fileName ' => 'test_file_name ' ,
726+ "useUniqueFileName " => true , // true|false
727+ "responseFields " => implode (", " , ["tags " , "customMetadata " ]), // Comma Separated, check docs for more responseFields
728+ 'transformation ' => [
729+ 'post ' => [
730+ [
731+ 'type ' => 'abs ' ,
732+ 'value ' => ''
733+ ]
734+ ]
735+ ],
736+ ];
737+
738+ $ error = [
739+ "message " => "Invalid post transformation parameter. " ,
740+ "help " =>
"For support kindly contact us at [email protected] . " 741+ ];
742+
743+ $ mockBodyResponse = Utils::streamFor (json_encode ($ fileOptions ));
744+
745+ $ this ->stubHttpClient (new Response (403 , ['X-Foo ' => 'Bar ' ], json_encode ($ error )));
746+
747+ $ response = $ this ->client ->uploadFile ($ fileOptions );
748+
749+ // Request Body Check
750+ UploadTest::assertEquals (json_encode ($ error ),json_encode ($ response ->error ));
751+ }
752+
753+ /**
754+ *
755+ */
756+ public function testFileUploadWithInvalidTransformationTypePostTransformation ()
757+ {
758+ $ fileOptions = [
759+ 'file ' => 'http://lorempixel.com/640/480/ ' ,
760+ 'fileName ' => 'test_file_name ' ,
761+ "useUniqueFileName " => true , // true|false
762+ "responseFields " => implode (", " , ["tags " , "customMetadata " ]), // Comma Separated, check docs for more responseFields
763+ 'transformation ' => [
764+ 'post ' => [
765+ [
766+ 'type ' => 'transformation ' ,
767+ 'value ' => ''
768+ ]
769+ ]
770+ ],
771+ ];
772+
773+ $ error = [
774+ "message " => "Invalid post transformation parameter. " ,
775+ "help " =>
"For support kindly contact us at [email protected] . " 776+ ];
777+
778+ $ mockBodyResponse = Utils::streamFor (json_encode ($ fileOptions ));
779+
780+ $ this ->stubHttpClient (new Response (403 , ['X-Foo ' => 'Bar ' ], json_encode ($ error )));
781+
782+ $ response = $ this ->client ->uploadFile ($ fileOptions );
783+
784+ // Request Body Check
785+ UploadTest::assertEquals (json_encode ($ error ),json_encode ($ response ->error ));
786+ }
787+
530788
531789 protected function setUp (): void
532790 {
0 commit comments