Skip to content

Commit 94f05ea

Browse files
LuborRodRodion Liuborets
andauthored
Return null when we receive 204 (#263)
Co-authored-by: Rodion Liuborets <[email protected]>
1 parent 98e009f commit 94f05ea

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

src/DocScan/DocScanClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ public function deleteSession(string $sessionId): void
109109
*
110110
* @param string $sessionId
111111
* @param string $mediaId
112-
* @return Media
112+
* @return Media|null
113113
* @throws Exception\DocScanException
114114
*/
115-
public function getMediaContent(string $sessionId, string $mediaId): Media
115+
public function getMediaContent(string $sessionId, string $mediaId): ?Media
116116
{
117117
return $this->docScanService->getMediaContent($sessionId, $mediaId);
118118
}

src/DocScan/Service.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828

2929
class Service
3030
{
31+
/** @const int */
32+
private const HTTP_STATUS_NO_CONTENT = 204;
33+
3134
/**
3235
* @var string
3336
*/
@@ -142,10 +145,10 @@ public function deleteSession(string $sessionId): void
142145
*
143146
* @param string $sessionId
144147
* @param string $mediaId
145-
* @return Media
148+
* @return Media|null if 204 No Content
146149
* @throws DocScanException
147150
*/
148-
public function getMediaContent(string $sessionId, string $mediaId): Media
151+
public function getMediaContent(string $sessionId, string $mediaId): ?Media
149152
{
150153
$response = (new RequestBuilder($this->config))
151154
->withBaseUrl($this->apiUrl)
@@ -158,6 +161,10 @@ public function getMediaContent(string $sessionId, string $mediaId): Media
158161

159162
self::assertResponseIsSuccess($response);
160163

164+
if ($response->getStatusCode() == self::HTTP_STATUS_NO_CONTENT) {
165+
return null;
166+
}
167+
161168
$content = (string)$response->getBody();
162169
$mimeType = $response->getHeader("Content-Type")[0] ?? '';
163170

tests/DocScan/DocScanClientTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,31 @@ public function testGetMedia()
226226
);
227227
}
228228

229+
/**
230+
* @test
231+
* @covers ::__construct
232+
* @covers ::getMediaContent
233+
*/
234+
public function testGetMediaIfNoContent()
235+
{
236+
$response = $this->createMock(ResponseInterface::class);
237+
$response->method('getStatusCode')->willReturn(204);
238+
$response->method('getHeader')->willReturn([ 'image/png' ]);
239+
240+
$httpClient = $this->createMock(ClientInterface::class);
241+
$httpClient->expects($this->exactly(1))
242+
->method('sendRequest')
243+
->willReturn($response);
244+
245+
$docScanClient = new DocScanClient(TestData::SDK_ID, TestData::PEM_FILE, [
246+
Config::HTTP_CLIENT => $httpClient,
247+
]);
248+
249+
$this->assertNull(
250+
$docScanClient->getMediaContent(TestData::DOC_SCAN_SESSION_ID, TestData::DOC_SCAN_MEDIA_ID)
251+
);
252+
}
253+
229254
/**
230255
* @test
231256
* @covers ::__construct

tests/DocScan/ServiceTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ function (RequestInterface $requestMessage) {
395395
* @covers ::getMediaContent
396396
* @covers ::assertResponseIsSuccess
397397
*/
398-
public function getMediaContentShouldReturnMediaObjectWithNoContent()
398+
public function getMediaContentShouldReturnNullWhenResponseWithNoContent()
399399
{
400400
$httpClient = $this->createMock(ClientInterface::class);
401401
$httpClient->expects($this->exactly(1))
@@ -419,9 +419,7 @@ public function getMediaContentShouldReturnMediaObjectWithNoContent()
419419
);
420420

421421
$media = $docScanService->getMediaContent(TestData::DOC_SCAN_SESSION_ID, TestData::DOC_SCAN_MEDIA_ID);
422-
$this->assertInstanceOf(Media::class, $media);
423-
$this->assertEquals('', $media->getMimeType());
424-
$this->assertEquals('', $media->getContent());
422+
$this->assertNull($media);
425423
}
426424

427425
/**

0 commit comments

Comments
 (0)