[FrameworkBundle] Remove ControllerTrait::isFormValid()

This commit is contained in:
Grégoire Pineau 2019-01-08 10:39:57 +01:00 committed by Fabien Potencier
parent 4ba6397c5b
commit 2be1987ad1
3 changed files with 19 additions and 157 deletions

View File

@ -1,11 +1,6 @@
CHANGELOG
=========
4.3.0
-----
* Added `ControllerTrait::isFormValid()`
4.2.0
-----

View File

@ -73,7 +73,7 @@ trait ControllerTrait
*
* @final
*/
protected function generateUrl(string $route, array $parameters = array(), int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string
protected function generateUrl(string $route, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string
{
return $this->container->get('router')->generate($route, $parameters, $referenceType);
}
@ -85,7 +85,7 @@ trait ControllerTrait
*
* @final
*/
protected function forward(string $controller, array $path = array(), array $query = array()): Response
protected function forward(string $controller, array $path = [], array $query = []): Response
{
$request = $this->container->get('request_stack')->getCurrentRequest();
$path['_controller'] = $controller;
@ -109,7 +109,7 @@ trait ControllerTrait
*
* @final
*/
protected function redirectToRoute(string $route, array $parameters = array(), int $status = 302): RedirectResponse
protected function redirectToRoute(string $route, array $parameters = [], int $status = 302): RedirectResponse
{
return $this->redirect($this->generateUrl($route, $parameters), $status);
}
@ -119,12 +119,12 @@ trait ControllerTrait
*
* @final
*/
protected function json($data, int $status = 200, array $headers = array(), array $context = array()): JsonResponse
protected function json($data, int $status = 200, array $headers = [], array $context = []): JsonResponse
{
if ($this->container->has('serializer')) {
$json = $this->container->get('serializer')->serialize($data, 'json', array_merge(array(
$json = $this->container->get('serializer')->serialize($data, 'json', array_merge([
'json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS,
), $context));
], $context));
return new JsonResponse($json, $status, $headers, true);
}
@ -203,7 +203,7 @@ trait ControllerTrait
*
* @final
*/
protected function renderView(string $view, array $parameters = array()): string
protected function renderView(string $view, array $parameters = []): string
{
if ($this->container->has('templating')) {
return $this->container->get('templating')->render($view, $parameters);
@ -221,7 +221,7 @@ trait ControllerTrait
*
* @final
*/
protected function render(string $view, array $parameters = array(), Response $response = null): Response
protected function render(string $view, array $parameters = [], Response $response = null): Response
{
if ($this->container->has('templating')) {
$content = $this->container->get('templating')->render($view, $parameters);
@ -245,7 +245,7 @@ trait ControllerTrait
*
* @final
*/
protected function stream(string $view, array $parameters = array(), StreamedResponse $response = null): StreamedResponse
protected function stream(string $view, array $parameters = [], StreamedResponse $response = null): StreamedResponse
{
if ($this->container->has('templating')) {
$templating = $this->container->get('templating');
@ -311,7 +311,7 @@ trait ControllerTrait
*
* @final
*/
protected function createForm(string $type, $data = null, array $options = array()): FormInterface
protected function createForm(string $type, $data = null, array $options = []): FormInterface
{
return $this->container->get('form.factory')->create($type, $data, $options);
}
@ -321,33 +321,11 @@ trait ControllerTrait
*
* @final
*/
protected function createFormBuilder($data = null, array $options = array()): FormBuilderInterface
protected function createFormBuilder($data = null, array $options = []): FormBuilderInterface
{
return $this->container->get('form.factory')->createBuilder(FormType::class, $data, $options);
}
/**
* Handles request and check form validity.
*
* @final
*/
protected function isFormValid(FormInterface $form, Request $request = null): bool
{
if ($form->isSubmitted()) {
throw new \LogicException('The form is already submitted, use $form->isValid() directly.');
}
if (!$request) {
$request = $this->container->get('request_stack')->getCurrentRequest();
}
if (!$request) {
throw new \LogicException('You must pass a request as second argument because the request stack is empty.');
}
return $form->handleRequest($request)->isSubmitted() && $form->isValid();
}
/**
* Shortcut to return the Doctrine Registry service.
*
@ -441,7 +419,7 @@ trait ControllerTrait
}
if (null === $linkProvider = $request->attributes->get('_links')) {
$request->attributes->set('_links', new GenericLinkProvider(array($link)));
$request->attributes->set('_links', new GenericLinkProvider([$link]));
return;
}

View File

@ -60,7 +60,7 @@ abstract class ControllerTraitTest extends TestCase
public function testGetUser()
{
$user = new User('user', 'pass');
$token = new UsernamePasswordToken($user, 'pass', 'default', array('ROLE_USER'));
$token = new UsernamePasswordToken($user, 'pass', 'default', ['ROLE_USER']);
$controller = $this->createController();
$controller->setContainer($this->getContainerWithTokenStorage($token));
@ -122,7 +122,7 @@ abstract class ControllerTraitTest extends TestCase
$controller = $this->createController();
$controller->setContainer(new Container());
$response = $controller->json(array());
$response = $controller->json([]);
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertEquals('[]', $response->getContent());
}
@ -135,7 +135,7 @@ abstract class ControllerTraitTest extends TestCase
$serializer
->expects($this->once())
->method('serialize')
->with(array(), 'json', array('json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS))
->with([], 'json', ['json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS])
->will($this->returnValue('[]'));
$container->set('serializer', $serializer);
@ -143,7 +143,7 @@ abstract class ControllerTraitTest extends TestCase
$controller = $this->createController();
$controller->setContainer($container);
$response = $controller->json(array());
$response = $controller->json([]);
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertEquals('[]', $response->getContent());
}
@ -156,7 +156,7 @@ abstract class ControllerTraitTest extends TestCase
$serializer
->expects($this->once())
->method('serialize')
->with(array(), 'json', array('json_encode_options' => 0, 'other' => 'context'))
->with([], 'json', ['json_encode_options' => 0, 'other' => 'context'])
->will($this->returnValue('[]'));
$container->set('serializer', $serializer);
@ -164,7 +164,7 @@ abstract class ControllerTraitTest extends TestCase
$controller = $this->createController();
$controller->setContainer($container);
$response = $controller->json(array(), 200, array(), array('json_encode_options' => 0, 'other' => 'context'));
$response = $controller->json([], 200, [], ['json_encode_options' => 0, 'other' => 'context']);
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertEquals('[]', $response->getContent());
$response->setEncodingOptions(JSON_FORCE_OBJECT);
@ -389,7 +389,7 @@ abstract class ControllerTraitTest extends TestCase
$controller->setContainer($container);
$controller->addFlash('foo', 'bar');
$this->assertSame(array('bar'), $flashBag->get('foo'));
$this->assertSame(['bar'], $flashBag->get('foo'));
}
public function testCreateAccessDeniedException()
@ -517,117 +517,6 @@ abstract class ControllerTraitTest extends TestCase
$this->assertEquals($formBuilder, $controller->createFormBuilder('foo'));
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage The form is already submitted, use $form->isValid() directly.
*/
public function testIsFormValidWhenAlreadySubmitted()
{
$requestStack = new RequestStack();
$requestStack->push($request = new Request());
$container = new Container();
$container->set('request_stack', $requestStack);
$controller = $this->createController();
$controller->setContainer($container);
$form = $this->getMockBuilder('Symfony\Component\Form\FormInterface')->getMock();
$form
->expects($this->once())
->method('isSubmitted')
->willReturn(true)
;
$controller->isFormValid($form);
}
public function testIsFormValidWhenInvalid()
{
$requestStack = new RequestStack();
$requestStack->push($request = new Request());
$container = new Container();
$container->set('request_stack', $requestStack);
$controller = $this->createController();
$controller->setContainer($container);
$form = $this->getMockBuilder('Symfony\Component\Form\FormInterface')->getMock();
$form
->expects($this->at(0))
->method('isSubmitted')
->willReturn(false)
;
$form
->expects($this->once())
->method('handleRequest')
->with($request)
->willReturn($form)
;
$form
->expects($this->at(2))
->method('isSubmitted')
->willReturn(false)
;
$this->assertFalse($controller->isFormValid($form));
}
public function testIsFormValidWhenValid()
{
$requestStack = new RequestStack();
$requestStack->push($request = new Request());
$container = new Container();
$container->set('request_stack', $requestStack);
$controller = $this->createController();
$controller->setContainer($container);
$form = $this->getMockBuilder('Symfony\Component\Form\FormInterface')->getMock();
$form
->expects($this->at(0))
->method('isSubmitted')
->willReturn(false)
;
$form
->expects($this->once())
->method('handleRequest')
->with($request)
->willReturn($form)
;
$form
->expects($this->at(2))
->method('isSubmitted')
->willReturn(true)
;
$form
->expects($this->once())
->method('isValid')
->willReturn(true)
;
$this->assertTrue($controller->isFormValid($form));
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage You must pass a request as second argument because the request stack is empty.
*/
public function testIsFormValidWhenRequestStackIsEmpty()
{
$container = new Container();
$container->set('request_stack', new RequestStack());
$controller = $this->createController();
$controller->setContainer($container);
$form = $this->getMockBuilder('Symfony\Component\Form\FormInterface')->getMock();
$this->assertTrue($controller->isFormValid($form));
}
public function testGetDoctrine()
{
$doctrine = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')->getMock();