88use Gt \Http \ServerRequest ;
99use Gt \Http \Uri ;
1010use Gt \Input \Input ;
11+ use Gt \Input \InputData \BodyInputData ;
1112use Gt \Input \InputData \Datum \FileUpload ;
1213use Gt \Input \InputData \Datum \InputDatum ;
1314use Gt \Input \InputData \FileUploadInputData ;
15+ use Gt \Input \InputData \InputData ;
1416use PHPUnit \Framework \MockObject \MockObject ;
1517use PHPUnit \Framework \TestCase ;
1618
@@ -174,6 +176,47 @@ public function testWithUploadedFilesEmptyToFull() {
174176 self ::assertCount (2 , $ sutFull ->getUploadedFiles ());
175177 }
176178
179+ public function testGetParsedBodyEmpty () {
180+ $ sut = self ::getServerRequest (
181+ "get " ,
182+ "/example "
183+ );
184+ self ::assertEmpty ($ sut ->getParsedBody ());
185+ }
186+
187+ public function testGetParsedBody () {
188+ $ sut = self ::getServerRequest (
189+ "post " ,
190+ "/example " ,
191+ [],
192+ [],
193+ [
194+ "post " => [
195+ "key1 " => "value1 " ,
196+ "key2 " => "value2 " ,
197+ ]
198+ ]
199+ );
200+
201+ /** @var InputData $inputData */
202+ $ inputData = $ sut ->getParsedBody ();
203+ self ::assertInstanceOf (InputData::class, $ inputData );
204+ self ::assertCount (2 , $ inputData ->asArray ());
205+ }
206+
207+ public function testWithParsedBody () {
208+ $ sut = self ::getServerRequest ();
209+ $ inputData = $ sut ->getParsedBody ();
210+ self ::assertEmpty ($ inputData ->asArray ());
211+
212+ $ inputDatumArray = [];
213+ $ inputDatumArray []= self ::createMock (InputDatum::class);
214+ $ inputDatumArray []= self ::createMock (InputDatum::class);
215+
216+ $ sut = $ sut ->withParsedBody ($ inputDatumArray );
217+ self ::assertCount (2 , $ sut ->getParsedBody ()->asArray ());
218+ }
219+
177220 protected function getServerRequest (
178221 string $ method = null ,
179222 string $ uri = null ,
@@ -243,9 +286,19 @@ protected function getMockInput(
243286 array $ post = [],
244287 array $ files = []
245288 ):MockObject {
289+ $ bodyArray = [];
290+ foreach ($ post as $ key => $ value ) {
291+ $ bodyArray []= self ::createMock (InputDatum::class);
292+ }
293+ $ bodyParameters = self ::createMock (BodyInputData::class);
294+ $ bodyParameters ->method ("asArray " )
295+ ->willReturnCallback (function ()use (&$ bodyArray ) {
296+ return $ bodyArray ;
297+ });
298+
246299 $ fileArray = [];
247300 foreach ($ files as $ file ) {
248- $ fileArray []= $ this -> createMock (FileUpload::class);
301+ $ fileArray []= self :: createMock (FileUpload::class);
249302 }
250303 $ fileUploadParameters = self ::createMock (FileUploadInputData::class);
251304 $ fileUploadParameters ->method ("getKeys " )
@@ -266,16 +319,26 @@ protected function getMockInput(
266319
267320 $ mock = self ::createMock (Input::class);
268321 $ mock ->method ("getAll " )
269- ->with (Input::DATA_FILES )
270- ->willReturn ($ fileUploadParameters );
322+ ->willReturnCallback (function ($ method )use ($ bodyParameters , $ fileUploadParameters ) {
323+ if ($ method === Input::DATA_FILES ) {
324+ return $ fileUploadParameters ;
325+ }
326+ if ($ method === Input::DATA_BODY ) {
327+ return $ bodyParameters ;
328+ }
329+ });
271330 $ mock ->method ("add " )
272- ->willReturnCallback (function (string $ key , InputDatum $ datum , string $ method )use (&$ fileArray ) {
331+ ->willReturnCallback (function (string $ key , InputDatum $ datum , string $ method )use (&$ bodyArray , & $ fileArray ) {
273332 if ($ method === Input::DATA_FILES ) {
274333 /** @var FileUpload $datum */
275334 $ fileArray [$ key ] = [
276335 "name " => $ datum ->getClientFilename ()
277336 ];
278337 }
338+ if ($ method === INPUT ::DATA_BODY ) {
339+ /** @var InputDatum $datum */
340+ $ bodyArray [$ key ] = $ datum ;
341+ }
279342 });
280343 return $ mock ;
281344 }
0 commit comments