/** */ 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')); }
/** */ 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')); }
/** */ 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')); }
/** */ 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')); }
/** * @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); }
/** * @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')); } }
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); }
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__); }
/** */ 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); }
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')); }
/** */ 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')); }
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')); }
/** * @expectedException SimpleValidatorException */ public function testInvalidRuleException() { $validator = new SimpleValidator(); $validator->singleValidate('sample text', 'alphaNumBlahBlah'); }
/** * @dataProvider data_BadCities */ public function testRuleInvalidCity($city) { $validator = new SimpleValidator(); $this->assertInvalid($validator->singleValidate($city, 'city')); }
/** */ public function testRulePhrase() { $validator = new SimpleValidator(); $this->assertValid($validator->singleValidate('Text with spaces', 'phrase')); $this->assertInvalid($validator->singleValidate('Text-with/extracharacters', 'phrase')); }
/** */ public function testRuleNone() { $validator = new SimpleValidator(); $this->assertValid($validator->singleValidate($this->randChoice(), 'none')); $this->assertValid($validator->singleValidate("4%6&#\$@\\}", 'none')); }
/** * @dataProvider data_Dates */ public function testRuleDate($valid, $date) { $validator = new SimpleValidator(); $method = $valid ? 'assertValid' : 'assertInvalid'; $this->{$method}($validator->singleValidate($date, 'date')); }
public function testRuleAlpha() { $validator = new SimpleValidator(); $this->assertValid($validator->singleValidate('abcdef', 'alpha')); $this->assertInvalid($validator->singleValidate('999', 'alpha')); }
/** * @dataProvider data_Addresses */ public function testRuleEmail($valid, $address) { $validator = new SimpleValidator(); $method = $valid ? 'assertValid' : 'assertInvalid'; $this->{$method}($validator->singleValidate($address, 'url')); }
/** */ public function testRuleNumeric() { $validator = new SimpleValidator(); $this->assertValid($validator->singleValidate('1234', 'numeric')); $this->assertInvalid($validator->singleValidate('abc', 'numeric')); }
/** */ public function testRuleRequired() { $validator = new SimpleValidator(); $this->assertInvalid($validator->singleValidate('', 'required')); $this->assertValid($validator->singleValidate('Value here', 'required')); }