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+ }
0 commit comments