public function testAddCustomConstraintShouldWork()
 {
     $validator = new Validator();
     $validator->registerConstraint(new PasswordConstraint());
     $this->assertTrue($validator->password()->validate("pass@2012"));
     $this->assertFalse($validator->password()->validate("12345678"));
 }
Beispiel #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;
 }
Beispiel #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);
 }
 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");
 }
Beispiel #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"));
 }
 /**
  * @expectedException Melody\Validation\Exceptions\InvalidInputException
  */
 public function testInvalidInputShouldRaiseAnException()
 {
     v::keyExists("name")->validate(new \stdClass());
 }
 public function testInvalidStringShouldFailValidation()
 {
     $this->assertFalse(v::noWhitespace()->validate("abcdef 01234"));
 }
Beispiel #11
0
 /**
  * @dataProvider invalidStringProvider
  */
 public function testInvalidStringShouldNotWork($input)
 {
     $this->assertFalse(v::string()->validate($input));
 }
 /**
  * @expectedException Melody\Validation\Exceptions\InvalidInputException
  */
 public function testInvalidInputShouldRaiseAnException()
 {
     v::maxLength(5)->validate(new \stdClass());
 }
 /**
  * @dataProvider invalidBooleanProvider
  */
 public function testInvalidBooleanShouldNotWork($input)
 {
     $this->assertFalse(v::boolean()->validate($input));
 }
Beispiel #14
0
 /**
  * @expectedException Melody\Validation\Exceptions\InvalidInputException
  */
 public function testInvalidInputShouldRaiseAnException()
 {
     v::length(2, 5)->validate(1);
 }
Beispiel #15
0
 /**
  * @dataProvider invalidNumberProvider
  */
 public function testInvalidNumberShouldNotWork($input)
 {
     $this->assertFalse(v::number()->validate($input));
 }
 /**
  * @dataProvider invalidArrayProvider
  */
 public function testInvalidArrayShouldNotWork($input)
 {
     $this->assertFalse(v::isArray()->validate($input));
 }
Beispiel #17
0
 /**
  * @expectedException Melody\Validation\Exceptions\InvalidInputException
  */
 public function testInvalidInputShouldRaiseAnException()
 {
     v::email()->validate(new \stdClass());
 }
Beispiel #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());
 }
 /**
  * @expectedException Melody\Validation\Exceptions\InvalidParameterException
  */
 public function testInvalidParameterShouldRaiseAnException()
 {
     v::instance(new \stdClass());
 }
 public function keyExists($key, $input)
 {
     return v::keyExists($key)->validate($input);
 }
 /**
  * @dataProvider providerForEmpty
  */
 public function testInvalidStringShouldFailValidation($input)
 {
     $this->assertFalse(v::notEmpty()->validate($input));
 }
Beispiel #23
0
 /**
  * @dataProvider invalidObjectProvider
  */
 public function testInvalidObjectShouldNotWork($input)
 {
     $this->assertFalse(v::object()->validate($input));
 }
Beispiel #24
0
 /**
  * @expectedException Melody\Validation\Exceptions\InvalidInputException
  */
 public function testInvalidInputShouldRaiseAnException()
 {
     v::range(5, 10)->validate("invalid argument");
 }