@@ -27,17 +27,17 @@ public function getBrowser(array $server = [], History $history = null, CookieJa
27
27
/**
28
28
* @dataProvider validContentTypes
29
29
*/
30
- public function testRequestHeaders (array $ request , array $ exepectedCall )
30
+ public function testRequestHeaders (array $ requestArguments , array $ expectedArguments )
31
31
{
32
32
$ client = $ this ->createMock (HttpClientInterface::class);
33
33
$ client
34
34
->expects ($ this ->once ())
35
35
->method ('request ' )
36
- ->with (...$ exepectedCall )
36
+ ->with (...$ expectedArguments )
37
37
->willReturn ($ this ->createMock (ResponseInterface::class));
38
38
39
39
$ browser = new HttpBrowser ($ client );
40
- $ browser ->request (...$ request );
40
+ $ browser ->request (...$ requestArguments );
41
41
}
42
42
43
43
public function validContentTypes ()
@@ -61,7 +61,7 @@ public function validContentTypes()
61
61
];
62
62
}
63
63
64
- public function testMultiPartRequest ()
64
+ public function testMultiPartRequestWithSingleFile ()
65
65
{
66
66
$ client = $ this ->createMock (HttpClientInterface::class);
67
67
$ client
@@ -81,4 +81,90 @@ public function testMultiPartRequest()
81
81
file_put_contents ($ path , 'my_file ' );
82
82
$ browser ->request ('POST ' , 'http://example.com/ ' , [], ['file ' => ['tmp_name ' => $ path , 'name ' => 'foo ' ]]);
83
83
}
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
+ }
84
170
}
0 commit comments