Skip to content

Commit

Permalink
[FrameworkBundle] Remove ControllerTrait::isFormValid()
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrixx committed Jan 10, 2019
1 parent 4ba6397 commit fd2985c
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 138 deletions.
5 changes: 0 additions & 5 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
@@ -1,11 +1,6 @@
CHANGELOG
=========

4.3.0
-----

* Added `ControllerTrait::isFormValid()`

4.2.0
-----

Expand Down
22 changes: 0 additions & 22 deletions src/Symfony/Bundle/FrameworkBundle/Controller/ControllerTrait.php
Expand Up @@ -326,28 +326,6 @@ protected function createFormBuilder($data = null, array $options = array()): Fo
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 @@ -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

0 comments on commit fd2985c

Please sign in to comment.