/**
  * Runs tests to ensure the value is true
  * @param ConstraintGroup $constraintGroup
  * @throws \TomWright\Validator\Exception\FailedConstraintException
  */
 protected function runTestsWithConstraintGroupRequiresFalse(ConstraintGroup $constraintGroup)
 {
     $passed = Validator::create('Test', true)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertFalse($passed);
     $passed = Validator::create('Test', false)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertTrue($passed);
 }
 protected function runIsStringMaxLength(ConstraintGroup $constraintGroup)
 {
     $check = Validator::create('Test1', 'asda')->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertTrue($check);
     $check = Validator::create('Test2', 'asdas')->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertTrue($check);
     $check = Validator::create('Test3', 'asdasd')->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertFalse($check);
 }
 protected function runIsObjectRequiredClass(ConstraintGroup $constraintGroup)
 {
     $object = new stdClass();
     $object->something = 'hello';
     $passed = Validator::create('Test', $object)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertFalse($passed);
     $object = new ObjectConstraintTestObject();
     $passed = Validator::create('Test', $object)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertTrue($passed);
 }
 protected function runIsFloatMaxValue(ConstraintGroup $constraintGroup)
 {
     $passed = Validator::create('Test', 1.0)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertTrue($passed);
     $passed = Validator::create('Test', 1.1)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertTrue($passed);
     $passed = Validator::create('Test', 1.2)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertTrue($passed);
     $passed = Validator::create('Test', 1.3)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertFalse($passed);
 }
 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);
 }
Example #6
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));
     }
 }
 protected function runNotInArray(ConstraintGroup $constraintGroup)
 {
     $passed = Validator::create('Test', 'egg')->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertFalse($passed);
     $passed = Validator::create('Test', 'tree')->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertFalse($passed);
     $passed = Validator::create('Test', 'trees')->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertTrue($passed);
     $passed = Validator::create('Test', 123)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertTrue($passed);
     $passed = Validator::create('Test', 'something')->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertTrue($passed);
 }
 protected function runIsEmail(ConstraintGroup $constraintGroup)
 {
     $passed = Validator::create('Test', 'TestString')->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertFalse($passed);
     $passed = Validator::create('Test', '*****@*****.**')->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertTrue($passed);
     $passed = Validator::create('Test', 'example@gmail')->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertFalse($passed);
     $passed = Validator::create('Test', 1)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertFalse($passed);
     $passed = Validator::create('Test', true)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertFalse($passed);
 }
 protected function runIsIntMaxValue(ConstraintGroup $constraintGroup)
 {
     $passed = Validator::create('Test', 'TestString')->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertFalse($passed);
     $passed = Validator::create('Test', true)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertFalse($passed);
     $passed = Validator::create('Test', 1)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertFalse($passed);
     $passed = Validator::create('Test', 0)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertTrue($passed);
     $passed = Validator::create('Test', -1)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertTrue($passed);
 }
 protected function runIsNotNull(ConstraintGroup $constraintGroup)
 {
     $check = Validator::create('Test1', 1)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertTrue($check);
     $passed = Validator::create('Test', 'TestString')->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertTrue($passed);
     $passed = Validator::create('Test', null)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertFalse($passed);
     $passed = Validator::create('Test', 123)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertTrue($passed);
     $passed = Validator::create('Test', '')->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertTrue($passed);
     $passed = Validator::create('Test', true)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertTrue($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);
 }
 protected function runTestsWithConstraintGroup(ConstraintGroup $constraintGroup)
 {
     $check = Validator::create('Test1', 1)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertFalse($check);
     $passed = Validator::create('Test', 'TestString')->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertFalse($passed);
     $passed = Validator::create('Test', null)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertFalse($passed);
     $passed = Validator::create('Test', 123)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertFalse($passed);
     $passed = Validator::create('Test', '')->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertFalse($passed);
     $passed = Validator::create('Test', true)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertFalse($passed);
     $passed = Validator::create('Test', ['asdasd', 'asd'])->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertTrue($passed);
     $passed = Validator::create('Test', [25441, 'Rasdee', true])->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertTrue($passed);
     $testObject = new stdClass();
     $testObject->x = 'asd';
     $passed = Validator::create('Test', $testObject)->addConstraintGroup($constraintGroup)->validate()->hasPassed();
     $this->assertFalse($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);
 }