Ejemplo n.º 1
0
 */
require_once dirname(__FILE__) . '/../../bootstrap/unit.php';
require_once $_test_dir . '/../../../../test/unit/sfContextMock.class.php';
require_once dirname(__FILE__) . '/sfValidatorTestHelper.class.php';
$t = new lime_test(36, new lime_output_color());
$context = sfContext::getInstance();
$v = new sfStringValidator($context);
// ->execute()
$t->diag('->execute()');
$text = 'a random string to test string validator';
$error = null;
$t->ok($v->execute($text, $error), '->execute() returns true if you don\'t define any parameter');
$h = new sfValidatorTestHelper($context, $t);
// min
$t->diag('->execute() - min parameter');
$h->launchTests($v, '123456', true, 'min', null, array('min' => 5));
$h->launchTests($v, '12345', true, 'min', null, array('min' => 5));
$h->launchTests($v, '123', false, 'min', 'min_error', array('min' => 5));
// max
$t->diag('->execute() - max parameter');
$h->launchTests($v, '123', true, 'max', null, array('max' => 5));
$h->launchTests($v, '12345', true, 'max', null, array('max' => 5));
$h->launchTests($v, '123456', false, 'max', 'max_error', array('max' => 5));
// values
$t->diag('->execute() - values parameter');
$h->launchTests($v, 'foo', true, 'values', null, array('values' => array('foo')));
$h->launchTests($v, 'bar', true, 'values', null, array('values' => array('foo', 'bar')));
$h->launchTests($v, 'bar', false, 'values', 'values_error', array('values' => array('foo')));
$h->launchTests($v, 'foo', true, 'values', null, array('values' => 'foo'));
// insensitive
$t->diag('->execute() - insensitive parameter');
    $t->ok(!$v->execute($number, $error), '->execute() returns "nan_error" if value is not a number');
    $t->is($error, 'Input is not a number', '->execute() changes "$error" with a default message if it returns false');
}
foreach (array('any', 'decimal', 'float', 'int', 'integer') as $type) {
    $t->ok($v->initialize($context, array('type' => $type)), sprintf('->execute() can take "%s" as a type argument', $type));
}
try {
    $v->initialize($context, array('type' => 'another type'));
    $t->fail('->initialize() throws an sfValidatorException if "type" is invalid');
} catch (sfValidatorException $e) {
    $t->pass('->initialize() throws an sfValidatorException if "type" is invalid');
}
$h = new sfValidatorTestHelper($context, $t);
// min
$t->diag('->execute() - min parameter');
$h->launchTests($v, 6, true, 'min', null, array('min' => 5));
$h->launchTests($v, 5, true, 'min', null, array('min' => 5));
$h->launchTests($v, 4, false, 'min', 'min_error', array('min' => 5));
// max
$t->diag('->execute() - max parameter');
$h->launchTests($v, 4, true, 'max', null, array('max' => 5));
$h->launchTests($v, 5, true, 'max', null, array('max' => 5));
$h->launchTests($v, 6, false, 'max', 'max_error', array('max' => 5));
// type is integer
$t->diag('->execute() - type is integer');
$h->launchTests($v, 4, true, 'type', null, array('type' => 'integer'));
$h->launchTests($v, 4.1, false, 'type', 'type_error', array('type' => 'integer'));
// type is int
$t->diag('->execute() - type is int');
$h->launchTests($v, 4, true, 'type', null, array('type' => 'int'));
$h->launchTests($v, 4.1, false, 'type', 'type_error', array('type' => 'int'));
try
{
  $v = new sfCompareValidator($context, array('check' => 'value', 'operator' => 'N'));
  $t->fail('->initialize() takes an "operator" parameter in (>, >=, <, <=, ==, !=)');
}
catch (sfValidatorException $e)
{
  $t->pass('->initialize() takes an "operator" parameter in (>, >=, <, <=, ==, !=)');
}

// == operator (default operator)
$t->diag('->execute() - == operator (default operator)');
$v = new sfCompareValidator($context, array('check' => 'value'));
$request->parameters = array('value2' => 'azerty');
$options = array('check' => 'value2');
$h->launchTests($v, 'azerty', true, 'check', null, $options);
$h->launchTests($v, 'azerty1', false, 'check', null, $options);
$h->launchTests($v, 'azerty1', false, 'check', 'compare_error', $options);

// == operator
$t->diag('->execute() - == operator');
$request->parameters = array('value2' => 'azerty');
$options = array('check' => 'value2', 'operator' => '==');
$h->launchTests($v, 'azerty', true, 'check', null, $options);
$h->launchTests($v, 'azerty1', false, 'check', null, $options);
$h->launchTests($v, 'azerty1', false, 'check', 'compare_error', $options);

// != operator
$t->diag('->execute() - != operator');
$request->parameters = array('value2' => 'azerty');
$options = array('check' => 'value2', 'operator' => '!=');