Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 349436c

Browse files
Fix upload chunk size configuration handling
1 parent d4dc8e2 commit 349436c

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

src/Api/ApiClient.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,7 @@ public function postFileAsync($endPoint, $file, $parameters, $options = [])
265265

266266
$size = $fileHandle->getSize();
267267

268-
$options[ApiConfig::CHUNK_SIZE] = min(
269-
$this->api->chunkSize,
270-
ArrayUtils::get($options, ApiConfig::CHUNK_SIZE, ApiConfig::DEFAULT_CHUNK_SIZE)
271-
);
268+
$options[ApiConfig::CHUNK_SIZE] = ArrayUtils::get($options, ApiConfig::CHUNK_SIZE, $this->api->chunkSize);
272269

273270
$options[ApiConfig::TIMEOUT] = ArrayUtils::get($options, ApiConfig::TIMEOUT, $this->api->uploadTimeout);
274271

tests/Helpers/MockApiClient.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function __construct($configuration = null)
4545
]
4646
);
4747

48-
$config = $this->httpClient->getConfig();
48+
$config = $this->httpClient->getConfig();
4949
$config['handler'] = HandlerStack::create($this->mockHandler);
5050

5151
$this->httpClient = new Client($config);
@@ -65,6 +65,16 @@ protected function callAsync($method, $endPoint, $options)
6565
return parent::callAsync($method, $endPoint, $options);
6666
}
6767

68+
/**
69+
* Returns request options.
70+
*
71+
* @return array
72+
*/
73+
public function getRequestOptions()
74+
{
75+
return $this->requestOptions;
76+
}
77+
6878
/**
6979
* Returns request multipart options.
7080
*
@@ -82,6 +92,7 @@ static function ($options, $item) {
8292
$options[$item['name']] = $item['contents'] instanceof Stream
8393
? $item['contents']->getContents()
8494
: $item['contents'];
95+
8596
return $options;
8697
}
8798
);

tests/Unit/Upload/UploadApiTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
namespace Cloudinary\Test\Unit\Upload;
1212

1313
use Cloudinary\Api\Exception\ApiError;
14+
use Cloudinary\Configuration\ApiConfig;
15+
use Cloudinary\Configuration\Configuration;
1416
use Cloudinary\Test\Helpers\MockUploadApi;
1517
use Cloudinary\Test\Helpers\RequestAssertionsTrait;
1618
use Cloudinary\Test\Unit\Asset\AssetTestCase;
@@ -20,6 +22,8 @@
2022
*/
2123
final class UploadApiTest extends AssetTestCase
2224
{
25+
const TEST_CHUNK_SIZE = 7357;
26+
2327
use RequestAssertionsTrait;
2428

2529
/**
@@ -61,4 +65,48 @@ public function testDownloadBackedupAsset()
6165
self::assertContains('asset_id', $url);
6266
self::assertContains('version_id', $url);
6367
}
68+
69+
/**
70+
* Should use default chunk size.
71+
*
72+
* @throws ApiError
73+
*/
74+
public function testUploadDefaultChunkSize()
75+
{
76+
$mockUploadApi = new MockUploadApi();
77+
$mockUploadApi->upload(self::TEST_BASE64_IMAGE);
78+
$lastOptions = $mockUploadApi->getApiClient()->getRequestOptions();
79+
80+
self::assertArraySubset(['chunk_size' => ApiConfig::DEFAULT_CHUNK_SIZE], $lastOptions);
81+
}
82+
83+
/**
84+
* Should support setting custom chunk size.
85+
*
86+
* @throws ApiError
87+
*/
88+
public function testUploadCustomChunkSizeOptions()
89+
{
90+
$mockUploadApi = new MockUploadApi();
91+
$mockUploadApi->upload(self::TEST_BASE64_IMAGE, ['chunk_size' => self::TEST_CHUNK_SIZE]);
92+
$lastOptions = $mockUploadApi->getApiClient()->getRequestOptions();
93+
94+
self::assertArraySubset(['chunk_size' => self::TEST_CHUNK_SIZE], $lastOptions);
95+
}
96+
97+
/**
98+
* Should support setting custom chunk size in config.
99+
*
100+
* @throws ApiError
101+
*/
102+
public function testUploadCustomChunkSizeConfig()
103+
{
104+
Configuration::instance()->api->chunkSize = self::TEST_CHUNK_SIZE;
105+
106+
$mockUploadApi = new MockUploadApi();
107+
$mockUploadApi->upload(self::TEST_BASE64_IMAGE);
108+
$lastOptions = $mockUploadApi->getApiClient()->getRequestOptions();
109+
110+
self::assertArraySubset(['chunk_size' => self::TEST_CHUNK_SIZE], $lastOptions);
111+
}
64112
}

0 commit comments

Comments
 (0)