Skip to content

Commit 963307f

Browse files
committed
Added tests
1 parent 997c6a3 commit 963307f

File tree

6 files changed

+179
-4
lines changed

6 files changed

+179
-4
lines changed

.travis.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
language: php
2+
3+
sudo: false
4+
5+
cache:
6+
directories:
7+
- $HOME/.composer/cache/files
8+
9+
php:
10+
- 5.5
11+
- 5.6
12+
- 7.0
13+
- hhvm
14+
15+
env:
16+
global:
17+
- TEST_COMMAND="composer test"
18+
19+
branches:
20+
except:
21+
- /^analysis-.*$/
22+
23+
matrix:
24+
fast_finish: true
25+
include:
26+
- php: 5.5
27+
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" COVERAGE=true TEST_COMMAND="composer test-ci"
28+
29+
before_install:
30+
- travis_retry composer self-update
31+
32+
install:
33+
- travis_retry composer update ${COMPOSER_FLAGS} --prefer-source --no-interaction
34+
35+
script:
36+
- $TEST_COMMAND
37+
38+
after_success:
39+
- if [[ "$COVERAGE" = true ]]; then wget https://scrutinizer-ci.com/ocular.phar; fi
40+
- if [[ "$COVERAGE" = true ]]; then php ocular.phar code-coverage:upload --format=php-clover build/coverage.xml; fi

composer.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,25 @@
1616
"php-http/message-factory": "^1.0",
1717
"php-http/discovery": "^0.9"
1818
},
19+
"require-dev": {
20+
"phpunit/phpunit": "^5.4",
21+
"php-http/message": "^1.0",
22+
"zendframework/zend-diactoros": "^1.3.5"
23+
},
1924
"autoload": {
2025
"psr-4": {
2126
"Http\\Message\\": "src/"
2227
}
2328
},
29+
"autoload-dev": {
30+
"psr-4": {
31+
"tests\\Http\\Message\\": "tests/"
32+
}
33+
},
34+
"scripts": {
35+
"test": "vendor/bin/phpunit",
36+
"test-ci": "vendor/bin/phpunit --coverage-text --coverage-clover=build/coverage.xml"
37+
},
2438
"extra": {
2539
"branch-alias": {
2640
"dev-master": "0.2-dev"

phpunit.xml.dist

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit bootstrap="./vendor/autoload.php"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
syntaxCheck="true">
9+
<testsuites>
10+
<testsuite name="MultipartStream tests">
11+
<directory suffix="Test.php">./tests</directory>
12+
</testsuite>
13+
</testsuites>
14+
15+
<filter>
16+
<whitelist>
17+
<directory>./</directory>
18+
<exclude>
19+
<directory>./tests</directory>
20+
<directory>./vendor</directory>
21+
</exclude>
22+
</whitelist>
23+
</filter>
24+
</phpunit>

src/MultipartStreamBuilder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function __construct(StreamFactory $streamFactory = null)
5454
*
5555
* @return MultipartStreamBuilder
5656
*/
57-
public function addResource($name, $resource, array $options)
57+
public function addResource($name, $resource, array $options = [])
5858
{
5959
$stream = $this->streamFactory->createStream($resource);
6060

@@ -68,7 +68,7 @@ public function addResource($name, $resource, array $options)
6868
$options['filename'] = null;
6969
$uri = $stream->getMetadata('uri');
7070
if (substr($uri, 0, 6) !== 'php://') {
71-
$config['filename'] = $uri;
71+
$options['filename'] = $uri;
7272
}
7373

7474
}
@@ -91,15 +91,15 @@ public function build()
9191

9292
// Add start and headers
9393
$streams .= "--{$this->getBoundary()}\r\n".
94-
$this->getHeaders($data['headers'])."\r\n\r\n";
94+
$this->getHeaders($data['headers'])."\r\n";
9595

9696
// Convert the stream to string
9797
$streams .= (string) $data['contents'];
9898
$streams .= "\r\n";
9999
}
100100

101101
// Append end
102-
$streams[] = "--{$this->getBoundary()}--\r\n";
102+
$streams .= "--{$this->getBoundary()}--\r\n";
103103

104104
return $this->streamFactory->createStream($streams);
105105
}

tests/FunctionTest.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace tests\Http\Message;
4+
5+
use Http\Message\MultipartStreamBuilder;
6+
use Zend\Diactoros\Stream;
7+
8+
class FunctionalTest extends \PHPUnit_Framework_TestCase
9+
{
10+
public function testSupportStreams()
11+
{
12+
$body = 'stream contents';
13+
14+
$builder = new MultipartStreamBuilder();
15+
$builder->addResource('foobar', $this->createStream($body));
16+
17+
$multipartStream = (string) $builder->build();
18+
$this->assertTrue(false !== strpos($multipartStream, $body));
19+
}
20+
21+
public function testSupportResources()
22+
{
23+
$resource = fopen(__DIR__.'/Resources/httplug.png', 'r');
24+
25+
$builder = new MultipartStreamBuilder();
26+
$builder->addResource('image', $resource);
27+
28+
$multipartStream = (string) $builder->build();
29+
$this->assertTrue(false !== strpos($multipartStream, 'Content-Disposition: form-data; name="image"; filename="httplug.png"'));
30+
$this->assertTrue(false !== strpos($multipartStream, 'Content-Type: image/png'));
31+
}
32+
33+
public function testHeaders()
34+
{
35+
$builder = new MultipartStreamBuilder();
36+
$builder->addResource('foobar', 'stream contents', ['headers' => ['Content-Type' => 'html/image', 'content-length'=>'4711', 'CONTENT-DISPOSITION'=>'none']]);
37+
38+
$multipartStream = (string) $builder->build();
39+
$this->assertTrue(false !== strpos($multipartStream, 'Content-Type: html/image'));
40+
$this->assertTrue(false !== strpos($multipartStream, 'content-length: 4711'));
41+
$this->assertTrue(false !== strpos($multipartStream, 'CONTENT-DISPOSITION: none'));
42+
43+
// Make sure we do not add extra headers with a different case
44+
$this->assertTrue(false === strpos($multipartStream, 'Content-Disposition:'));
45+
}
46+
47+
public function testContentLength()
48+
{
49+
$builder = new MultipartStreamBuilder();
50+
$builder->addResource('foobar', 'stream contents');
51+
52+
$multipartStream = (string) $builder->build();
53+
$this->assertTrue(false !== strpos($multipartStream, 'Content-Length: 15'));
54+
}
55+
56+
public function testFormName()
57+
{
58+
$builder = new MultipartStreamBuilder();
59+
$builder->addResource('a-formname', 'string');
60+
61+
$multipartStream = (string) $builder->build();
62+
$this->assertTrue(false !== strpos($multipartStream, 'Content-Disposition: form-data; name="a-formname"'));
63+
}
64+
65+
public function testBoundary()
66+
{
67+
$boundary = 'SpecialBoundary';
68+
$builder = new MultipartStreamBuilder();
69+
$builder->addResource('content0', 'string');
70+
$builder->setBoundary($boundary);
71+
72+
$multipartStream = (string) $builder->build();
73+
$this->assertEquals(2, substr_count($multipartStream, $boundary));
74+
75+
76+
$builder->addResource('content1', 'string');
77+
$builder->addResource('content2', 'string');
78+
$builder->addResource('content3', 'string');
79+
80+
$multipartStream = (string) $builder->build();
81+
$this->assertEquals(5, substr_count($multipartStream, $boundary));
82+
}
83+
84+
/**
85+
* @param $body
86+
*
87+
* @return Stream
88+
*/
89+
private function createStream($body)
90+
{
91+
$stream = new Stream('php://memory', 'rw');
92+
$stream->write($body);
93+
$stream->rewind();
94+
95+
return $stream;
96+
}
97+
}

tests/Resources/httplug.png

13.3 KB
Loading

0 commit comments

Comments
 (0)