Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
}
},
"scripts": {
"analyse": "vendor/bin/phpstan analyse -c phpstan.neon --memory-limit=256M",
"analyse": "vendor/bin/phpstan analyse -c phpstan.neon --memory-limit=512M",
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes src",
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage",
Expand Down
11 changes: 7 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ public function request(string $method, string $uri, array $queryParams = [], ar
]);

$fullUrl = $this->baseUrl . $uri . '?' . http_build_query($queryParams);
$request = $this->requestFactory->createRequest($method, $fullUrl);
$request = $this->requestFactory
->createRequest($method, $fullUrl)
->withHeader('Authorization', $this->accessToken);

if (!empty($body)) {
try {
Expand All @@ -82,8 +84,7 @@ public function request(string $method, string $uri, array $queryParams = [], ar
$stream = $this->streamFactory->createStream($payload);
$request = $request
->withBody($stream)
->withHeader('Content-Type', 'application/json; charset=utf-8')
->withHeader('Authorization', $this->accessToken);
->withHeader('Content-Type', 'application/json; charset=utf-8');
}

try {
Expand Down Expand Up @@ -143,6 +144,7 @@ public function multipartUpload(string $uri, mixed $fileContents, string $fileNa
$request = $this->requestFactory
->createRequest('POST', $uri)
->withHeader('Content-Type', 'multipart/form-data; boundary=' . $boundary)
->withHeader('Authorization', $this->accessToken)
->withBody($bodyStream);

try {
Expand Down Expand Up @@ -198,7 +200,8 @@ public function resumableUpload(
->withBody($chunkStream)
->withHeader('Content-Type', 'application/octet-stream')
->withHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"')
->withHeader('Content-Range', "bytes {$startByte}-{$endByte}/{$fileSize}");
->withHeader('Content-Range', "bytes {$startByte}-{$endByte}/{$fileSize}")
->withHeader('Authorization', $this->accessToken);

try {
$response = $this->httpClient->sendRequest($request);
Expand Down
82 changes: 71 additions & 11 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ public function successfulGetRequest(): void
->with('GET', $expectedUrl)
->willReturn($this->requestMock);

$this->requestMock
->expects($this->once())
->method('withHeader')
->with('Authorization', self::FAKE_TOKEN)
->willReturn($this->requestMock);

$this->httpClientMock
->expects($this->once())
->method('sendRequest')
Expand Down Expand Up @@ -161,17 +167,18 @@ public function successfulPostRequestWithJsonBody(): void
->method('withHeader')
->willReturnCallback(function (string $header, string $value) use (&$headerCallCount) {
if ($headerCallCount === 0) {
$this->assertSame('Content-Type', $header);
$this->assertSame('application/json; charset=utf-8', $value);
} elseif ($headerCallCount === 1) {
$this->assertSame('Authorization', $header);
$this->assertSame(self::FAKE_TOKEN, $value);
} elseif ($headerCallCount === 1) {
$this->assertSame('Content-Type', $header);
$this->assertSame('application/json; charset=utf-8', $value);
}

$headerCallCount++;

return $this->requestMock;
});

$this->responseMock->method('getStatusCode')->willReturn(200);
$this->streamMock->method('__toString')->willReturn(json_encode($responsePayload));

Expand Down Expand Up @@ -307,11 +314,23 @@ public function uploadMethodSendsCorrectMultipartRequest(): void
)
->willReturn($this->requestMock);

$headerCallCount = 0;
$this->requestMock
->expects($this->once())
->expects($this->exactly(2))
->method('withHeader')
->with($this->stringStartsWith('Content-Type'), $this->stringStartsWith('multipart/form-data'))
->willReturn($this->requestMock);
->willReturnCallback(function (string $header, string $value) use (&$headerCallCount) {
if ($headerCallCount === 0) {
$this->assertSame('Content-Type', $header);
$this->assertStringStartsWith('multipart/form-data; boundary=', $value);
} elseif ($headerCallCount === 1) {
$this->assertSame('Authorization', $header);
$this->assertSame(self::FAKE_TOKEN, $value);
}

$headerCallCount++;

return $this->requestMock;
});

$this->requestFactoryMock
->expects($this->once())
Expand Down Expand Up @@ -339,9 +358,26 @@ public function uploadMethodHandlesStreamResourceCorrectly(): void
rewind($tmpFileHandle);

$this->requestFactoryMock->method('createRequest')->willReturn($this->requestMock);
$this->requestMock->method('withHeader')->willReturn($this->requestMock);

$headerCallCount = 0;
$this->requestMock
->expects($this->exactly(2))
->method('withHeader')
->willReturnCallback(function (string $header, string $value) use (&$headerCallCount) {
if ($headerCallCount === 0) {
$this->assertSame('Content-Type', $header);
$this->assertStringStartsWith('multipart/form-data; boundary=', $value);
} elseif ($headerCallCount === 1) {
$this->assertSame('Authorization', $header);
$this->assertSame(self::FAKE_TOKEN, $value);
}

$headerCallCount++;

return $this->requestMock;
});

$this->requestMock->method('withBody')->willReturn($this->requestMock);
// $this->httpClientMock->method('sendRequest')->willReturn($this->responseMock);
$this->responseMock->method('getStatusCode')->willReturn(200);
$this->streamMock->method('__toString')->willReturn(json_encode($responsePayload));

Expand Down Expand Up @@ -475,7 +511,30 @@ public function resumableUploadSuccessfullyUploadsSingleChunk(): void

$this->requestFactoryMock->method('createRequest')->willReturn($this->requestMock);
$this->requestMock->method('withBody')->willReturnSelf();
$this->requestMock->method('withHeader')->willReturnSelf();

$headerCallCount = 0;
$this->requestMock
->expects($this->exactly(4))
->method('withHeader')
->willReturnCallback(function (string $header, string $value) use (&$headerCallCount, $fileName, $fileSize) {
if ($headerCallCount === 0) {
$this->assertSame('Content-Type', $header);
$this->assertSame('application/octet-stream', $value);
} elseif ($headerCallCount === 1) {
$this->assertSame('Content-Disposition', $header);
$this->assertSame('attachment; filename="' . $fileName . '"', $value);
} elseif ($headerCallCount === 2) {
$this->assertSame('Content-Range', $header);
$this->assertSame("bytes 0-8/{$fileSize}", $value);
} elseif ($headerCallCount === 3) {
$this->assertSame('Authorization', $header);
$this->assertSame(self::FAKE_TOKEN, $value);
}

$headerCallCount++;

return $this->requestMock;
});

$this->httpClientMock
->expects($this->once())
Expand All @@ -495,7 +554,8 @@ public function resumableUploadSuccessfullyUploadsSingleChunk(): void
#[Test]
public function resumableUploadSuccessfullyUploadsMultipleChunks(): void
{
$fileContents = str_repeat('A', 3 * 1024 * 1024); // 3 MB
$chunkSize = 1024 * 1024;
$fileContents = str_repeat('A', 3 * $chunkSize); // 3 MB
$fileResource = fopen('php://memory', 'w+');
fwrite($fileResource, $fileContents);
rewind($fileResource);
Expand All @@ -522,7 +582,7 @@ public function resumableUploadSuccessfullyUploadsMultipleChunks(): void
->method('__toString')
->willReturnOnConsecutiveCalls('', '', '<retval>1</retval>');

$result = $this->client->resumableUpload($uploadUrl, $fileResource, $fileName, $fileSize, 1024 * 1024);
$result = $this->client->resumableUpload($uploadUrl, $fileResource, $fileName, $fileSize, $chunkSize);

$this->assertSame('<retval>1</retval>', $result);
fclose($fileResource);
Expand Down