예제 #1
0
 public function testAddCustomConstraintShouldWork()
 {
     $validator = new Validator();
     $validator->registerConstraint(new PasswordConstraint());
     $this->assertTrue($validator->password()->validate("pass@2012"));
     $this->assertFalse($validator->password()->validate("12345678"));
 }
예제 #2
0
 public function add(Validator $validatorBuilder)
 {
     $builder = self::create();
     $builder->constraints = clone $this->constraints;
     $constraints = $validatorBuilder->getConstraints();
     if (count($constraints)) {
         foreach ($constraints as $name => $constraint) {
             $builder->constraints->set($name, $constraint);
         }
     }
     return $builder;
 }
 /**
  * @param string $string
  *
  * @return \Melody\Validation\ValidationGroups\Validator
  */
 protected function parseString($string)
 {
     $validator = new Validator();
     $rules = explode("|", $string);
     foreach ($rules as $rule) {
         $parts = explode(":", $rule);
         $class = array_shift($parts);
         $options = $parts;
         $validator->set($class, $options);
     }
     return $validator;
 }
예제 #4
0
 /**
  * @dataProvider invalidDateProvider
  */
 public function testInvalidDateWithCustomViolationMessageShouldNotWork($input)
 {
     $customMessage = "Custom date violation message";
     $dateValidator = v::date();
     $this->assertFalse($dateValidator->validate($input));
     $this->assertEquals($dateValidator->getViolation('date', $customMessage), $customMessage);
 }
예제 #5
0
 public function testCustomMessagesFromChainedValidatorConfiguration()
 {
     $validEmail = v::email();
     $validEmail->validate("email @gmail.com");
     $errors = $validEmail->getViolations(array('email' => "'{{input}}' deve conter um email válido"));
     $this->assertEquals($errors['email'], "'email @gmail.com' deve conter um email válido");
 }
예제 #6
0
 public function validate($input)
 {
     if (!v::isArray()->validate($input)) {
         throw new InvalidInputException("The input must be an array");
     }
     return array_key_exists($this->key, $input);
 }
 public function testShouldReturnFalseWhenSimpleObjectInvalidUsingGetterMethod()
 {
     $mock = new Mock();
     $mock->setName("pedro");
     $config['registering'] = array('name' => v::maxLength(1));
     $validationObject = ValidationGroupsFactory::build(new ArrayParserObject($config));
     $result = $validationObject->validate($mock, "registering", array('name' => "'{{input}}' invalid name"));
     $errors = $validationObject->getViolations();
     $this->assertFalse($result);
     $this->assertEquals("'{{input}}' invalid name", $errors["name"]);
 }
 public function testValidationGroupsMethods()
 {
     $constraintsCollection = new ConstraintsCollection();
     $constraintsCollection->set('name', v::maxLength(50));
     $constraintsCollection->set('email', v::email()->maxLength(50));
     $validationGroups = new ValidationGroups();
     $validationGroups->add("registering", $constraintsCollection);
     $this->assertTrue($validationGroups->has("registering"));
     $this->assertInstanceOf('Melody\\Validation\\Common\\Collections\\ConstraintsCollection', $validationGroups->get("registering"));
     $validationGroups->remove("registering");
     $this->assertFalse($validationGroups->has("registering"));
     $this->setExpectedException('InvalidArgumentException');
     $this->assertInstanceOf('InvalidArgumentException', $validationGroups->get("registering"));
 }
예제 #9
0
 /**
  * @expectedException Melody\Validation\Exceptions\InvalidInputException
  */
 public function testInvalidInputShouldRaiseAnException()
 {
     v::keyExists("name")->validate(new \stdClass());
 }
예제 #10
0
 public function testInvalidStringShouldFailValidation()
 {
     $this->assertFalse(v::noWhitespace()->validate("abcdef 01234"));
 }
예제 #11
0
 /**
  * @dataProvider invalidStringProvider
  */
 public function testInvalidStringShouldNotWork($input)
 {
     $this->assertFalse(v::string()->validate($input));
 }
예제 #12
0
 /**
  * @expectedException Melody\Validation\Exceptions\InvalidInputException
  */
 public function testInvalidInputShouldRaiseAnException()
 {
     v::maxLength(5)->validate(new \stdClass());
 }
예제 #13
0
 /**
  * @dataProvider invalidBooleanProvider
  */
 public function testInvalidBooleanShouldNotWork($input)
 {
     $this->assertFalse(v::boolean()->validate($input));
 }
예제 #14
0
 /**
  * @expectedException Melody\Validation\Exceptions\InvalidInputException
  */
 public function testInvalidInputShouldRaiseAnException()
 {
     v::length(2, 5)->validate(1);
 }
예제 #15
0
 /**
  * @dataProvider invalidNumberProvider
  */
 public function testInvalidNumberShouldNotWork($input)
 {
     $this->assertFalse(v::number()->validate($input));
 }
예제 #16
0
 /**
  * @dataProvider invalidArrayProvider
  */
 public function testInvalidArrayShouldNotWork($input)
 {
     $this->assertFalse(v::isArray()->validate($input));
 }
예제 #17
0
 /**
  * @expectedException Melody\Validation\Exceptions\InvalidInputException
  */
 public function testInvalidInputShouldRaiseAnException()
 {
     v::email()->validate(new \stdClass());
 }
예제 #18
0
<?php

use Melody\Validation\Validator as v;
$config['registering'] = array('name' => v::maxLength(50), 'email' => v::email()->maxLength(50), 'username' => v::length(6, 12)->alnum()->noWhitespace(), 'password' => v::length(6, 12)->containsSpecial(1)->containsLetter(3)->containsDigit(2)->noWhitespace());
return $config;
 /**
  * @expectedException Melody\Validation\Exceptions\InvalidInputException
  */
 public function testInvalidInputShouldRaiseAnException()
 {
     v::containsLetter(5)->validate(new \stdClass());
 }
예제 #20
0
 /**
  * @expectedException Melody\Validation\Exceptions\InvalidParameterException
  */
 public function testInvalidParameterShouldRaiseAnException()
 {
     v::instance(new \stdClass());
 }
예제 #21
0
 public function keyExists($key, $input)
 {
     return v::keyExists($key)->validate($input);
 }
예제 #22
0
 /**
  * @dataProvider providerForEmpty
  */
 public function testInvalidStringShouldFailValidation($input)
 {
     $this->assertFalse(v::notEmpty()->validate($input));
 }
예제 #23
0
 /**
  * @dataProvider invalidObjectProvider
  */
 public function testInvalidObjectShouldNotWork($input)
 {
     $this->assertFalse(v::object()->validate($input));
 }
예제 #24
0
 /**
  * @expectedException Melody\Validation\Exceptions\InvalidInputException
  */
 public function testInvalidInputShouldRaiseAnException()
 {
     v::range(5, 10)->validate("invalid argument");
 }