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); }
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)); } }
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); }
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); }
public function testNotInArray() { $constraintGroup = new ConstraintGroup(); $constraintGroup->addConstraint(NotInArrayConstraint::create()->setArrayValues($this->valuesArray)); $this->runNotInArray($constraintGroup); }
public function testIsNull() { $constraintGroup = new ConstraintGroup(); $constraintGroup->addConstraint(NullConstraint::create()); $this->runIsNull($constraintGroup); }
public function testIsFloatMaxValue() { $constraintGroup = ConstraintGroup::create([FloatConstraint::create()->setMaxValue(1.2)]); $this->runIsFloatMaxValue($constraintGroup); }
/** * Generate the ConstraintGroup the traditional way. */ public function testIsBoolRequiresFalse() { $constraintGroup = new ConstraintGroup(); $constraintGroup->addConstraint(BoolConstraint::create()->setRequiredValue(false)); $this->runTestsWithConstraintGroupRequiresFalse($constraintGroup); }
public function testIsNumericMaxValue() { $constraintGroup = ConstraintGroup::create([NumericConstraint::create()->setMaxValue(1.2)]); $this->runIsNumericMaxValue($constraintGroup); }
public function testIsIntMaxValue() { $constraintGroup = ConstraintGroup::create([IntConstraint::create()->setMaxValue(0)]); $this->runIsIntMaxValue($constraintGroup); }
public function testMaxLengthCheck() { $constraintGroup = new ConstraintGroup(); $constraintGroup->addConstraint(StringConstraint::create()->setMaxLength(5)); $this->runIsStringMaxLength($constraintGroup); }
/** * Generate the ConstraintGroup the traditional way. */ public function testIsArrayCheck() { $constraintGroup = new ConstraintGroup(); $constraintGroup->addConstraint(ArrayConstraint::create()); $this->runTestsWithConstraintGroup($constraintGroup); }
public function testRequiredClass() { $constraintGroup = new ConstraintGroup(); $constraintGroup->addConstraint(ObjectConstraint::create()->setRequiredClass('ObjectConstraintTestObject')); $this->runIsObjectRequiredClass($constraintGroup); }