Code snippets for symfony 1.x


Snippets tagged "validator test"

Test a validator class

If you create a custom validator, you might want to test it by creating a unit test script.

This is a sample unit test script for testing validators.


$app='frontend'; // Necessary for fonctional boostrap
$context = sfContext::getInstance();
$request = $context->getRequest();
$manager = new sfValidatorManager();
$validator = new myFooBarValidator();
// The values to validate.
// You can make a second array with values
// that are supposed to fail and do another
// loop below.
$values = Array('foo', 'bar');
$t = new lime_test(count($values) * 2, new lime_output_color());
foreach ( $values as $value ) {
  // Re-initialize the validation entry
  // Without this, the first failure would
  // cause any additional validation to
  // be skipped
  $manager->registerName('myname', false);
  $manager->registerValidator('myname', $validator);
  $request->setParameter('myname', $value);
  $retval = $manager->execute();
  $t->is($retval, true);
  $t->is($request->getErrors(), Array());
  // We remove the error so that the next loop
  // does not carry the error.
by Christian Roy on 2007-06-04, tagged test  validator