Ejemplo n.º 1
0
 * 
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
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')));
$t->ok($v->execute($number, $error), '->execute() returns true if you don\'t define any parameter');
foreach (array('not a number', '0xFE') as $number) {
    $error = null;
    $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 = new lime_test(55);

class sfRequest
{
  public $parameters = array();

  function getParameter($key)
  {
    return $this->parameters[$key];
  }
}

$context = sfContext::getInstance(array('request' => 'sfRequest'));
$request = $context->request;

$h = new sfValidatorTestHelper($context, $t);

// check exceptions
try
{
  $v = new sfCompareValidator($context);
  $t->fail('->initialize() takes a required "check" parameter');
}
catch (sfValidatorException $e)
{
  $t->pass('->initialize() takes a required "check" parameter');
}

try
{
  $v = new sfCompareValidator($context, array('check' => 'value', 'operator' => 'N'));