Skip to content

[FrameworkBundle] Remove ControllerTrait::isFormValid() #29813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
CHANGELOG
=========

4.3.0
-----

* Added `ControllerTrait::isFormValid()`

4.2.0
-----

Expand Down
46 changes: 12 additions & 34 deletions src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function get(string $id)
*
* @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);
}
Expand All @@ -85,7 +85,7 @@ protected function generateUrl(string $route, array $parameters = array(), int $
*
* @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;
Expand All @@ -109,7 +109,7 @@ protected function redirect(string $url, int $status = 302): RedirectResponse
*
* @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);
}
Expand All @@ -119,12 +119,12 @@ protected function redirectToRoute(string $route, array $parameters = array(), i
*
* @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);
}
Expand Down Expand Up @@ -203,7 +203,7 @@ protected function denyAccessUnlessGranted($attributes, $subject = null, string
*
* @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);
Expand All @@ -221,7 +221,7 @@ protected function renderView(string $view, array $parameters = array()): string
*
* @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);
Expand All @@ -245,7 +245,7 @@ protected function render(string $view, array $parameters = array(), Response $r
*
* @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');
Expand Down Expand Up @@ -311,7 +311,7 @@ protected function createAccessDeniedException(string $message = 'Access Denied.
*
* @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);
}
Expand All @@ -321,33 +321,11 @@ protected function createForm(string $type, $data = null, array $options = array
*
* @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.
*
Expand Down Expand Up @@ -441,7 +419,7 @@ protected function addLink(Request $request, Link $link)
}

if (null === $linkProvider = $request->attributes->get('_links')) {
$request->attributes->set('_links', new GenericLinkProvider(array($link)));
$request->attributes->set('_links', new GenericLinkProvider([$link]));

return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testForward()
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));
Expand Down Expand Up @@ -122,7 +122,7 @@ public function testJson()
$controller = $this->createController();
$controller->setContainer(new Container());

$response = $controller->json(array());
$response = $controller->json([]);
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertEquals('[]', $response->getContent());
}
Expand All @@ -135,15 +135,15 @@ public function testJsonWithSerializer()
$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);

$controller = $this->createController();
$controller->setContainer($container);

$response = $controller->json(array());
$response = $controller->json([]);
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertEquals('[]', $response->getContent());
}
Expand All @@ -156,15 +156,15 @@ public function testJsonWithSerializerContextOverride()
$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);

$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);
Expand Down Expand Up @@ -389,7 +389,7 @@ public function testAddFlash()
$controller->setContainer($container);
$controller->addFlash('foo', 'bar');

$this->assertSame(array('bar'), $flashBag->get('foo'));
$this->assertSame(['bar'], $flashBag->get('foo'));
}

public function testCreateAccessDeniedException()
Expand Down Expand Up @@ -517,117 +517,6 @@ public function testCreateFormBuilder()
$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();
Expand Down