Skip to content

Commit ceece78

Browse files
authored
Merge pull request #47 from sunrise-php/release/v2.2.0
v2.2
2 parents f685ec6 + 1b47ffb commit ceece78

5 files changed

Lines changed: 275 additions & 1 deletion

File tree

src/Client.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,10 @@ private function createCurlHandleFromRequest(RequestInterface $request, int|stri
178178

179179
$curlOptions[CURLOPT_POSTFIELDS] = null;
180180
if (!in_array($request->getMethod(), self::BODYLESS_HTTP_METHODS, true)) {
181-
$curlOptions[CURLOPT_POSTFIELDS] = (string) $request->getBody();
181+
$body = $request->getBody();
182+
$curlOptions[CURLOPT_POSTFIELDS] = ($body instanceof FormData)
183+
? $body->data
184+
: (string) $body;
182185
}
183186

184187
$curlOptions[CURLOPT_RETURNTRANSFER] = true;

src/File.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/**
4+
* It's free open-source software released under the MIT License.
5+
*
6+
* @author Anatoly Nekhay <afenric@gmail.com>
7+
* @copyright Copyright (c) 2018, Anatoly Nekhay
8+
* @license https://github.com/sunrise-php/http-client-curl/blob/master/LICENSE
9+
* @link https://github.com/sunrise-php/http-client-curl
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sunrise\Http\Client\Curl;
15+
16+
use CURLFile;
17+
18+
/**
19+
* @since 2.2.0
20+
*/
21+
final class File extends CURLFile
22+
{
23+
public function __construct(string $filename, ?string $mediaType = null)
24+
{
25+
parent::__construct($filename, $mediaType);
26+
}
27+
}

src/FormData.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
/**
4+
* It's free open-source software released under the MIT License.
5+
*
6+
* @author Anatoly Nekhay <afenric@gmail.com>
7+
* @copyright Copyright (c) 2018, Anatoly Nekhay
8+
* @license https://github.com/sunrise-php/http-client-curl/blob/master/LICENSE
9+
* @link https://github.com/sunrise-php/http-client-curl
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sunrise\Http\Client\Curl;
15+
16+
use Psr\Http\Message\StreamInterface;
17+
use RuntimeException;
18+
19+
/**
20+
* @since 2.2.0
21+
*/
22+
final class FormData implements StreamInterface
23+
{
24+
public function __construct(
25+
/** @var array<array-key, mixed> */
26+
public readonly array $data,
27+
) {
28+
}
29+
30+
public function __toString(): string
31+
{
32+
return '';
33+
}
34+
35+
public function close(): void
36+
{
37+
}
38+
39+
public function detach(): mixed
40+
{
41+
return null;
42+
}
43+
44+
public function getSize(): ?int
45+
{
46+
return null;
47+
}
48+
49+
public function tell(): int
50+
{
51+
throw new RuntimeException('Stream does not support the tell operation');
52+
}
53+
54+
public function eof(): bool
55+
{
56+
return true;
57+
}
58+
59+
public function isSeekable(): bool
60+
{
61+
return false;
62+
}
63+
64+
public function seek(int $offset, int $whence = SEEK_SET): void
65+
{
66+
throw new RuntimeException('Stream is not seekable');
67+
}
68+
69+
public function rewind(): void
70+
{
71+
throw new RuntimeException('Stream is not seekable');
72+
}
73+
74+
public function isWritable(): bool
75+
{
76+
return false;
77+
}
78+
79+
public function write(string $string): int
80+
{
81+
throw new RuntimeException('Stream is not writable');
82+
}
83+
84+
public function isReadable(): bool
85+
{
86+
return false;
87+
}
88+
89+
public function read(int $length): string
90+
{
91+
throw new RuntimeException('Stream is not readable');
92+
}
93+
94+
public function getContents(): string
95+
{
96+
throw new RuntimeException('Stream is not readable');
97+
}
98+
99+
public function getMetadata(?string $key = null): mixed
100+
{
101+
return $key === null ? [] : null;
102+
}
103+
}

tests/FileTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sunrise\Http\Client\Curl\Tests;
6+
7+
use CURLFile;
8+
use PHPUnit\Framework\TestCase;
9+
use Sunrise\Http\Client\Curl\File;
10+
11+
final class FileTest extends TestCase
12+
{
13+
public function testContract(): void
14+
{
15+
$file = new File('foo', 'bar/baz');
16+
$this->assertInstanceOf(CURLFile::class, $file);
17+
}
18+
19+
public function testConstructor(): void
20+
{
21+
$file = new File('foo', 'bar/baz');
22+
$this->assertSame('foo', $file->getFilename());
23+
$this->assertSame('bar/baz', $file->getMimeType());
24+
}
25+
}

tests/FormDataTest.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sunrise\Http\Client\Curl\Tests;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Psr\Http\Message\StreamInterface;
9+
use RuntimeException;
10+
use Sunrise\Http\Client\Curl\FormData;
11+
12+
final class FormDataTest extends TestCase
13+
{
14+
public function testContract(): void
15+
{
16+
$formData = new FormData([]);
17+
$this->assertInstanceOf(StreamInterface::class, $formData);
18+
}
19+
20+
public function testConstructor(): void
21+
{
22+
$formData = new FormData(['foo' => 'bar']);
23+
$this->assertSame(['foo' => 'bar'], $formData->data);
24+
}
25+
26+
public function testToString(): void
27+
{
28+
$formData = new FormData([]);
29+
$this->assertSame('', (string) $formData);
30+
}
31+
32+
public function testDetach(): void
33+
{
34+
$formData = new FormData([]);
35+
$this->assertNull($formData->detach());
36+
}
37+
38+
public function testGetSize(): void
39+
{
40+
$formData = new FormData([]);
41+
$this->assertNull($formData->getSize());
42+
}
43+
44+
public function testTell(): void
45+
{
46+
$formData = new FormData([]);
47+
$this->expectException(RuntimeException::class);
48+
$formData->tell();
49+
}
50+
51+
public function testEof(): void
52+
{
53+
$formData = new FormData([]);
54+
$this->assertTrue($formData->eof());
55+
}
56+
57+
public function testIsSeekable(): void
58+
{
59+
$formData = new FormData([]);
60+
$this->assertFalse($formData->isSeekable());
61+
}
62+
63+
public function testSeek(): void
64+
{
65+
$formData = new FormData([]);
66+
$this->expectException(RuntimeException::class);
67+
$formData->seek(0);
68+
}
69+
70+
public function testRewind(): void
71+
{
72+
$formData = new FormData([]);
73+
$this->expectException(RuntimeException::class);
74+
$formData->rewind();
75+
}
76+
77+
public function testIsWritable(): void
78+
{
79+
$formData = new FormData([]);
80+
$this->assertFalse($formData->isWritable());
81+
}
82+
83+
public function testWrite(): void
84+
{
85+
$formData = new FormData([]);
86+
$this->expectException(RuntimeException::class);
87+
$formData->write('foo');
88+
}
89+
90+
public function testIsReadable(): void
91+
{
92+
$formData = new FormData([]);
93+
$this->assertFalse($formData->isReadable());
94+
}
95+
96+
public function testRead(): void
97+
{
98+
$formData = new FormData([]);
99+
$this->expectException(RuntimeException::class);
100+
$formData->read(1);
101+
}
102+
103+
public function testGetContents(): void
104+
{
105+
$formData = new FormData([]);
106+
$this->expectException(RuntimeException::class);
107+
$formData->getContents();
108+
}
109+
110+
public function testGetMetadata(): void
111+
{
112+
$formData = new FormData([]);
113+
$this->assertSame([], $formData->getMetadata());
114+
$this->assertSame(null, $formData->getMetadata('foo'));
115+
}
116+
}

0 commit comments

Comments
 (0)