@@ -376,6 +376,162 @@ public function testIsOkWithRedirects()
376376 $ this ->assertTrue ($ response ->isOK ());
377377 }
378378
379+ public function testCallGetWithParams ()
380+ {
381+ $ this ->withRoutes ([
382+ [
383+ 'get ' ,
384+ 'home ' ,
385+ static fn () => json_encode (Services ::request ()->getGet ()),
386+ ],
387+ ]);
388+ $ data = [
389+ 'true ' => true ,
390+ 'false ' => false ,
391+ 'int ' => 2 ,
392+ 'null ' => null ,
393+ 'float ' => 1.23 ,
394+ 'string ' => 'foo ' ,
395+ ];
396+ $ response = $ this ->get ('home ' , $ data );
397+
398+ $ response ->assertOK ();
399+ $ this ->assertStringContainsString (
400+ // All GET values will be strings.
401+ '{"true":"1","false":"","int":"2","null":"","float":"1.23","string":"foo"} ' ,
402+ $ response ->getBody ()
403+ );
404+ }
405+
406+ public function testCallGetWithParamsAndREQUEST ()
407+ {
408+ $ this ->withRoutes ([
409+ [
410+ 'get ' ,
411+ 'home ' ,
412+ static fn () => json_encode (Services ::request ()->fetchGlobal ('request ' )),
413+ ],
414+ ]);
415+ $ data = [
416+ 'true ' => true ,
417+ 'false ' => false ,
418+ 'int ' => 2 ,
419+ 'null ' => null ,
420+ 'float ' => 1.23 ,
421+ 'string ' => 'foo ' ,
422+ ];
423+ $ response = $ this ->get ('home ' , $ data );
424+
425+ $ response ->assertOK ();
426+ $ this ->assertStringContainsString (
427+ // All GET values will be strings.
428+ '{"true":"1","false":"","int":"2","null":"","float":"1.23","string":"foo"} ' ,
429+ $ response ->getBody ()
430+ );
431+ }
432+
433+ public function testCallPostWithParams ()
434+ {
435+ $ this ->withRoutes ([
436+ [
437+ 'post ' ,
438+ 'home ' ,
439+ static fn () => json_encode (Services ::request ()->getPost ()),
440+ ],
441+ ]);
442+ $ data = [
443+ 'true ' => true ,
444+ 'false ' => false ,
445+ 'int ' => 2 ,
446+ 'null ' => null ,
447+ 'float ' => 1.23 ,
448+ 'string ' => 'foo ' ,
449+ ];
450+ $ response = $ this ->post ('home ' , $ data );
451+
452+ $ response ->assertOK ();
453+ $ this ->assertStringContainsString (
454+ // All POST values will be strings.
455+ '{"true":"1","false":"","int":"2","null":"","float":"1.23","string":"foo"} ' ,
456+ $ response ->getBody ()
457+ );
458+ }
459+
460+ public function testCallPostWithParamsAndREQUEST ()
461+ {
462+ $ this ->withRoutes ([
463+ [
464+ 'post ' ,
465+ 'home ' ,
466+ static fn () => json_encode (Services ::request ()->fetchGlobal ('request ' )),
467+ ],
468+ ]);
469+ $ data = [
470+ 'true ' => true ,
471+ 'false ' => false ,
472+ 'int ' => 2 ,
473+ 'null ' => null ,
474+ 'float ' => 1.23 ,
475+ 'string ' => 'foo ' ,
476+ ];
477+ $ response = $ this ->post ('home ' , $ data );
478+
479+ $ response ->assertOK ();
480+ $ this ->assertStringContainsString (
481+ // All POST values will be strings.
482+ '{"true":"1","false":"","int":"2","null":"","float":"1.23","string":"foo"} ' ,
483+ $ response ->getBody ()
484+ );
485+ }
486+
487+ public function testCallPutWithJsonRequest ()
488+ {
489+ $ this ->withRoutes ([
490+ [
491+ 'put ' ,
492+ 'home ' ,
493+ '\Tests\Support\Controllers\Popcorn::echoJson ' ,
494+ ],
495+ ]);
496+ $ data = [
497+ 'true ' => true ,
498+ 'false ' => false ,
499+ 'int ' => 2 ,
500+ 'null ' => null ,
501+ 'float ' => 1.23 ,
502+ 'string ' => 'foo ' ,
503+ ];
504+ $ response = $ this ->withBodyFormat ('json ' )
505+ ->call ('put ' , 'home ' , $ data );
506+
507+ $ response ->assertOK ();
508+ $ response ->assertJSONExact ($ data );
509+ }
510+
511+ public function testCallPutWithJsonRequestAndREQUEST ()
512+ {
513+ $ this ->withRoutes ([
514+ [
515+ 'put ' ,
516+ 'home ' ,
517+ static fn () => json_encode (Services ::request ()->fetchGlobal ('request ' )),
518+ ],
519+ ]);
520+ $ data = [
521+ 'true ' => true ,
522+ 'false ' => false ,
523+ 'int ' => 2 ,
524+ 'null ' => null ,
525+ 'float ' => 1.23 ,
526+ 'string ' => 'foo ' ,
527+ ];
528+ $ response = $ this ->withBodyFormat ('json ' )
529+ ->call ('put ' , 'home ' , $ data );
530+
531+ $ response ->assertOK ();
532+ $ this ->assertStringContainsString ('[] ' , $ response ->getBody ());
533+ }
534+
379535 public function testCallWithJsonRequest ()
380536 {
381537 $ this ->withRoutes ([
@@ -385,9 +541,19 @@ public function testCallWithJsonRequest()
385541 '\Tests\Support\Controllers\Popcorn::echoJson ' ,
386542 ],
387543 ]);
388- $ response = $ this ->withBodyFormat ('json ' )->call ('post ' , 'home ' , ['foo ' => 'bar ' ]);
544+ $ data = [
545+ 'true ' => true ,
546+ 'false ' => false ,
547+ 'int ' => 2 ,
548+ 'null ' => null ,
549+ 'float ' => 1.23 ,
550+ 'string ' => 'foo ' ,
551+ ];
552+ $ response = $ this ->withBodyFormat ('json ' )
553+ ->call ('post ' , 'home ' , $ data );
554+
389555 $ response ->assertOK ();
390- $ response ->assertJSONExact ([ ' foo ' => ' bar ' ] );
556+ $ response ->assertJSONExact ($ data );
391557 }
392558
393559 public function testSetupRequestBodyWithParams ()
@@ -400,14 +566,39 @@ public function testSetupRequestBodyWithParams()
400566 $ this ->assertSame ('application/json ' , $ request ->header ('Content-Type ' )->getValue ());
401567 }
402568
569+ public function testSetupJSONRequestBodyWithBody ()
570+ {
571+ $ request = $ this ->setupRequest ('post ' , 'home ' );
572+ $ request = $ this ->withBodyFormat ('json ' )
573+ ->withBody (json_encode (['foo1 ' => 'bar1 ' ]))
574+ ->setRequestBody ($ request );
575+
576+ $ this ->assertJsonStringEqualsJsonString (
577+ json_encode (['foo1 ' => 'bar1 ' ]),
578+ $ request ->getBody ()
579+ );
580+ $ this ->assertSame (
581+ 'application/json ' ,
582+ $ request ->header ('Content-Type ' )->getValue ()
583+ );
584+ }
585+
403586 public function testSetupRequestBodyWithXml ()
404587 {
405588 $ request = $ this ->setupRequest ('post ' , 'home ' );
406589
407- $ request = $ this ->withBodyFormat ('xml ' )->setRequestBody ($ request , ['foo ' => 'bar ' ]);
590+ $ data = [
591+ 'true ' => true ,
592+ 'false ' => false ,
593+ 'int ' => 2 ,
594+ 'null ' => null ,
595+ 'float ' => 1.23 ,
596+ 'string ' => 'foo ' ,
597+ ];
598+ $ request = $ this ->withBodyFormat ('xml ' )->setRequestBody ($ request , $ data );
408599
409600 $ expectedXml = '<?xml version="1.0"?>
410- <response><foo>bar</ foo></response>
601+ <response><true>1</true><false/><int>2</int><null/><float>1.23</float><string> foo</string ></response>
411602 ' ;
412603
413604 $ this ->assertSame ($ expectedXml , $ request ->getBody ());
0 commit comments