Skip to content

Commit

Permalink
Merge pull request #14 from kimlai/invalid_form_errors
Browse files Browse the repository at this point in the history
InvalidQueryForm's detail is now complete.
  • Loading branch information
Jean-François Lépine committed Oct 30, 2013
2 parents 81ab08a + fea3876 commit e004843
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 24 deletions.
43 changes: 23 additions & 20 deletions src/Alterway/Bundle/RestProblemBundle/Problem/InvalidQueryForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Alterway\Bundle\RestProblemBundle\Problem;

use Symfony\Component\Form\FormInterface;

/*
* (c) 2013 La Ruche Qui Dit Oui!
*
Expand All @@ -11,34 +13,35 @@

class InvalidQueryForm extends Problem
{

public function __construct(\Symfony\Component\Form\FormInterface $form)
public function __construct(FormInterface $form)
{
$formErrors = array();
$formChildrenErrors = array();

$this->title = "Invalid query form";
$this->detail = array(
'errors' => $this->buildErrorsTree($form),
);
$this->httpStatus = 400;
}

/**
* Recursively builds a tree of form errors (including children errors).
* Based on Form::getErrorsAsString() : https://github.com/symfony/Form/blob/master/Form.php#L750
*
* @param FormInterface $form
*/
private function buildErrorsTree($form) {
$errors = array();

foreach ($form->getErrors() as $key => $error) {
$formErrors['generic'][$key] = $error->getMessage() . ' [parameters: ' . implode(', ', $error->getMessageParameters()) . ']';
$errors[$key] = $error->getMessage();
}

foreach ($form->all() as $key => $child) {
if(!isset($errors[$key])) {
$formChildrenErrors[$key] = array();
}

$childErrors = $child->getErrors();
foreach($childErrors as $err) {
$formChildrenErrors[$key][] = $err->getMessage();
}

if (empty($formChildrenErrors[$key])) {
unset($formChildrenErrors[$key]);
if ($child instanceOf FormInterface && $err = $this->buildErrorsTree($child)) {
$errors[$key] = $err;
}
}

$this->title = "Invalid query";
$this->detail = array_merge($formErrors, $formChildrenErrors);
$this->httpStatus = 400;
return $errors;
}

}
26 changes: 22 additions & 4 deletions test/unit/Problem/InvalidQueryFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,39 @@ public function testDetailsOfInvalidFormAreGiven()
// @see https://github.com/sebastianbergmann/phpunit-mock-objects/issues/103
// $form = $this->getMock('\Symfony\Component\Form\FormInterface', array('all', 'getErrors'));
$form = $this->getMock('\Symfony\Component\Form\Form', array('all', 'getErrors'), array(), '', false);

$child = $this->getMock('\Symfony\Component\Form\Form', array('all', 'getErrors'), array(), '', false);
$error1 = new \Symfony\Component\Form\FormError("an error occured in the root form");
$error2 = new \Symfony\Component\Form\FormError("an error occured in a child form");

$form
->expects($this->once())
->method('getErrors')
->will($this->returnValue(array('field1' => array('an error occured'))))
->will($this->returnValue(array($error1)))
;
$form
->expects($this->once())
->method('all')
->will($this->returnValue(array('child' => $child)))
;

$child
->expects($this->once())
->method('getErrors')
->will($this->returnValue(array($error2)))
;
$child
->expects($this->once())
->method('all')
->will($this->returnValue(array()))
;

$object = new \Alterway\Bundle\RestProblemBundle\Problem\InvalidQueryForm($form);
$expected = array('field1' => array('an error occured'));
$expected = array(
'errors' => array(
'an error occured in the root form',
'child' => array('an error occured in a child form'),
),
);
$this->assertEquals($expected, $object->getDetail());
}

Expand All @@ -54,7 +72,7 @@ public function testNoProblemIsFoundWhenFormIsValid()
;

$object = new \Alterway\Bundle\RestProblemBundle\Problem\InvalidQueryForm($form);
$expected = array();
$expected = array('errors' => array());
$this->assertEquals($expected, $object->getDetail());
}

Expand Down

0 comments on commit e004843

Please sign in to comment.