I use Symfony3 and forms. I have a formType and I want to test it. I follow this article : http://symfony.com/doc/current/form/unit_testing.html
My formType is the following :
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Required;
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $aOption)
{
$builder->add('name', TextType::class, array(
'constraints' => array(
new NotBlank(),
new Length(array('min' => 3)),
),
))
->add('age', IntegerType::class, array(
'constraints' => array(
new GreaterThanOrEqual(array('value' => 18)),
new Required()),
));
}
}
The name must have more than 3 chars and the age must be greater than 18. I want to test with phpunit, with invalid data to submit. The age I submut is invalid because it is less than 18, and the name is invalid because it contains 1 char. My test class is the following :
namespace Tests\AppBundle\Controller;
use AppBundle\Form\UserType;
use Symfony\Component\Form\Test\TypeTestCase;
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
use Symfony\Component\Form\Form;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class UserTypeTest extends TypeTestCase
{
private $validator;
protected function getExtensions()
{
$this->validator = $this->createMock(ValidatorInterface::class);
// use getMock() on PHPUnit 5.3 or below
// $this->validator = $this->getMock(ValidatorInterface::class);
$this->validator
->method('validate')
->will($this->returnValue(new ConstraintViolationList()));
$this->validator
->method('getMetadataFor')
->will($this->returnValue(new ClassMetadata(Form::class)));
return array(
new ValidatorExtension($this->validator),
);
}
public function testIndex()
{
$formData = array(
'name' => 'a',
'age' => '4'
);
$formBuilder = $this->factory->createBuilder(UserType::class);
$form = $formBuilder->getForm();
$form->submit($formData);
$this->assertTrue($form->isSynchronized());
$this->assertFalse($form->isValid());
}
}
When I want to run the tests with invalid data to submit, I have failure :
./vendor/bin/phpunit tests/AppBundle/Controller/UserTypeTest.php
Failed asserting that true is false.
Why the form is always valid ?