Skip to content

Commit 94ec907

Browse files
committed
Added resumable upload tests
1 parent 869c254 commit 94ec907

File tree

5 files changed

+92
-2
lines changed

5 files changed

+92
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Max Bot API Client library for PHP
1+
# Max Messenger Bot API Client library for PHP
22

33
[![Actions status](https://github.com/BushlanovDev/max-bot-api-client-php/actions/workflows/ci.yml/badge.svg?style=flat-square)](https://github.com/BushlanovDev/max-bot-api-client-php/actions)
44
[![Coverage](https://raw.githubusercontent.com/BushlanovDev/max-bot-api-client-php/refs/heads/master/.github/badge-coverage.svg?v=2)](https://github.com/BushlanovDev/max-bot-api-client-php/actions)

docs/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ $updateList = $api->getUpdates(
332332

333333
```php
334334
$uploadEndpoint = $api->getUploadUrl(UploadType::Video);
335-
// Далее вы можете загрузить файл по полученному URL самостоятельно или воспользоваться методом Client::upload()
335+
// Далее вы можете загрузить файл по полученному URL самостоятельно
336+
// или воспользоваться методами Client::multipartUpload(), Client::resumableUpload(), Api::uploadFile()
336337
```
337338

338339
### Загрузка файла

src/Api.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,8 @@ public function getUploadUrl(UploadType $type): UploadEndpoint
446446
}
447447

448448
/**
449+
* Uploads a file to the specified URL.
450+
*
449451
* @param string $uploadUrl The target URL for the upload.
450452
* @param resource $fileHandle A stream resource pointing to the file.
451453
* @param string $fileName The desired file name for the upload.

src/Client.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ public function resumableUpload(
181181
while (!feof($fileResource)) {
182182
$chunk = fread($fileResource, $chunkSize);
183183
if ($chunk === false) {
184+
// @codeCoverageIgnoreStart
184185
throw new RuntimeException('Failed to read chunk from file stream.');
186+
// @codeCoverageIgnoreEnd
185187
}
186188

187189
$chunkLength = strlen($chunk);

tests/ClientTest.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,89 @@ public function resumableUploadThrowsNetworkExceptionOnChunkUploadFailure(): voi
451451
$this->client->resumableUpload('http://a.b', $fileResource, 'file.txt', 9);
452452
fclose($fileResource);
453453
}
454+
455+
#[Test]
456+
public function resumableUploadSuccessfullyUploadsSingleChunk(): void
457+
{
458+
$fileContents = 'test-data';
459+
$fileResource = fopen('php://memory', 'w+');
460+
fwrite($fileResource, $fileContents);
461+
rewind($fileResource);
462+
463+
$uploadUrl = 'http://a.b';
464+
$fileName = 'file.txt';
465+
$fileSize = strlen($fileContents);
466+
467+
$this->requestFactoryMock->method('createRequest')->willReturn($this->requestMock);
468+
$this->requestMock->method('withBody')->willReturnSelf();
469+
$this->requestMock->method('withHeader')->willReturnSelf();
470+
471+
$this->httpClientMock
472+
->expects($this->once())
473+
->method('sendRequest')
474+
->with($this->requestMock)
475+
->willReturn($this->responseMock);
476+
477+
$this->responseMock->method('getStatusCode')->willReturn(200);
478+
$this->streamMock->method('__toString')->willReturn('<retval>1</retval>');
479+
480+
$result = $this->client->resumableUpload($uploadUrl, $fileResource, $fileName, $fileSize);
481+
482+
$this->assertSame('<retval>1</retval>', $result);
483+
fclose($fileResource);
484+
}
485+
486+
#[Test]
487+
public function resumableUploadSuccessfullyUploadsMultipleChunks(): void
488+
{
489+
$fileContents = str_repeat('A', 3 * 1024 * 1024); // 3 MB
490+
$fileResource = fopen('php://memory', 'w+');
491+
fwrite($fileResource, $fileContents);
492+
rewind($fileResource);
493+
494+
$uploadUrl = 'http://a.b';
495+
$fileName = 'bigfile.bin';
496+
$fileSize = strlen($fileContents);
497+
498+
$this->requestFactoryMock->method('createRequest')->willReturn($this->requestMock);
499+
$this->requestMock->method('withBody')->willReturnSelf();
500+
$this->requestMock->method('withHeader')->willReturnSelf();
501+
502+
$this->httpClientMock
503+
->expects($this->exactly(3))
504+
->method('sendRequest')
505+
->willReturnOnConsecutiveCalls(
506+
$this->responseMock,
507+
$this->responseMock,
508+
$this->responseMock,
509+
);
510+
511+
$this->responseMock->method('getStatusCode')->willReturn(200);
512+
$this->streamMock
513+
->method('__toString')
514+
->willReturnOnConsecutiveCalls('', '', '<retval>1</retval>');
515+
516+
$result = $this->client->resumableUpload($uploadUrl, $fileResource, $fileName, $fileSize, 1024 * 1024);
517+
518+
$this->assertSame('<retval>1</retval>', $result);
519+
fclose($fileResource);
520+
}
521+
522+
#[Test]
523+
public function resumableUploadStopsOnEmptyChunk(): void
524+
{
525+
$fileResource = fopen('php://memory', 'w+');
526+
rewind($fileResource);
527+
528+
$uploadUrl = 'http://a.b';
529+
$fileName = 'empty.txt';
530+
531+
$this->requestFactoryMock->expects($this->never())->method('createRequest');
532+
$this->httpClientMock->expects($this->never())->method('sendRequest');
533+
534+
$result = $this->client->resumableUpload($uploadUrl, $fileResource, $fileName, 100);
535+
536+
$this->assertSame('', $result);
537+
fclose($fileResource);
538+
}
454539
}

0 commit comments

Comments
 (0)