Commit 7509fa2f authored by Grégoire Pineau's avatar Grégoire Pineau

Added more tests on validator provider

To avoid another regression fixed by PR #321,
tests ensure validation works on form built without class
parent fe7cbfdc
......@@ -13,6 +13,8 @@ namespace Silex\Tests\Provider;
use Silex\Application;
use Silex\Provider\ValidatorServiceProvider;
use Silex\Provider\FormServiceProvider;
use Symfony\Component\Validator\Constraints as Assert;
/**
* ValidatorServiceProvider
......@@ -51,7 +53,7 @@ class ValidatorServiceProviderTest extends \PHPUnit_Framework_TestCase
public function testValidatorServiceWithFormServiceDisabled($app)
{
if (!is_dir(__DIR__ . '/../../../../vendor/symfony/form')) {
$this->markTestSkipped('Form submodule was not installed.');
$this->markTestSkipped('Form component was not installed.');
}
$metadatas = $app['validator']->getMetadataFactory()->getClassMetadata('Symfony\Component\Form\Form');
......@@ -65,7 +67,7 @@ class ValidatorServiceProviderTest extends \PHPUnit_Framework_TestCase
public function testValidatorServiceWithFormServiceEnabled($app)
{
if (!is_dir(__DIR__ . '/../../../../vendor/symfony/form')) {
$this->markTestSkipped('Form submodule was not installed.');
$this->markTestSkipped('Form component was not installed.');
}
$app->register(new ValidatorServiceProvider());
......@@ -76,4 +78,52 @@ class ValidatorServiceProviderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(1, count($metadatas->constraints));
}
/**
* @depends testRegister
* @dataProvider testValidatorConstraintProvider
*/
public function testValidatorConstraint($email, $isValid, $nbGlobalError, $nbEmailError, $app)
{
if (!is_dir(__DIR__ . '/../../../../vendor/symfony/form')) {
$this->markTestSkipped('Form component was not installed.');
}
$app->register(new ValidatorServiceProvider());
$app->register(new FormServiceProvider());
$constraints = new Assert\Collection(array(
'email' => array(
new Assert\NotBlank(),
new Assert\Email(),
),
));
$builder = $app['form.factory']->createBuilder('form', array(), array(
'validation_constraint' => $constraints,
'csrf_protection' => false,
));
$form = $builder
->add('email', 'email', array('label' => 'Email'))
->getForm()
;
$form->bind(array('email' => $email));
$this->assertEquals($isValid, $form->isValid());
$this->assertEquals($nbGlobalError, count($form->getErrors()));
$this->assertEquals($nbEmailError, count($form->offsetGet('email')->getErrors()));
}
public function testValidatorConstraintProvider()
{
// Email, form is valid , nb global error, nb email error
return array(
array('', false, 0, 1),
array('not an email', false, 0, 1),
array('email@sample.com', true, 0, 0),
);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment