mirror of
https://github.com/BushlanovDev/max-bot-api-client-php.git
synced 2026-08-30 20:16:57 +00:00
Added resumable upload tests
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
# Max Bot API Client library for PHP
|
# Max Messenger Bot API Client library for PHP
|
||||||
|
|
||||||
[](https://github.com/BushlanovDev/max-bot-api-client-php/actions)
|
[](https://github.com/BushlanovDev/max-bot-api-client-php/actions)
|
||||||
[](https://github.com/BushlanovDev/max-bot-api-client-php/actions)
|
[](https://github.com/BushlanovDev/max-bot-api-client-php/actions)
|
||||||
|
|||||||
+2
-1
@@ -332,7 +332,8 @@ $updateList = $api->getUpdates(
|
|||||||
|
|
||||||
```php
|
```php
|
||||||
$uploadEndpoint = $api->getUploadUrl(UploadType::Video);
|
$uploadEndpoint = $api->getUploadUrl(UploadType::Video);
|
||||||
// Далее вы можете загрузить файл по полученному URL самостоятельно или воспользоваться методом Client::upload()
|
// Далее вы можете загрузить файл по полученному URL самостоятельно
|
||||||
|
// или воспользоваться методами Client::multipartUpload(), Client::resumableUpload(), Api::uploadFile()
|
||||||
```
|
```
|
||||||
|
|
||||||
### Загрузка файла
|
### Загрузка файла
|
||||||
|
|||||||
@@ -446,6 +446,8 @@ class Api
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Uploads a file to the specified URL.
|
||||||
|
*
|
||||||
* @param string $uploadUrl The target URL for the upload.
|
* @param string $uploadUrl The target URL for the upload.
|
||||||
* @param resource $fileHandle A stream resource pointing to the file.
|
* @param resource $fileHandle A stream resource pointing to the file.
|
||||||
* @param string $fileName The desired file name for the upload.
|
* @param string $fileName The desired file name for the upload.
|
||||||
|
|||||||
@@ -181,7 +181,9 @@ final readonly class Client implements ClientApiInterface
|
|||||||
while (!feof($fileResource)) {
|
while (!feof($fileResource)) {
|
||||||
$chunk = fread($fileResource, $chunkSize);
|
$chunk = fread($fileResource, $chunkSize);
|
||||||
if ($chunk === false) {
|
if ($chunk === false) {
|
||||||
|
// @codeCoverageIgnoreStart
|
||||||
throw new RuntimeException('Failed to read chunk from file stream.');
|
throw new RuntimeException('Failed to read chunk from file stream.');
|
||||||
|
// @codeCoverageIgnoreEnd
|
||||||
}
|
}
|
||||||
|
|
||||||
$chunkLength = strlen($chunk);
|
$chunkLength = strlen($chunk);
|
||||||
|
|||||||
@@ -451,4 +451,89 @@ final class ClientTest extends TestCase
|
|||||||
$this->client->resumableUpload('http://a.b', $fileResource, 'file.txt', 9);
|
$this->client->resumableUpload('http://a.b', $fileResource, 'file.txt', 9);
|
||||||
fclose($fileResource);
|
fclose($fileResource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[Test]
|
||||||
|
public function resumableUploadSuccessfullyUploadsSingleChunk(): void
|
||||||
|
{
|
||||||
|
$fileContents = 'test-data';
|
||||||
|
$fileResource = fopen('php://memory', 'w+');
|
||||||
|
fwrite($fileResource, $fileContents);
|
||||||
|
rewind($fileResource);
|
||||||
|
|
||||||
|
$uploadUrl = 'http://a.b';
|
||||||
|
$fileName = 'file.txt';
|
||||||
|
$fileSize = strlen($fileContents);
|
||||||
|
|
||||||
|
$this->requestFactoryMock->method('createRequest')->willReturn($this->requestMock);
|
||||||
|
$this->requestMock->method('withBody')->willReturnSelf();
|
||||||
|
$this->requestMock->method('withHeader')->willReturnSelf();
|
||||||
|
|
||||||
|
$this->httpClientMock
|
||||||
|
->expects($this->once())
|
||||||
|
->method('sendRequest')
|
||||||
|
->with($this->requestMock)
|
||||||
|
->willReturn($this->responseMock);
|
||||||
|
|
||||||
|
$this->responseMock->method('getStatusCode')->willReturn(200);
|
||||||
|
$this->streamMock->method('__toString')->willReturn('<retval>1</retval>');
|
||||||
|
|
||||||
|
$result = $this->client->resumableUpload($uploadUrl, $fileResource, $fileName, $fileSize);
|
||||||
|
|
||||||
|
$this->assertSame('<retval>1</retval>', $result);
|
||||||
|
fclose($fileResource);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Test]
|
||||||
|
public function resumableUploadSuccessfullyUploadsMultipleChunks(): void
|
||||||
|
{
|
||||||
|
$fileContents = str_repeat('A', 3 * 1024 * 1024); // 3 MB
|
||||||
|
$fileResource = fopen('php://memory', 'w+');
|
||||||
|
fwrite($fileResource, $fileContents);
|
||||||
|
rewind($fileResource);
|
||||||
|
|
||||||
|
$uploadUrl = 'http://a.b';
|
||||||
|
$fileName = 'bigfile.bin';
|
||||||
|
$fileSize = strlen($fileContents);
|
||||||
|
|
||||||
|
$this->requestFactoryMock->method('createRequest')->willReturn($this->requestMock);
|
||||||
|
$this->requestMock->method('withBody')->willReturnSelf();
|
||||||
|
$this->requestMock->method('withHeader')->willReturnSelf();
|
||||||
|
|
||||||
|
$this->httpClientMock
|
||||||
|
->expects($this->exactly(3))
|
||||||
|
->method('sendRequest')
|
||||||
|
->willReturnOnConsecutiveCalls(
|
||||||
|
$this->responseMock,
|
||||||
|
$this->responseMock,
|
||||||
|
$this->responseMock,
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->responseMock->method('getStatusCode')->willReturn(200);
|
||||||
|
$this->streamMock
|
||||||
|
->method('__toString')
|
||||||
|
->willReturnOnConsecutiveCalls('', '', '<retval>1</retval>');
|
||||||
|
|
||||||
|
$result = $this->client->resumableUpload($uploadUrl, $fileResource, $fileName, $fileSize, 1024 * 1024);
|
||||||
|
|
||||||
|
$this->assertSame('<retval>1</retval>', $result);
|
||||||
|
fclose($fileResource);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Test]
|
||||||
|
public function resumableUploadStopsOnEmptyChunk(): void
|
||||||
|
{
|
||||||
|
$fileResource = fopen('php://memory', 'w+');
|
||||||
|
rewind($fileResource);
|
||||||
|
|
||||||
|
$uploadUrl = 'http://a.b';
|
||||||
|
$fileName = 'empty.txt';
|
||||||
|
|
||||||
|
$this->requestFactoryMock->expects($this->never())->method('createRequest');
|
||||||
|
$this->httpClientMock->expects($this->never())->method('sendRequest');
|
||||||
|
|
||||||
|
$result = $this->client->resumableUpload($uploadUrl, $fileResource, $fileName, 100);
|
||||||
|
|
||||||
|
$this->assertSame('', $result);
|
||||||
|
fclose($fileResource);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user