Mocking
2 years ago
BodyTest.php
2 years ago
RequestTest.php
2 years ago
ResponseTest.php
2 years ago
bootstrap.php
2 years ago
BodyTest.php
89 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Unirest\Test; |
| 4 | |
| 5 | use PHPUnit\Framework\TestCase; |
| 6 | use Unirest\Request\Body; |
| 7 | use Unirest\Request\Request; |
| 8 | |
| 9 | class BodyTest extends TestCase |
| 10 | { |
| 11 | public function testCURLFile() |
| 12 | { |
| 13 | $fixture = __DIR__ . '/Mocking/upload.txt'; |
| 14 | |
| 15 | $file = Body::File($fixture); |
| 16 | |
| 17 | if (PHP_MAJOR_VERSION === 5 && PHP_MINOR_VERSION === 4) { |
| 18 | $this->assertEquals($file, sprintf('@%s;filename=%s;type=', $fixture, basename($fixture))); |
| 19 | } else { |
| 20 | $this->assertTrue($file instanceof \CURLFile); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | public function testHttpBuildQueryWithCurlFile() |
| 25 | { |
| 26 | $fixture = __DIR__ . '/Mocking/upload.txt'; |
| 27 | |
| 28 | $file = Body::File($fixture); |
| 29 | $body = [ |
| 30 | 'to' => 'mail@mailinator.com', |
| 31 | 'from' => 'mail@mailinator.com', |
| 32 | 'file' => $file |
| 33 | ]; |
| 34 | |
| 35 | $result = Request::buildHTTPCurlQuery($body); |
| 36 | $this->assertEquals($result['file'], $file); |
| 37 | } |
| 38 | |
| 39 | public function testJson() |
| 40 | { |
| 41 | $body = Body::Json(['foo', 'bar']); |
| 42 | |
| 43 | $this->assertEquals($body, '["foo","bar"]'); |
| 44 | } |
| 45 | |
| 46 | public function testForm() |
| 47 | { |
| 48 | $body = Body::Form(['foo' => 'bar', 'bar' => 'baz']); |
| 49 | |
| 50 | $this->assertEquals($body, 'foo=bar&bar=baz'); |
| 51 | |
| 52 | // try again with a string |
| 53 | $body = Body::Form($body); |
| 54 | |
| 55 | $this->assertEquals($body, 'foo=bar&bar=baz'); |
| 56 | } |
| 57 | |
| 58 | public function testMultipart() |
| 59 | { |
| 60 | $arr = ['foo' => 'bar', 'bar' => 'baz']; |
| 61 | |
| 62 | $body = Body::Multipart((object) $arr); |
| 63 | |
| 64 | $this->assertEquals($body, $arr); |
| 65 | |
| 66 | $body = Body::Multipart('flat'); |
| 67 | |
| 68 | $this->assertEquals($body, ['flat']); |
| 69 | } |
| 70 | |
| 71 | public function testMultipartFiles() |
| 72 | { |
| 73 | $fixture = __DIR__ . '/Mocking/upload.txt'; |
| 74 | |
| 75 | $data = ['foo' => 'bar', 'bar' => 'baz']; |
| 76 | $files = ['test' => $fixture]; |
| 77 | |
| 78 | $body = Body::Multipart($data, $files); |
| 79 | |
| 80 | // echo $body; |
| 81 | |
| 82 | $this->assertEquals($body, [ |
| 83 | 'foo' => 'bar', |
| 84 | 'bar' => 'baz', |
| 85 | 'test' => Body::File($fixture) |
| 86 | ]); |
| 87 | } |
| 88 | } |
| 89 |