|
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; |
26 | 27 | use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
27 | 28 | use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
|
28 | 29 | use Symfony\Component\HttpFoundation\File\File;
|
@@ -411,102 +412,63 @@ public function testRenderTwig()
|
411 | 412 | $this->assertEquals('bar', $controller->render('foo')->getContent());
|
412 | 413 | }
|
413 | 414 |
|
414 |
| - public function testStreamTwig() |
| 415 | + public function testRenderFormNew() |
415 | 416 | {
|
416 |
| - $twig = $this->createMock(Environment::class); |
| 417 | + $formView = new FormView(); |
| 418 | + |
| 419 | + $form = $this->getMockBuilder(FormInterface::class)->getMock(); |
| 420 | + $form->expects($this->once())->method('createView')->willReturn($formView); |
| 421 | + |
| 422 | + $twig = $this->getMockBuilder(Environment::class)->disableOriginalConstructor()->getMock(); |
| 423 | + $twig->expects($this->once())->method('render')->with('foo', ['form' => $formView, 'bar' => 'bar'])->willReturn('bar'); |
417 | 424 |
|
418 | 425 | $container = new Container();
|
419 | 426 | $container->set('twig', $twig);
|
420 | 427 |
|
421 | 428 | $controller = $this->createController();
|
422 | 429 | $controller->setContainer($container);
|
423 | 430 |
|
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 |
| - ); |
| 431 | + $response = $controller->renderForm('foo', $form, ['bar' => 'bar']); |
443 | 432 |
|
444 | 433 | $this->assertTrue($response->isSuccessful());
|
445 |
| - $this->assertSame('rendered', $response->getContent()); |
| 434 | + $this->assertSame('bar', $response->getContent()); |
446 | 435 | }
|
447 | 436 |
|
448 |
| - public function testHandleFormInvalid() |
| 437 | + public function testRenderFormSubmittedAndInvalid() |
449 | 438 | {
|
450 |
| - $form = $this->createMock(FormInterface::class); |
| 439 | + $formView = new FormView(); |
| 440 | + |
| 441 | + $form = $this->getMockBuilder(FormInterface::class)->getMock(); |
| 442 | + $form->expects($this->once())->method('createView')->willReturn($formView); |
451 | 443 | $form->expects($this->once())->method('isSubmitted')->willReturn(true);
|
452 | 444 | $form->expects($this->once())->method('isValid')->willReturn(false);
|
453 | 445 |
|
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 |
| - } |
| 446 | + $twig = $this->getMockBuilder(Environment::class)->disableOriginalConstructor()->getMock(); |
| 447 | + $twig->expects($this->once())->method('render')->with('foo', ['myForm' => $formView, 'bar' => 'bar'])->willReturn('bar'); |
469 | 448 |
|
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); |
| 449 | + $container = new Container(); |
| 450 | + $container->set('twig', $twig); |
475 | 451 |
|
476 | 452 | $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 |
| - ); |
| 453 | + $controller->setContainer($container); |
487 | 454 |
|
488 |
| - $this->assertInstanceOf(RedirectResponse::class, $response); |
489 |
| - $this->assertSame(Response::HTTP_SEE_OTHER, $response->getStatusCode()); |
490 |
| - $this->assertSame('https://example.com/redir', $response->getTargetUrl()); |
| 455 | + $response = $controller->renderForm('foo', $form, ['bar' => 'bar'], null, 'myForm'); |
| 456 | + |
| 457 | + $this->assertSame(422, $response->getStatusCode()); |
| 458 | + $this->assertSame('bar', $response->getContent()); |
491 | 459 | }
|
492 | 460 |
|
493 |
| - public function testHandleFormTypeError() |
| 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