예제 #1
0
 /**
  */
 public function testRuleLength()
 {
     $validator = new SimpleValidator();
     $this->assertInvalid($validator->singleValidate('123', 'length=9'));
     $this->assertValid($validator->singleValidate('123456789', 'length=9'));
     $this->assertInvalid($validator->singleValidate('12345678', 'length=9'));
 }
예제 #2
0
 /**
  */
 public function testRuleMinLength()
 {
     $validator = new SimpleValidator();
     $this->assertValid($validator->singleValidate('1234', 'minlength=3'));
     $this->assertValid($validator->singleValidate('123', 'minlength=3'));
     $this->assertInvalid($validator->singleValidate('12', 'minlength=3'));
 }
예제 #3
0
 /**
  */
 public function testRuleFloat()
 {
     $validator = new SimpleValidator();
     $this->assertValid($validator->singleValidate('1234.456', 'float'));
     $this->assertValid($validator->singleValidate(1234, 'float'));
     $this->assertValid($validator->singleValidate(1234.78, 'float'));
     $this->assertInvalid($validator->singleValidate('abc', 'float'));
 }
예제 #4
0
 /**
  */
 public function testRuleMin()
 {
     $validator = new SimpleValidator();
     $this->assertInvalid($validator->singleValidate(2, 'min=5'));
     $this->assertValid($validator->singleValidate('10', 'min=10'));
     $this->assertValid($validator->singleValidate(10, 'min=10'));
     $this->assertValid($validator->singleValidate(15, 'min=10'));
 }
예제 #5
0
 /**
  * @expectedException SimpleValidatorException
  */
 public function testInvalidRuleSetException()
 {
     // note that the rules could be an empty string
     $invalid_rules = "string";
     $data = array('sample' => 'foo', 'another' => 'bar');
     $validator = new SimpleValidator($invalid_rules);
     $validator->validate($data);
 }
예제 #6
0
 /**
  * @dataProvider dataNumeric
  */
 public function testNumeric($expected_success, $value)
 {
     $validator = new SimpleValidator();
     if ($expected_success) {
         $this->assertEquals(NULL, $validator->singleValidate($value, 'numeric'));
     } else {
         $this->assertEquals($validator->errorForNumeric, $validator->singleValidate($value, 'numeric'));
     }
 }
예제 #7
0
 public function testRuleRequiredIfToNotRequired()
 {
     $validation_rules = array('new_member' => 'required choices=yes,no', 'email' => 'email required_if=new_member->yes');
     $data = array('new_member' => 'no', 'email' => '*****@*****.**');
     $expected = array();
     $validator = new SimpleValidator($validation_rules);
     $errors = $validator->validate($data);
     $this->assertEquals($expected, $errors);
 }
예제 #8
0
 public function testNotRequiredAndEqualTo()
 {
     // [NOTE]
     // Best practice on this to NOT set the type of the second field (email)
     // but instead simply use the equalTo
     $validation_rules = array('email' => 'email', 'email_confirm' => 'equalTo=email');
     $validator = new SimpleValidator($validation_rules);
     $this->assertEquals(array('email_confirm' => $validator->errorForEqualTo), $validator->validate(array('email' => '*****@*****.**', 'email_confirm' => '')), "Problem when first is valid email, and second is empty on line " . __LINE__);
     $validator = new SimpleValidator($validation_rules);
     $this->assertEmpty($validator->validate(array('email' => '', 'email_confirm' => '')), "Problem when both are empty on line " . __LINE__);
 }
 public function testDateGreaterThanEpoch()
 {
     $validation_rules = array('start_date' => 'date', 'end_date' => 'dateGreaterThan=start_date');
     $validator = new SimpleValidator($validation_rules);
     $this->assertEquals(array(), $validator->validate(array('start_date' => '2/01/2011', 'end_date' => '6/01/11')), "Problem when date is in epoch and greater than on " . __LINE__);
     $validator = new SimpleValidator($validation_rules);
     $this->assertEquals(array('end_date' => $validator->errorForDateGreaterThan), $validator->validate(array('start_date' => '6/01/11', 'end_date' => '2/01/2011')), "Problem when date is in epoch and less than on " . __LINE__);
     $validator = new SimpleValidator($validation_rules);
     $this->assertEquals(array(), $validator->validate(array('start_date' => '2/01/11', 'end_date' => '6/01/2011')), "Problem when date is in epoch and greater than on " . __LINE__);
     $validator = new SimpleValidator($validation_rules);
     $this->assertEquals(array('end_date' => $validator->errorForDateGreaterThan), $validator->validate(array('start_date' => '6/01/2011', 'end_date' => '2/01/11')), "Problem when date is in epoch and less than on " . __LINE__);
 }
예제 #10
0
 /**
  */
 public function testRuleNotEqualTo()
 {
     $validation_rules = array('choice_one' => 'required', 'choice_two' => 'required notEqualTo=choice_one', 'choice_three' => 'required notEqualTo=choice_one|choice_two');
     $validator = new SimpleValidator($validation_rules);
     $good_form_data = array('choice_one' => 'red', 'choice_two' => 'blue', 'choice_three' => 'green');
     $errors = $validator->validate($good_form_data);
     $this->assertEquals(array(), $errors);
     $bad_form_data = array('choice_one' => 'red', 'choice_two' => 'red', 'choice_three' => 'green');
     $expected_fail = array('choice_two' => $validator->errorForNotEqualTo);
     $errors = $validator->validate($bad_form_data);
     $this->assertEquals($expected_fail, $errors);
     $bad_form_data = array('choice_one' => 'red', 'choice_two' => 'blue', 'choice_three' => 'blue');
     $expected_fail = array('choice_two' => $validator->errorForNotEqualTo);
     $errors = $validator->validate($bad_form_data);
     $this->assertEquals($expected_fail, $errors);
 }
예제 #11
0
 public function testRuleZip()
 {
     $validator = new SimpleValidator();
     $this->assertValid($validator->singleValidate('50312', 'zip'));
     $this->assertValid($validator->singleValidate('98226-1234', 'zip'));
     $this->assertValid($validator->singleValidate('12345', 'zip'));
     $this->assertInvalid($validator->singleValidate('999', 'zip'));
     $this->assertInvalid($validator->singleValidate('abcdef', 'zip'));
 }
예제 #12
0
 /**
  */
 public function testRuleMax()
 {
     $validator = new SimpleValidator();
     $this->assertInvalid($validator->singleValidate(100, 'max=99'));
     $this->assertInvalid($validator->singleValidate("100", 'max=99'));
     $this->assertValid($validator->singleValidate('10', 'max=10'));
     $this->assertValid($validator->singleValidate(10, 'max=10'));
     $this->assertValid($validator->singleValidate(5, 'max=10'));
 }
예제 #13
0
 public function testRuleChoices()
 {
     $validator = new SimpleValidator();
     $this->assertValid($validator->singleValidate('two', 'choices=one,two,three,four'));
     $this->assertValid($validator->singleValidate(array('one', 'two'), 'choices=one,two,three,four'));
     $this->assertInvalid($validator->singleValidate(array('one', 'six'), 'choices=one,two,three,four'));
     // large list of choices
     $choice = $this->randChoice();
     $options = "choices=" . $choice;
     for ($i = 0; $i < 100; $i++) {
         $options .= ',' . $this->randChoice();
     }
     $this->assertValid($validator->singleValidate($choice, $options));
     $this->assertInvalid($validator->singleValidate('none', 'choices=one,two,three,four'));
 }
예제 #14
0
 /**
  * @expectedException SimpleValidatorException
  */
 public function testInvalidRuleException()
 {
     $validator = new SimpleValidator();
     $validator->singleValidate('sample text', 'alphaNumBlahBlah');
 }
예제 #15
0
 /**
  * @dataProvider data_BadCities
  */
 public function testRuleInvalidCity($city)
 {
     $validator = new SimpleValidator();
     $this->assertInvalid($validator->singleValidate($city, 'city'));
 }
예제 #16
0
 /**
  */
 public function testRulePhrase()
 {
     $validator = new SimpleValidator();
     $this->assertValid($validator->singleValidate('Text with spaces', 'phrase'));
     $this->assertInvalid($validator->singleValidate('Text-with/extracharacters', 'phrase'));
 }
예제 #17
0
 /**
  */
 public function testRuleNone()
 {
     $validator = new SimpleValidator();
     $this->assertValid($validator->singleValidate($this->randChoice(), 'none'));
     $this->assertValid($validator->singleValidate("4%6&#\$@\\}", 'none'));
 }
예제 #18
0
 /**
  * @dataProvider data_Dates
  */
 public function testRuleDate($valid, $date)
 {
     $validator = new SimpleValidator();
     $method = $valid ? 'assertValid' : 'assertInvalid';
     $this->{$method}($validator->singleValidate($date, 'date'));
 }
예제 #19
0
 public function testRuleAlpha()
 {
     $validator = new SimpleValidator();
     $this->assertValid($validator->singleValidate('abcdef', 'alpha'));
     $this->assertInvalid($validator->singleValidate('999', 'alpha'));
 }
예제 #20
0
 /**
  * @dataProvider data_Addresses
  */
 public function testRuleEmail($valid, $address)
 {
     $validator = new SimpleValidator();
     $method = $valid ? 'assertValid' : 'assertInvalid';
     $this->{$method}($validator->singleValidate($address, 'url'));
 }
예제 #21
0
 /**
  */
 public function testRuleNumeric()
 {
     $validator = new SimpleValidator();
     $this->assertValid($validator->singleValidate('1234', 'numeric'));
     $this->assertInvalid($validator->singleValidate('abc', 'numeric'));
 }
예제 #22
0
 /**
  */
 public function testRuleRequired()
 {
     $validator = new SimpleValidator();
     $this->assertInvalid($validator->singleValidate('', 'required'));
     $this->assertValid($validator->singleValidate('Value here', 'required'));
 }