21
21
use Symfony \Component \Form \Form ;
22
22
use Symfony \Component \Form \FormBuilderInterface ;
23
23
use Symfony \Component \Form \FormConfigInterface ;
24
+ use Symfony \Component \Form \FormError ;
24
25
use Symfony \Component \Form \FormFactoryInterface ;
25
26
use Symfony \Component \Form \FormInterface ;
27
+ use Symfony \Component \Form \FormView ;
26
28
use Symfony \Component \HttpFoundation \BinaryFileResponse ;
27
29
use Symfony \Component \HttpFoundation \File \Exception \FileNotFoundException ;
28
30
use Symfony \Component \HttpFoundation \File \File ;
@@ -411,102 +413,62 @@ public function testRenderTwig()
411
413
$ this ->assertEquals ('bar ' , $ controller ->render ('foo ' )->getContent ());
412
414
}
413
415
414
- public function testStreamTwig ()
416
+ public function testRenderFormNew ()
415
417
{
416
- $ twig = $ this ->createMock (Environment::class);
418
+ $ formView = new FormView ();
419
+
420
+ $ form = $ this ->getMockBuilder (FormInterface::class)->getMock ();
421
+ $ form ->expects ($ this ->once ())->method ('createView ' )->willReturn ($ formView );
422
+
423
+ $ twig = $ this ->getMockBuilder (Environment::class)->disableOriginalConstructor ()->getMock ();
424
+ $ twig ->expects ($ this ->once ())->method ('render ' )->with ('foo ' , ['form ' => $ formView , 'bar ' => 'bar ' ])->willReturn ('bar ' );
417
425
418
426
$ container = new Container ();
419
427
$ container ->set ('twig ' , $ twig );
420
428
421
429
$ controller = $ this ->createController ();
422
430
$ controller ->setContainer ($ container );
423
431
424
- $ this ->assertInstanceOf (StreamedResponse::class, $ controller ->stream ('foo ' ));
425
- }
426
-
427
- public function testHandleFormNotSubmitted ()
428
- {
429
- $ form = $ this ->createMock (FormInterface::class);
430
- $ form ->expects ($ this ->once ())->method ('isSubmitted ' )->willReturn (false );
431
-
432
- $ controller = $ this ->createController ();
433
- $ response = $ controller ->handleForm (
434
- $ form ,
435
- Request::create ('https://example.com ' ),
436
- function (FormInterface $ form , $ data , Request $ request ): Response {
437
- return new RedirectResponse ('https://example.com/redir ' , Response::HTTP_SEE_OTHER );
438
- },
439
- function (FormInterface $ form , $ data , Request $ request ): Response {
440
- return new Response ('rendered ' );
441
- }
442
- );
432
+ $ response = $ controller ->renderForm ('foo ' , $ form , ['bar ' => 'bar ' ]);
443
433
444
434
$ this ->assertTrue ($ response ->isSuccessful ());
445
- $ this ->assertSame ('rendered ' , $ response ->getContent ());
435
+ $ this ->assertSame ('bar ' , $ response ->getContent ());
446
436
}
447
437
448
- public function testHandleFormInvalid ()
438
+ public function testRenderFormSubmittedAndInvalid ()
449
439
{
450
- $ form = $ this ->createMock (FormInterface::class);
440
+ $ formView = new FormView ();
441
+
442
+ $ form = $ this ->getMockBuilder (FormInterface::class)->getMock ();
443
+ $ form ->expects ($ this ->once ())->method ('createView ' )->willReturn ($ formView );
451
444
$ form ->expects ($ this ->once ())->method ('isSubmitted ' )->willReturn (true );
452
445
$ form ->expects ($ this ->once ())->method ('isValid ' )->willReturn (false );
453
446
454
- $ controller = $ this ->createController ();
455
- $ response = $ controller ->handleForm (
456
- $ form ,
457
- Request::create ('https://example.com ' ),
458
- function (FormInterface $ form , $ data , Request $ request ): Response {
459
- return new RedirectResponse ('https://example.com/redir ' , Response::HTTP_SEE_OTHER );
460
- },
461
- function (FormInterface $ form , $ data , Request $ request ): Response {
462
- return new Response ('rendered ' );
463
- }
464
- );
465
-
466
- $ this ->assertSame (Response::HTTP_UNPROCESSABLE_ENTITY , $ response ->getStatusCode ());
467
- $ this ->assertSame ('rendered ' , $ response ->getContent ());
468
- }
447
+ $ twig = $ this ->getMockBuilder (Environment::class)->disableOriginalConstructor ()->getMock ();
448
+ $ twig ->expects ($ this ->once ())->method ('render ' )->with ('foo ' , ['myForm ' => $ formView , 'bar ' => 'bar ' ])->willReturn ('bar ' );
469
449
470
- public function testHandleFormValid ()
471
- {
472
- $ form = $ this ->createMock (FormInterface::class);
473
- $ form ->expects ($ this ->once ())->method ('isSubmitted ' )->willReturn (true );
474
- $ form ->expects ($ this ->once ())->method ('isValid ' )->willReturn (true );
450
+ $ container = new Container ();
451
+ $ container ->set ('twig ' , $ twig );
475
452
476
453
$ controller = $ this ->createController ();
477
- $ response = $ controller ->handleForm (
478
- $ form ,
479
- Request::create ('https://example.com ' ),
480
- function (FormInterface $ form , $ data , Request $ request ): Response {
481
- return new RedirectResponse ('https://example.com/redir ' , Response::HTTP_SEE_OTHER );
482
- },
483
- function (FormInterface $ form , $ data , Request $ request ): Response {
484
- return new Response ('rendered ' );
485
- }
486
- );
454
+ $ controller ->setContainer ($ container );
487
455
488
- $ this ->assertInstanceOf (RedirectResponse::class, $ response );
489
- $ this ->assertSame (Response::HTTP_SEE_OTHER , $ response ->getStatusCode ());
490
- $ this ->assertSame ('https://example.com/redir ' , $ response ->getTargetUrl ());
491
- }
456
+ $ response = $ controller ->renderForm ('foo ' , $ form , ['bar ' => 'bar ' ], null , 'myForm ' );
492
457
493
- public function testHandleFormTypeError ()
458
+ $ this ->assertSame (422 , $ response ->getStatusCode ());
459
+ $ this ->assertSame ('bar ' , $ response ->getContent ());
460
+ }
461
+ public function testStreamTwig ()
494
462
{
495
- $ form = $ this ->createMock (FormInterface::class);
496
- $ form ->expects ($ this ->once ())->method ('isSubmitted ' )->willReturn (true );
497
- $ form ->expects ($ this ->once ())->method ('isValid ' )->willReturn (false );
463
+ $ twig = $ this ->createMock (Environment::class);
498
464
499
- $ controller = $ this ->createController ();
465
+ $ container = new Container ();
466
+ $ container ->set ('twig ' , $ twig );
500
467
501
- $ this ->expectException (\TypeError::class );
502
- $ this -> expectExceptionMessage ( ' The "$render" callable passed to "Symfony\Bundle\FrameworkBundle\Tests\Controller\TestAbstractController::handleForm()" must return a Response, "string" returned. ' );
468
+ $ controller = $ this ->createController ( );
469
+ $ controller -> setContainer ( $ container );
503
470
504
- $ response = $ controller ->handleForm (
505
- $ form ,
506
- Request::create ('https://example.com ' ),
507
- function () { return 'abc ' ; },
508
- function () { return 'abc ' ; }
509
- );
471
+ $ this ->assertInstanceOf (StreamedResponse::class, $ controller ->stream ('foo ' ));
510
472
}
511
473
512
474
public function testRedirectToRoute ()
0 commit comments