Skip to content

Commit 05e454e

Browse files
author
awu
committed
refactor: remove FileUpload handling from CurlFactory and related tests
1 parent 9ec8ead commit 05e454e

9 files changed

Lines changed: 4 additions & 249 deletions

File tree

src/Stream/CurlFactory.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,27 +72,13 @@ public static function create(
7272
$options[CURLOPT_POSTFIELDS] = $request->getBody();
7373
}
7474

75-
if ($request->getBody() instanceof FileUpload) {
76-
$fileUpload = $request->getBody();
77-
78-
$options[CURLOPT_UPLOAD] = true;
79-
$options[CURLOPT_RETURNTRANSFER] = true;
80-
$options[CURLOPT_INFILESIZE] = $fileUpload->getSize();
81-
$options[CURLOPT_READFUNCTION] = function () use ($fileUpload): string {
82-
return $fileUpload->read();
83-
};
84-
}
85-
8675
if ($request->hasHeader('Accept-Encoding')) {
8776
$options[CURLOPT_ENCODING] = implode(',', $request->getHeader('Accept-Encoding'));
8877
}
8978

9079
if ($request->getMethod() === 'HEAD') {
9180
$options[CURLOPT_NOBODY] = true;
9281
unset(
93-
$options[CURLOPT_UPLOAD],
94-
$options[CURLOPT_INFILESIZE],
95-
$options[CURLOPT_READFUNCTION],
9682
$options[CURLOPT_WRITEFUNCTION],
9783
);
9884
}

src/Stream/CurlMultiHandler.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ public function execute(): void
7676
throw new RuntimeException('Internal HttpClient: Failed to add cURL handle to multi handle: ' . curl_multi_strerror(curl_multi_errno($multiHandle)));
7777
}
7878

79-
$infinityLoop = 0;
80-
8179
do {
8280
$status = curl_multi_exec($multiHandle, $isRunning);
8381
if ($isRunning) {
@@ -86,10 +84,6 @@ public function execute(): void
8684

8785
$this->verifyCurlHandle($multiHandle);
8886

89-
if (++$infinityLoop === 5) {
90-
throw new RuntimeException('Internal HttpClient: cURL multi exec loop exceeded maximum iterations.');
91-
}
92-
9387
} while ($this->header->isEmpty() && $isRunning && $status === CURLM_OK);
9488

9589
$this->multiHandle = $multiHandle;

src/Stream/FileUpload.php

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/Stream/HttpClient.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,19 @@ public function buildHeaders(?string $apiToken, null|array|object $body = null):
3737
if ($apiToken !== null) {
3838
$headers[] = 'Authorization: Bearer ' . $apiToken;
3939
}
40-
if ($body !== null && !$body instanceof FileUpload) {
40+
if ($body !== null) {
4141
$headers[] = 'Content-Type: application/json';
4242
}
43-
if ($body instanceof FileUpload) {
44-
$headers[] = 'Content-Type: ' . $body->getContentType();
45-
}
4643

4744
return $headers;
4845
}
4946

50-
public function buildBody(null|array|object $body): string|FileUpload
47+
public function buildBody(null|array|object $body): string
5148
{
5249
if ($body === null) {
5350
return '';
5451
}
5552

56-
if ($body instanceof FileUpload) {
57-
return $body;
58-
}
59-
6053
return json_encode($body);
6154
}
6255

src/Stream/Request.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function __construct(
1414
private readonly string $method,
1515
string $uri,
1616
array $headers = [],
17-
private readonly null|string|FileUpload $body = null,
17+
private readonly ?string $body = null,
1818
private readonly string $protocolVersion = '1.1'
1919
) {
2020
$this->uri = new Uri($uri);
@@ -36,7 +36,7 @@ public function getProtocolVersion(): string
3636
return $this->protocolVersion;
3737
}
3838

39-
public function getBody(): null|string|FileUpload
39+
public function getBody(): ?string
4040
{
4141
return $this->body;
4242
}

tests/Stream/CurlFactoryTest.php

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PHPUnit\Framework\MockObject\MockObject;
88
use PHPUnit\Framework\TestCase;
99
use Thenativeweb\Eventsourcingdb\Stream\CurlFactory;
10-
use Thenativeweb\Eventsourcingdb\Stream\FileUpload;
1110
use Thenativeweb\Eventsourcingdb\Stream\Queue;
1211
use Thenativeweb\Eventsourcingdb\Stream\Request;
1312
use Thenativeweb\Eventsourcingdb\Stream\Uri;
@@ -122,38 +121,6 @@ public function testCreateSetsPostFieldsIfBodyExists(): void
122121
$this->assertSame($body, $options[CURLOPT_POSTFIELDS]);
123122
}
124123

125-
public function testCreateSetsReadFunctionIfBodyFileUpload(): void
126-
{
127-
$fileUpload = $this->createMock(FileUpload::class);
128-
$fileUpload->method('getSize')->willReturn(123);
129-
$fileUpload->method('read')->willReturn('chunk');
130-
131-
$this->uriMock->method('__toString')->willReturn('https://example.com/upload');
132-
$this->uriMock->method('getScheme')->willReturn('https');
133-
134-
$this->requestMock->method('getMethod')->willReturn('POST');
135-
$this->requestMock->method('getProtocolVersion')->willReturn('1.1');
136-
$this->requestMock->method('getHeaders')->willReturn([]);
137-
$this->requestMock->method('getUri')->willReturn($this->uriMock);
138-
$this->requestMock->method('getBody')->willReturn($fileUpload);
139-
$this->requestMock->method('hasHeader')->willReturn(false);
140-
141-
$options = CurlFactory::create(
142-
$this->requestMock,
143-
$this->headerQueueMock,
144-
$this->writeQueueMock,
145-
);
146-
147-
$this->assertArrayHasKey(CURLOPT_INFILESIZE, $options);
148-
$this->assertArrayHasKey(CURLOPT_READFUNCTION, $options);
149-
$this->assertArrayHasKey(CURLOPT_RETURNTRANSFER, $options);
150-
$this->assertArrayHasKey(CURLOPT_UPLOAD, $options);
151-
$this->assertSame(123, $options[CURLOPT_INFILESIZE]);
152-
$this->assertIsCallable($options[CURLOPT_READFUNCTION]);
153-
$this->assertTrue($options[CURLOPT_RETURNTRANSFER]);
154-
$this->assertTrue($options[CURLOPT_UPLOAD]);
155-
}
156-
157124
public function testCreateSetsNoBodyForHeadMethod(): void
158125
{
159126
$this->requestMock->method('getMethod')->willReturnCallback(static fn (): string => 'HEAD');

tests/Stream/FileUploadTest.php

Lines changed: 0 additions & 91 deletions
This file was deleted.

tests/Stream/HttpClientTest.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use ArrayIterator;
88
use PHPUnit\Framework\Attributes\DataProvider;
99
use PHPUnit\Framework\TestCase;
10-
use Thenativeweb\Eventsourcingdb\Stream\FileUpload;
1110
use Thenativeweb\Eventsourcingdb\Stream\Header;
1211
use Thenativeweb\Eventsourcingdb\Stream\HttpClient;
1312
use Thenativeweb\Eventsourcingdb\Stream\Queue;
@@ -134,20 +133,6 @@ public function testReturnsJsonHeaderWithoutToken(): void
134133
$this->assertContains('Content-Type: application/json', $headers);
135134
}
136135

137-
public function testReturnsFileUploadContentType(): void
138-
{
139-
$mockFileUpload = $this->createMock(FileUpload::class);
140-
$mockFileUpload->method('getContentType')->willReturn('application/x-ndjson');
141-
142-
$httpClient = new HttpClient();
143-
$headers = $httpClient->buildHeaders('secret', $mockFileUpload);
144-
145-
$this->assertCount(3, $headers);
146-
$this->assertContains('Expect:', $headers, 'Ignore curl response header 100-continue');
147-
$this->assertContains('Authorization: Bearer secret', $headers);
148-
$this->assertContains('Content-Type: application/x-ndjson', $headers);
149-
}
150-
151136
public function testReturnsEmptyHeadersWhenNoArgumentsGiven(): void
152137
{
153138
$httpClient = new HttpClient();
@@ -162,15 +147,6 @@ public function testReturnsEmptyStringForNull(): void
162147
$this->assertSame('', $result);
163148
}
164149

165-
public function testReturnsFileUploadInstanceUnchanged(): void
166-
{
167-
$fileUpload = $this->createMock(FileUpload::class);
168-
169-
$httpClient = new HttpClient();
170-
$result = $httpClient->buildBody($fileUpload);
171-
$this->assertSame($fileUpload, $result);
172-
}
173-
174150
public function testReturnsJsonEncodedStringForArray(): void
175151
{
176152
$array = [

tests/Stream/RequestTest.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
namespace Stream;
66

77
use PHPUnit\Framework\TestCase;
8-
use SplFileObject;
9-
use Thenativeweb\Eventsourcingdb\Stream\FileUpload;
108
use Thenativeweb\Eventsourcingdb\Stream\Request;
119
use Thenativeweb\Eventsourcingdb\Stream\Uri;
1210

@@ -51,16 +49,4 @@ public function testGetProtocolVersionDefaultsTo11(): void
5149
$request = new Request('GET', 'https://example.com');
5250
$this->assertSame('1.1', $request->getProtocolVersion());
5351
}
54-
55-
public function testConstructorInitializesWithFileUpload(): void
56-
{
57-
$fileUpload = new FileUpload(new SplFileObject(__FILE__));
58-
$request = new Request(
59-
method: 'post',
60-
uri: 'https://example.com/path',
61-
body: $fileUpload,
62-
);
63-
64-
$this->assertSame($fileUpload, $request->getBody());
65-
}
6652
}

0 commit comments

Comments
 (0)