コード例 #1
0
 public function testReUseValidatorWithMultipleValues()
 {
     $constraintGroup = new ConstraintGroup();
     $constraintGroup->addConstraint(IntConstraint::create());
     $validator = Validator::create('Test', null)->addConstraintGroup($constraintGroup);
     $passed = $validator->setValue('asd')->validate()->hasPassed();
     $this->assertFalse($passed);
     $passed = $validator->setValue(5123)->validate()->hasPassed();
     $this->assertTrue($passed);
     $passed = $validator->setValue(false)->validate()->hasPassed();
     $this->assertFalse($passed);
 }
コード例 #2
0
 public function testArrayValidatorWorks()
 {
     $constraintGroup = new ConstraintGroup();
     $constraintGroup->addConstraint(IntConstraint::create()->setMinValue(1)->setMaxValue(9));
     $validator = ArrayValidator::create('Test', [])->addConstraintGroup($constraintGroup);
     $passed = $validator->setValue([1, 3, 4, 5, 6, 8, 9])->validate()->hasPassed();
     $this->assertTrue($passed);
     $passed = $validator->setValue([])->validate()->hasPassed();
     $this->assertTrue($passed);
     $passed = $validator->setValue([0, 2, 4, 5, 6, 8, 9])->validate()->hasPassed();
     $this->assertFalse($passed);
     $passed = $validator->setValue([1, 2, 4, 5, 6, 8, 10])->validate()->hasPassed();
     $this->assertFalse($passed);
 }
コード例 #3
0
 public function testThreeConstraintGroups()
 {
     $constraintGroupOne = new ConstraintGroup();
     $constraintGroupOne->addConstraint(BoolConstraint::create()->requiresTrue());
     $constraintGroupTwo = new ConstraintGroup();
     $constraintGroupTwo->addConstraint(IntConstraint::create()->setMinValue(1)->setMaxValue(1));
     $constraintGroupThree = new ConstraintGroup();
     $constraintGroupThree->addConstraint(NullConstraint::create());
     $passed = Validator::create('Test', 1)->addConstraintGroup($constraintGroupOne)->addConstraintGroup($constraintGroupTwo)->addConstraintGroup($constraintGroupThree)->validate()->hasPassed();
     $this->assertTrue($passed);
     $passed = Validator::create('Test', 0)->addConstraintGroup($constraintGroupOne)->addConstraintGroup($constraintGroupTwo)->addConstraintGroup($constraintGroupThree)->validate()->hasPassed();
     $this->assertFalse($passed);
     $passed = Validator::create('Test', true)->addConstraintGroup($constraintGroupOne)->addConstraintGroup($constraintGroupTwo)->addConstraintGroup($constraintGroupThree)->validate()->hasPassed();
     $this->assertTrue($passed);
     $passed = Validator::create('Test', false)->addConstraintGroup($constraintGroupOne)->addConstraintGroup($constraintGroupTwo)->addConstraintGroup($constraintGroupThree)->validate()->hasPassed();
     $this->assertFalse($passed);
     $passed = Validator::create('Test', null)->addConstraintGroup($constraintGroupOne)->addConstraintGroup($constraintGroupTwo)->addConstraintGroup($constraintGroupThree)->validate()->hasPassed();
     $this->assertTrue($passed);
 }
コード例 #4
0
 public function testValidatorDoesNotThrowExceptions()
 {
     $constraintGroup = new ConstraintGroup();
     $constraintGroup->addConstraint(IntConstraint::create());
     $exceptionThrown = false;
     try {
         // Ensuring default is not to throw exceptions.
         $passed = Validator::create('Test', false)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     } catch (FailedConstraintException $e) {
         $exceptionThrown = true;
         $passed = false;
     }
     $this->assertFalse($passed);
     $this->assertFalse($exceptionThrown);
     $exceptionThrown = false;
     try {
         $passed = Validator::create('Test', false, false)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     } catch (FailedConstraintException $e) {
         $exceptionThrown = true;
         $passed = false;
     }
     $this->assertFalse($passed);
     $this->assertFalse($exceptionThrown);
 }
コード例 #5
0
 public function testIsIntMaxValue()
 {
     $constraintGroup = ConstraintGroup::create([IntConstraint::create()->setMaxValue(0)]);
     $this->runIsIntMaxValue($constraintGroup);
 }