@@ -27,17 +27,17 @@ public function getBrowser(array $server = [], History $history = null, CookieJa
2727 /**
2828 * @dataProvider validContentTypes
2929 */
30- public function testRequestHeaders (array $ request , array $ exepectedCall )
30+ public function testRequestHeaders (array $ requestArguments , array $ expectedArguments )
3131 {
3232 $ client = $ this ->createMock (HttpClientInterface::class);
3333 $ client
3434 ->expects ($ this ->once ())
3535 ->method ('request ' )
36- ->with (...$ exepectedCall )
36+ ->with (...$ expectedArguments )
3737 ->willReturn ($ this ->createMock (ResponseInterface::class));
3838
3939 $ browser = new HttpBrowser ($ client );
40- $ browser ->request (...$ request );
40+ $ browser ->request (...$ requestArguments );
4141 }
4242
4343 public function validContentTypes ()
@@ -61,7 +61,7 @@ public function validContentTypes()
6161 ];
6262 }
6363
64- public function testMultiPartRequest ()
64+ public function testMultiPartRequestWithSingleFile ()
6565 {
6666 $ client = $ this ->createMock (HttpClientInterface::class);
6767 $ client
@@ -81,4 +81,90 @@ public function testMultiPartRequest()
8181 file_put_contents ($ path , 'my_file ' );
8282 $ browser ->request ('POST ' , 'http://example.com/ ' , [], ['file ' => ['tmp_name ' => $ path , 'name ' => 'foo ' ]]);
8383 }
84+
85+ public function testMultiPartRequestWithNormalFlatArray ()
86+ {
87+ $ client = $ this ->createMock (HttpClientInterface::class);
88+ $ this ->expectClientToSendRequestWithFiles ($ client , ['file1_content ' , 'file2_content ' ]);
89+
90+ $ browser = new HttpBrowser ($ client );
91+ $ browser ->request ('POST ' , 'http://example.com/ ' , [], [
92+ 'file1 ' => $ this ->getUploadedFile ('file1 ' ),
93+ 'file2 ' => $ this ->getUploadedFile ('file2 ' ),
94+ ]);
95+ }
96+
97+ public function testMultiPartRequestWithNormalNestedArray ()
98+ {
99+ $ client = $ this ->createMock (HttpClientInterface::class);
100+ $ this ->expectClientToSendRequestWithFiles ($ client , ['file1_content ' , 'file2_content ' ]);
101+
102+ $ browser = new HttpBrowser ($ client );
103+ $ browser ->request ('POST ' , 'http://example.com/ ' , [], [
104+ 'level1 ' => [
105+ 'level2 ' => [
106+ 'file1 ' => $ this ->getUploadedFile ('file1 ' ),
107+ 'file2 ' => $ this ->getUploadedFile ('file2 ' ),
108+ ],
109+ ],
110+ ]);
111+ }
112+
113+ public function testMultiPartRequestWithBracketedArray ()
114+ {
115+ $ client = $ this ->createMock (HttpClientInterface::class);
116+ $ this ->expectClientToSendRequestWithFiles ($ client , ['file1_content ' , 'file2_content ' ]);
117+
118+ $ browser = new HttpBrowser ($ client );
119+ $ browser ->request ('POST ' , 'http://example.com/ ' , [], [
120+ 'form[file1] ' => $ this ->getUploadedFile ('file1 ' ),
121+ 'form[file2] ' => $ this ->getUploadedFile ('file2 ' ),
122+ ]);
123+ }
124+
125+ public function testMultiPartRequestWithInvalidItem ()
126+ {
127+ $ client = $ this ->createMock (HttpClientInterface::class);
128+ $ this ->expectClientToSendRequestWithFiles ($ client , ['file1_content ' ]);
129+
130+ $ browser = new HttpBrowser ($ client );
131+ $ browser ->request ('POST ' , 'http://example.com/ ' , [], [
132+ 'file1 ' => $ this ->getUploadedFile ('file1 ' ),
133+ 'file2 ' => 'INVALID ' ,
134+ ]);
135+ }
136+
137+ private function uploadFile (string $ data ): string
138+ {
139+ $ path = tempnam (sys_get_temp_dir (), 'http ' );
140+ file_put_contents ($ path , $ data );
141+
142+ return $ path ;
143+ }
144+
145+ private function getUploadedFile (string $ name ): array
146+ {
147+ return [
148+ 'tmp_name ' => $ this ->uploadFile ($ name .'_content ' ),
149+ 'name ' => $ name .'_name ' ,
150+ ];
151+ }
152+
153+ protected function expectClientToSendRequestWithFiles (HttpClientInterface $ client , $ fileContents )
154+ {
155+ $ client
156+ ->expects ($ this ->once ())
157+ ->method ('request ' )
158+ ->with ('POST ' , 'http://example.com/ ' , $ this ->callback (function ($ options ) use ($ fileContents ) {
159+ $ this ->assertStringContainsString ('Content-Type: multipart/form-data ' , implode ('' , $ options ['headers ' ]));
160+ $ this ->assertInstanceOf ('\Generator ' , $ options ['body ' ]);
161+ $ body = implode ('' , iterator_to_array ($ options ['body ' ], false ));
162+ foreach ($ fileContents as $ content ) {
163+ $ this ->assertStringContainsString ($ content , $ body );
164+ }
165+
166+ return true ;
167+ }))
168+ ->willReturn ($ this ->createMock (ResponseInterface::class));
169+ }
84170}
0 commit comments