18
18
use Symfony \Component \DependencyInjection \ParameterBag \FrozenParameterBag ;
19
19
use Symfony \Component \Form \Form ;
20
20
use Symfony \Component \Form \FormConfigInterface ;
21
+ use Symfony \Component \Form \FormInterface ;
22
+ use Symfony \Component \Form \FormView ;
21
23
use Symfony \Component \HttpFoundation \BinaryFileResponse ;
22
24
use Symfony \Component \HttpFoundation \File \File ;
23
25
use Symfony \Component \HttpFoundation \JsonResponse ;
31
33
use Symfony \Component \Security \Core \User \User ;
32
34
use Symfony \Component \Serializer \SerializerInterface ;
33
35
use Symfony \Component \WebLink \Link ;
36
+ use Twig \Environment ;
34
37
35
38
class AbstractControllerTest extends TestCase
36
39
{
@@ -371,7 +374,7 @@ public function testdenyAccessUnlessGranted()
371
374
372
375
public function testRenderViewTwig ()
373
376
{
374
- $ twig = $ this ->getMockBuilder (\ Twig \ Environment::class)->disableOriginalConstructor ()->getMock ();
377
+ $ twig = $ this ->getMockBuilder (Environment::class)->disableOriginalConstructor ()->getMock ();
375
378
$ twig ->expects ($ this ->once ())->method ('render ' )->willReturn ('bar ' );
376
379
377
380
$ container = new Container ();
@@ -385,7 +388,7 @@ public function testRenderViewTwig()
385
388
386
389
public function testRenderTwig ()
387
390
{
388
- $ twig = $ this ->getMockBuilder (\ Twig \ Environment::class)->disableOriginalConstructor ()->getMock ();
391
+ $ twig = $ this ->getMockBuilder (Environment::class)->disableOriginalConstructor ()->getMock ();
389
392
$ twig ->expects ($ this ->once ())->method ('render ' )->willReturn ('bar ' );
390
393
391
394
$ container = new Container ();
@@ -399,7 +402,7 @@ public function testRenderTwig()
399
402
400
403
public function testStreamTwig ()
401
404
{
402
- $ twig = $ this ->getMockBuilder (\ Twig \ Environment::class)->disableOriginalConstructor ()->getMock ();
405
+ $ twig = $ this ->getMockBuilder (Environment::class)->disableOriginalConstructor ()->getMock ();
403
406
404
407
$ container = new Container ();
405
408
$ container ->set ('twig ' , $ twig );
@@ -410,6 +413,52 @@ public function testStreamTwig()
410
413
$ this ->assertInstanceOf (\Symfony \Component \HttpFoundation \StreamedResponse::class, $ controller ->stream ('foo ' ));
411
414
}
412
415
416
+ public function testRenderFormTwig ()
417
+ {
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 ' );
425
+
426
+ $ container = new Container ();
427
+ $ container ->set ('twig ' , $ twig );
428
+
429
+ $ controller = $ this ->createController ();
430
+ $ controller ->setContainer ($ container );
431
+
432
+ $ response = $ controller ->renderForm ('foo ' , $ form , ['bar ' => 'bar ' ]);
433
+
434
+ $ this ->assertTrue ($ response ->isSuccessful ());
435
+ $ this ->assertSame ('bar ' , $ response ->getContent ());
436
+ }
437
+
438
+ public function testRenderInvalidFormTwig ()
439
+ {
440
+ $ formView = new FormView ();
441
+
442
+ $ form = $ this ->getMockBuilder (FormInterface::class)->getMock ();
443
+ $ form ->expects ($ this ->once ())->method ('createView ' )->willReturn ($ formView );
444
+ $ form ->expects ($ this ->once ())->method ('isSubmitted ' )->willReturn (true );
445
+ $ form ->expects ($ this ->once ())->method ('isValid ' )->willReturn (false );
446
+
447
+ $ twig = $ this ->getMockBuilder (Environment::class)->disableOriginalConstructor ()->getMock ();
448
+ $ twig ->expects ($ this ->once ())->method ('render ' )->with ('foo ' , ['form ' => $ formView , 'bar ' => 'bar ' ])->willReturn ('bar ' );
449
+
450
+ $ container = new Container ();
451
+ $ container ->set ('twig ' , $ twig );
452
+
453
+ $ controller = $ this ->createController ();
454
+ $ controller ->setContainer ($ container );
455
+
456
+ $ response = $ controller ->renderForm ('foo ' , $ form , ['bar ' => 'bar ' ]);
457
+
458
+ $ this ->assertSame (Response::HTTP_UNPROCESSABLE_ENTITY , $ response ->getStatusCode ());
459
+ $ this ->assertSame ('bar ' , $ response ->getContent ());
460
+ }
461
+
413
462
public function testRedirectToRoute ()
414
463
{
415
464
$ router = $ this ->getMockBuilder (\Symfony \Component \Routing \RouterInterface::class)->getMock ();
0 commit comments