|
23 | 23 | use Symfony\Component\Form\FormConfigInterface; |
24 | 24 | use Symfony\Component\Form\FormFactoryInterface; |
25 | 25 | use Symfony\Component\Form\FormInterface; |
26 | | -use Symfony\Component\Form\FormView; |
27 | 26 | use Symfony\Component\HttpFoundation\BinaryFileResponse; |
28 | 27 | use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException; |
29 | 28 | use Symfony\Component\HttpFoundation\File\File; |
@@ -425,50 +424,70 @@ public function testStreamTwig() |
425 | 424 | $this->assertInstanceOf(StreamedResponse::class, $controller->stream('foo')); |
426 | 425 | } |
427 | 426 |
|
428 | | - public function testRenderFormTwig() |
| 427 | + public function testHandleFormNotSubmitted() |
429 | 428 | { |
430 | | - $formView = new FormView(); |
431 | | - |
432 | 429 | $form = $this->createMock(FormInterface::class); |
433 | | - $form->expects($this->once())->method('createView')->willReturn($formView); |
434 | | - |
435 | | - $twig = $this->createMock(Environment::class); |
436 | | - $twig->expects($this->once())->method('render')->with('foo', ['form' => $formView, 'bar' => 'bar'])->willReturn('bar'); |
437 | | - |
438 | | - $container = new Container(); |
439 | | - $container->set('twig', $twig); |
| 430 | + $form->expects($this->once())->method('isSubmitted')->willReturn(false); |
440 | 431 |
|
441 | 432 | $controller = $this->createController(); |
442 | | - $controller->setContainer($container); |
443 | | - |
444 | | - $response = $controller->renderForm('foo', $form, ['bar' => 'bar']); |
| 433 | + $response = $controller->handleForm( |
| 434 | + $form, |
| 435 | + Request::create('https://example.com'), |
| 436 | + function (FormInterface $form, $data): Re
ED4F
sponse { |
| 437 | + return new RedirectResponse('https://example.com/redir', Response::HTTP_SEE_OTHER); |
| 438 | + }, |
| 439 | + function (FormInterface $form, $data): Response { |
| 440 | + return new Response('rendered'); |
| 441 | + } |
| 442 | + ); |
445 | 443 |
|
446 | 444 | $this->assertTrue($response->isSuccessful()); |
447 | | - $this->assertSame('bar', $response->getContent()); |
| 445 | + $this->assertSame('rendered', $response->getContent()); |
448 | 446 | } |
449 | 447 |
|
450 | | - public function testRenderInvalidFormTwig() |
| 448 | + public function testHandleFormInvalid() |
451 | 449 | { |
452 | | - $formView = new FormView(); |
453 | | - |
454 | 450 | $form = $this->createMock(FormInterface::class); |
455 | | - $form->expects($this->once())->method('createView')->willReturn($formView); |
456 | 451 | $form->expects($this->once())->method('isSubmitted')->willReturn(true); |
457 | 452 | $form->expects($this->once())->method('isValid')->willReturn(false); |
458 | 453 |
|
459 | | - $twig = $this->createMock(Environment::class); |
460 | | - $twig->expects($this->once())->method('render')->with('foo', ['form' => $formView, 'bar' => 'bar'])->willReturn('bar'); |
| 454 | + $controller = $this->createController(); |
| 455 | + $response = $controller->handleForm( |
| 456 | + $form, |
| 457 | + Request::create('https://example.com'), |
| 458 | + function (FormInterface $form): Response { |
| 459 | + return new RedirectResponse('https://example.com/redir', Response::HTTP_SEE_OTHER); |
| 460 | + }, |
| 461 | + function (FormInterface $form): Response { |
| 462 | + return new Response('rendered'); |
| 463 | + } |
| 464 | + ); |
461 | 465 |
|
462 | | - $container = new Container(); |
463 | | - $container->set('twig', $twig); |
| 466 | + $this->assertSame(Response::HTTP_UNPROCESSABLE_ENTITY, $response->getStatusCode()); |
| 467 | + $this->assertSame('rendered', $response->getContent()); |
| 468 | + } |
464 | 469 |
|
465 | | - $controller = $this->createController(); |
466 | | - $controller->setContainer($container); |
| 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); |
467 | 475 |
|
468 | | - $response = $controller->renderForm('foo', $form, ['bar' => 'bar']); |
| 476 | + $controller = $this->createController(); |
| 477 | + $response = $controller->handleForm( |
| 478 | + $form, |
| 479 | + Request::create('https://example.com'), |
| 480 | + function (FormInterface $form): Response { |
| 481 | + return new RedirectResponse('https://example.com/redir', Response::HTTP_SEE_OTHER); |
| 482 | + }, |
| 483 | + function (FormInterface $form): Response { |
| 484 | + return new Response('rendered'); |
| 485 | + } |
| 486 | + ); |
469 | 487 |
|
470 | | - $this->assertSame(Response::HTTP_UNPROCESSABLE_ENTITY, $response->getStatusCode()); |
471 | | - $this->assertSame('bar', $response->getContent()); |
| 488 | + $this->assertInstanceOf(RedirectResponse::class, $response); |
| 489 | + $this->assertSame(Response::HTTP_SEE_OTHER, $response->getStatusCode()); |
| 490 | + $this->assertSame('https://example.com/redir', $response->getTargetUrl()); |
472 | 491 | } |
473 | 492 |
|
474 | 493 | public function testRedirectToRoute() |
|
0 commit comments