Skip to content

Added support for multipart stream created from uri streams #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 14, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"require-dev": {
"phpunit/phpunit": "^4.8 || ^5.4",
"php-http/message": "^1.0",
"zendframework/zend-diactoros": "^1.3.5"
"guzzlehttp/psr7": "^1.3.1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets change back to zend.

},
"autoload": {
"psr-4": {
Expand Down
9 changes: 8 additions & 1 deletion src/MultipartStreamBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,14 @@ public function build()
$this->getHeaders($data['headers'])."\r\n";

// Convert the stream to string
$streams .= (string) $data['contents'];
/* @var $contentStream StreamInterface */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good

$contentStream = $data['contents'];
if ($contentStream->isSeekable()) {
$streams .= (string) $data['contents'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though the output it the same. I think I would like to change this line to:

$streams .= $contentStream->_toString();

It will increase readability.

Copy link
Contributor Author

@Mats Mats Feb 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great to hear you plan to merge the PR next week. I agree with your comment about readability and have made the change from string casting to explicitly calling __toString().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome. Thank you.

} else {
$streams .= $contentStream->getContents();
}

$streams .= "\r\n";
}

Expand Down
33 changes: 17 additions & 16 deletions tests/FunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace tests\Http\Message\MultipartStream;

use Http\Message\MultipartStream\MultipartStreamBuilder;
use Zend\Diactoros\Stream;


/**
* @author Tobias Nyholm <[email protected]>
Expand All @@ -15,7 +15,7 @@ public function testSupportStreams()
$body = 'stream contents';

$builder = new MultipartStreamBuilder();
$builder->addResource('foobar', $this->createStream($body));
$builder->addResource('foobar', $body);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tests make sure we support a Stream. Please do not change this.


$multipartStream = (string) $builder->build();
$this->assertTrue(false !== strpos($multipartStream, $body));
Expand All @@ -33,6 +33,21 @@ public function testSupportResources()
$this->assertTrue(false !== strpos($multipartStream, 'Content-Type: image/png'));
}

public function testSupportURIResources()
{
$builder = new MultipartStreamBuilder();
$resource = 'https://raw.githubusercontent.com/php-http/multipart-stream-builder/master/tests/Resources/httplug.png';
$stream = fopen($resource, 'r');
$builder->addResource('image', $stream);
$multipartStream = (string) $builder->build();

$this->assertTrue(false !== strpos($multipartStream, 'Content-Disposition: form-data; name="image"; filename="httplug.png"'));
$this->assertTrue(false !== strpos($multipartStream, 'Content-Type: image/png'));

$uriResourceContents = file_get_contents($resource);
$this->assertContains($uriResourceContents, $multipartStream);
}

public function testResourceFilenameIsNotLocaleAware()
{
// Get current locale
Expand Down Expand Up @@ -116,18 +131,4 @@ public function testReset()
$this->assertNotEquals($boundary, $builder->getBoundary(), 'Stream should have a new boundary after reset()');
$this->assertNotEmpty($builder->getBoundary());
}

/**
* @param string $body
*
* @return Stream
*/
private function createStream($body)
{
$stream = new Stream('php://memory', 'rw');
$stream->write($body);
$stream->rewind();

return $stream;
}
}