Skip to content

Commit

Permalink
handle null case and improve reader test
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Apr 6, 2024
1 parent d455b5e commit 66ef26e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Reader/Multipart.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function read(string $data): mixed
// we use the multipart body only in case there are file uploads, otherwise we return a stdClass similar to
// the form reader, this is needed since some HTTP clients like RestSharp always uses a multipart content
// type containing the form values
return CurveArray::objectify($this->post);
return !empty($this->post) ? CurveArray::objectify($this->post) : null;
}
}

Expand Down
29 changes: 28 additions & 1 deletion tests/Reader/MultipartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public function testRead()
$post = ['bar' => 'foo'];

$reader = new Multipart($files, $post);
$data = $reader->read('');

$this->assertInstanceOf(Body::class, $data);

$actual = json_encode($reader->read(''), JSON_PRETTY_PRINT);

$expect = <<<JSON
Expand All @@ -65,11 +69,34 @@ public function testRead()
$this->assertJsonStringEqualsJsonString($expect, $actual, $actual);
}

public function testReadNoFiles()
{
$files = [];
$files['foo'] = [];

$post = ['bar' => 'foo'];

$reader = new Multipart($files, $post);
$data = $reader->read('');

$this->assertInstanceOf(\stdClass::class, $data);

$actual = json_encode($data, JSON_PRETTY_PRINT);

$expect = <<<JSON
{
"bar": "foo"
}
JSON;

$this->assertJsonStringEqualsJsonString($expect, $actual, $actual);
}

public function testReadEmpty()
{
$reader = new Multipart();
$file = $reader->read('');

$this->assertInstanceOf(Body::class, $file);
$this->assertEmpty($file);
}
}

0 comments on commit 66ef26e

Please sign in to comment.