示例#1
0
}
$v->setOption('halt_on_error', true);
try {
    $v->clean('foo');
    $t->fail('->clean() throws an sfValidatorError exception if one of the validators fails');
    $t->skip('', 3);
} catch (sfValidatorError $e) {
    $t->pass('->clean() throws an sfValidatorError exception if one of the validators fails');
    $t->is(count($e), 1, '->clean() only returns the first error if halt_on_error option is true');
    $t->is($e[0]->getCode(), 'max_length', '->clean() throws a sfValidatorSchemaError');
    $t->is($e instanceof sfValidatorErrorSchema, 'max_length', '->clean() throws a sfValidatorSchemaError');
}
try {
    $v->setMessage('invalid', 'Invalid.');
    $v->clean('foo');
    $t->fail('->clean() throws an sfValidatorError exception if one of the validators fails');
    $t->skip('', 2);
} catch (sfValidatorError $e) {
    $t->pass('->clean() throws an sfValidatorError exception if one of the validators fails');
    $t->is($e->getCode(), 'invalid', '->clean() throws a sfValidatorError if invalid message is not empty');
    $t->is(!$e instanceof sfValidatorErrorSchema, 'max_length', '->clean() throws a sfValidatorError if invalid message is not empty');
}
// ->asString()
$t->diag('->asString()');
$v1 = new sfValidatorString(array('max_length' => 3));
$v2 = new sfValidatorString(array('min_length' => 3));
$v = new sfValidatorAnd(array($v1, $v2));
$t->is($v->asString(), "(\n  String({ max_length: 3 })\n  and\n  String({ min_length: 3 })\n)", '->asString() returns a string representation of the validator');
$v = new sfValidatorAnd(array($v1, $v2), array(), array('required' => 'This is required.'));
$t->is($v->asString(), "(\n  String({ max_length: 3 })\n  and({}, { required: 'This is required.' })\n  String({ min_length: 3 })\n)", '->asString() returns a string representation of the validator');
  $t->is(count($e), 1, '->clean() only returns the first error if halt_on_error option is true');
  $t->is($e[0]->getCode(), 'max_length', '->clean() throws a sfValidatorSchemaError');
  $t->is($e instanceof sfValidatorErrorSchema, 'max_length', '->clean() throws a sfValidatorSchemaError');
}

try
{
  $v->setMessage('invalid', 'Invalid.');
  $v->clean('foo');
  $t->fail('->clean() throws an sfValidatorError exception if one of the validators fails');
  $t->skip('', 2);
}
catch (sfValidatorError $e)
{
  $t->pass('->clean() throws an sfValidatorError exception if one of the validators fails');
  $t->is($e->getCode(), 'invalid', '->clean() throws a sfValidatorError if invalid message is not empty');
  $t->is(!$e instanceof sfValidatorErrorSchema, 'max_length', '->clean() throws a sfValidatorError if invalid message is not empty');
}

// ->asString()
$t->diag('->asString()');
$v1 = new sfValidatorString(array('max_length' => 3));
$v2 = new sfValidatorString(array('min_length' => 3));
$v = new sfValidatorAnd(array($v1, $v2));
$t->is($v->asString(), "(\n  String({ max_length: 3 })\n  and\n  String({ min_length: 3 })\n)"
, '->asString() returns a string representation of the validator');

$v = new sfValidatorAnd(array($v1, $v2), array(), array('required' => 'This is required.'));
$t->is($v->asString(), "(\n  String({ max_length: 3 })\n  and({}, { required: 'This is required.' })\n  String({ min_length: 3 })\n)"
, '->asString() returns a string representation of the validator');