@@ -358,6 +358,162 @@ public function testIsOkWithRedirects()
358358 $ this ->assertTrue ($ response ->isOK ());
359359 }
360360
361+ public function testCallGetWithParams ()
362+ {
363+ $ this ->withRoutes ([
364+ [
365+ 'get ' ,
366+ 'home ' ,
367+ static fn () => json_encode (Services::request ()->getGet ()),
368+ ],
369+ ]);
370+ $ data = [
371+ 'true ' => true ,
372+ 'false ' => false ,
373+ 'int ' => 2 ,
374+ 'null ' => null ,
375+ 'float ' => 1.23 ,
376+ 'string ' => 'foo ' ,
377+ ];
378+ $ response = $ this ->get ('home ' , $ data );
379+
380+ $ response ->assertOK ();
381+ $ this ->assertStringContainsString (
382+ // All GET values will be strings.
383+ '{"true":"1","false":"","int":"2","null":"","float":"1.23","string":"foo"} ' ,
384+ $ response ->getBody ()
385+ );
386+ }
387+
388+ public function testCallGetWithParamsAndREQUEST ()
389+ {
390+ $ this ->withRoutes ([
391+ [
392+ 'get ' ,
393+ 'home ' ,
394+ static fn () => json_encode (Services::request ()->fetchGlobal ('request ' )),
395+ ],
396+ ]);
397+ $ data = [
398+ 'true ' => true ,
399+ 'false ' => false ,
400+ 'int ' => 2 ,
401+ 'null ' => null ,
402+ 'float ' => 1.23 ,
403+ 'string ' => 'foo ' ,
404+ ];
405+ $ response = $ this ->get ('home ' , $ data );
406+
407+ $ response ->assertOK ();
408+ $ this ->assertStringContainsString (
409+ // All GET values will be strings.
410+ '{"true":"1","false":"","int":"2","null":"","float":"1.23","string":"foo"} ' ,
411+ $ response ->getBody ()
412+ );
413+ }
414+
415+ public function testCallPostWithParams ()
416+ {
417+ $ this ->withRoutes ([
418+ [
419+ 'post ' ,
420+ 'home ' ,
421+ static fn () => json_encode (Services::request ()->getPost ()),
422+ ],
423+ ]);
424+ $ data = [
425+ 'true ' => true ,
426+ 'false ' => false ,
427+ 'int ' => 2 ,
428+ 'null ' => null ,
429+ 'float ' => 1.23 ,
430+ 'string ' => 'foo ' ,
431+ ];
432+ $ response = $ this ->post ('home ' , $ data );
433+
434+ $ response ->assertOK ();
435+ $ this ->assertStringContainsString (
436+ // All POST values will be strings.
437+ '{"true":"1","false":"","int":"2","null":"","float":"1.23","string":"foo"} ' ,
438+ $ response ->getBody ()
439+ );
440+ }
441+
442+ public function testCallPostWithParamsAndREQUEST ()
443+ {
444+ $ this ->withRoutes ([
445+ [
446+ 'post ' ,
447+ 'home ' ,
448+ static fn () => json_encode (Services::request ()->fetchGlobal ('request ' )),
449+ ],
450+ ]);
451+ $ data = [
452+ 'true ' => true ,
453+ 'false ' => false ,
454+ 'int ' => 2 ,
455+ 'null ' => null ,
456+ 'float ' => 1.23 ,
457+ 'string ' => 'foo ' ,
458+ ];
459+ $ response = $ this ->post ('home ' , $ data );
460+
461+ $ response ->assertOK ();
462+ $ this ->assertStringContainsString (
463+ // All POST values will be strings.
464+ '{"true":"1","false":"","int":"2","null":"","float":"1.23","string":"foo"} ' ,
465+ $ response ->getBody ()
466+ );
467+ }
468+
469+ public function testCallPutWithJsonRequest ()
470+ {
471+ $ this ->withRoutes ([
472+ [
473+ 'put ' ,
474+ 'home ' ,
475+ '\Tests\Support\Controllers\Popcorn::echoJson ' ,
476+ ],
477+ ]);
478+ $ data = [
479+ 'true ' => true ,
480+ 'false ' => false ,
481+ 'int ' => 2 ,
482+ 'null ' => null ,
483+ 'float ' => 1.23 ,
484+ 'string ' => 'foo ' ,
485+ ];
486+ $ response = $ this ->withBodyFormat ('json ' )
487+ ->call ('put ' , 'home ' , $ data );
488+
489+ $ response ->assertOK ();
490+ $ response ->assertJSONExact ($ data );
491+ }
492+
493+ public function testCallPutWithJsonRequestAndREQUEST ()
494+ {
495+ $ this ->withRoutes ([
496+ [
497+ 'put ' ,
498+ 'home ' ,
499+ static fn () => json_encode (Services::request ()->fetchGlobal ('request ' )),
500+ ],
501+ ]);
502+ $ data = [
503+ 'true ' => true ,
504+ 'false ' => false ,
505+ 'int ' => 2 ,
506+ 'null ' => null ,
507+ 'float ' => 1.23 ,
508+ 'string ' => 'foo ' ,
509+ ];
510+ $ response = $ this ->withBodyFormat ('json ' )
511+ ->call ('put ' , 'home ' , $ data );
512+
513+ $ response ->assertOK ();
514+ $ this ->assertStringContainsString ('[] ' , $ response ->getBody ());
515+ }
516+
361517 public function testCallWithJsonRequest ()
362518 {
363519 $ this ->withRoutes ([
@@ -367,9 +523,19 @@ public function testCallWithJsonRequest()
367523 '\Tests\Support\Controllers\Popcorn::echoJson ' ,
368524 ],
369525 ]);
370- $ response = $ this ->withBodyFormat ('json ' )->call ('post ' , 'home ' , ['foo ' => 'bar ' ]);
526+ $ data = [
527+ 'true ' => true ,
528+ 'false ' => false ,
529+ 'int ' => 2 ,
530+ 'null ' => null ,
531+ 'float ' => 1.23 ,
532+ 'string ' => 'foo ' ,
533+ ];
534+ $ response = $ this ->withBodyFormat ('json ' )
535+ ->call ('post ' , 'home ' , $ data );
536+
371537 $ response ->assertOK ();
372- $ response ->assertJSONExact ([ ' foo ' => ' bar ' ] );
538+ $ response ->assertJSONExact ($ data );
373539 }
374540
375541 public function testSetupRequestBodyWithParams ()
@@ -382,14 +548,39 @@ public function testSetupRequestBodyWithParams()
382548 $ this ->assertSame ('application/json ' , $ request ->header ('Content-Type ' )->getValue ());
383549 }
384550
551+ public function testSetupJSONRequestBodyWithBody ()
552+ {
553+ $ request = $ this ->setupRequest ('post ' , 'home ' );
554+ $ request = $ this ->withBodyFormat ('json ' )
555+ ->withBody (json_encode (['foo1 ' => 'bar1 ' ]))
556+ ->setRequestBody ($ request );
557+
558+ $ this ->assertJsonStringEqualsJsonString (
559+ json_encode (['foo1 ' => 'bar1 ' ]),
560+ $ request ->getBody ()
561+ );
562+ $ this ->assertSame (
563+ 'application/json ' ,
564+ $ request ->header ('Content-Type ' )->getValue ()
565+ );
566+ }
567+
385568 public function testSetupRequestBodyWithXml ()
386569 {
387570 $ request = $ this ->setupRequest ('post ' , 'home ' );
388571
389- $ request = $ this ->withBodyFormat ('xml ' )->setRequestBody ($ request , ['foo ' => 'bar ' ]);
572+ $ data = [
573+ 'true ' => true ,
574+ 'false ' => false ,
575+ 'int ' => 2 ,
576+ 'null ' => null ,
577+ 'float ' => 1.23 ,
578+ 'string ' => 'foo ' ,
579+ ];
580+ $ request = $ this ->withBodyFormat ('xml ' )->setRequestBody ($ request , $ data );
390581
391582 $ expectedXml = '<?xml version="1.0"?>
392- <response><foo>bar</ foo></response>
583+ <response><true>1</true><false/><int>2</int><null/><float>1.23</float><string> foo</string ></response>
393584 ' ;
394585
395586 $ this ->assertSame ($ expectedXml , $ request ->getBody ());
0 commit comments