Skip to content

Commit bc3bc28

Browse files
committed
updated mock client for upload api test cases
1 parent 261d947 commit bc3bc28

File tree

3 files changed

+49
-52
lines changed

3 files changed

+49
-52
lines changed

src/ImageKit/ImageKit.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class ImageKit
5959
* @return void
6060
*/
6161
public function __construct($publicKey, $privateKey, $urlEndpoint, $transformationPosition =
62-
Transformation::DEFAULT_TRANSFORMATION_POSITION)
62+
Transformation::DEFAULT_TRANSFORMATION_POSITION, $handlerStack=null)
6363
{
6464
$this->configuration = new Configuration();
6565
if ($publicKey == null || empty($publicKey)) {
@@ -90,8 +90,7 @@ public function __construct($publicKey, $privateKey, $urlEndpoint, $transformati
9090
}
9191
$this->configuration->transformationPosition = $transformationPosition;
9292

93-
94-
$client = new Client(Authorization::addAuthorization($this->configuration));
93+
$client = new Client(Authorization::addAuthorization($this->configuration,$handlerStack));
9594
$this->httpClient = new GuzzleHttpWrapper($client);
9695
}
9796

src/ImageKit/Utils/Authorization.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ class Authorization
1313
* @param Configuration $configuration
1414
* @return array[]
1515
*/
16-
public static function addAuthorization(Configuration $configuration)
16+
public static function addAuthorization(Configuration $configuration,$handlerStack=null)
1717
{
1818
return [
1919
'auth' => [
2020
$configuration->privateKey, ''
21-
]
21+
],
22+
'handler'=>$handlerStack
2223
];
2324
}
2425
}

tests/ImageKit/Upload/UploadTest.php

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use GuzzleHttp\Psr7\Request;
1010
use GuzzleHttp\Psr7\Utils;
1111
use ImageKit\ImageKit;
12+
use ImageKit\Utils\Transformation;
1213
use ImageKit\Resource\GuzzleHttpWrapper;
1314
use GuzzleHttp\Handler\MockHandler;
1415
use 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

Comments
 (0)