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);
 }
Ejemplo n.º 2
0
 public function testErrorMessageIsString()
 {
     $constraintGroup = new ConstraintGroup();
     $constraintGroup->addConstraint(StringConstraint::create());
     $validator = Validator::create('Test', 123123)->addConstraintGroup($constraintGroup)->validate();
     $passed = $validator->hasPassed();
     if (!$passed) {
         $error = $validator->getRequirementsString();
         $searchFor = "{$validator->getName()} must ";
         $trimmedError = trim(str_replace($searchFor, '', $error));
         $this->assertGreaterThan(0, strlen($trimmedError));
     }
 }
Ejemplo n.º 3
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);
 }
Ejemplo n.º 4
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);
 }
 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);
 }
Ejemplo n.º 6
0
 public function testNotInArray()
 {
     $constraintGroup = new ConstraintGroup();
     $constraintGroup->addConstraint(NotInArrayConstraint::create()->setArrayValues($this->valuesArray));
     $this->runNotInArray($constraintGroup);
 }
Ejemplo n.º 7
0
 public function testIsNull()
 {
     $constraintGroup = new ConstraintGroup();
     $constraintGroup->addConstraint(NullConstraint::create());
     $this->runIsNull($constraintGroup);
 }
Ejemplo n.º 8
0
 public function testIsFloatMaxValue()
 {
     $constraintGroup = ConstraintGroup::create([FloatConstraint::create()->setMaxValue(1.2)]);
     $this->runIsFloatMaxValue($constraintGroup);
 }
Ejemplo n.º 9
0
 /**
  * Generate the ConstraintGroup the traditional way.
  */
 public function testIsBoolRequiresFalse()
 {
     $constraintGroup = new ConstraintGroup();
     $constraintGroup->addConstraint(BoolConstraint::create()->setRequiredValue(false));
     $this->runTestsWithConstraintGroupRequiresFalse($constraintGroup);
 }
Ejemplo n.º 10
0
 public function testIsNumericMaxValue()
 {
     $constraintGroup = ConstraintGroup::create([NumericConstraint::create()->setMaxValue(1.2)]);
     $this->runIsNumericMaxValue($constraintGroup);
 }
Ejemplo n.º 11
0
 public function testIsIntMaxValue()
 {
     $constraintGroup = ConstraintGroup::create([IntConstraint::create()->setMaxValue(0)]);
     $this->runIsIntMaxValue($constraintGroup);
 }
Ejemplo n.º 12
0
 public function testMaxLengthCheck()
 {
     $constraintGroup = new ConstraintGroup();
     $constraintGroup->addConstraint(StringConstraint::create()->setMaxLength(5));
     $this->runIsStringMaxLength($constraintGroup);
 }
Ejemplo n.º 13
0
 /**
  * Generate the ConstraintGroup the traditional way.
  */
 public function testIsArrayCheck()
 {
     $constraintGroup = new ConstraintGroup();
     $constraintGroup->addConstraint(ArrayConstraint::create());
     $this->runTestsWithConstraintGroup($constraintGroup);
 }
Ejemplo n.º 14
0
 public function testRequiredClass()
 {
     $constraintGroup = new ConstraintGroup();
     $constraintGroup->addConstraint(ObjectConstraint::create()->setRequiredClass('ObjectConstraintTestObject'));
     $this->runIsObjectRequiredClass($constraintGroup);
 }