Beispiel #1
0
 public function testUnionValidator()
 {
     $this->assertFalse($this->validator->validate(new UnionTestClass(0)));
     $this->assertFalse($this->validator->validate(new UnionTestClass(1)));
     $this->assertTrue($this->validator->validate(new UnionTestClass(2)));
     $this->assertTrue($this->validator->validate(new UnionTestClass("string")));
 }
Beispiel #2
0
 public function testAllValidator()
 {
     $this->assertFalse($this->validator->validate(new AllTestClass("baz")));
     //not in enum
     $this->assertFalse($this->validator->validate(new AllTestClass("foobar")));
     //wrong length
     $this->assertTrue($this->validator->validate(new AllTestClass("foo")));
 }
Beispiel #3
0
 /**
  * @param Validator $validator
  * @param           $object
  *
  * @param           $forScenario
  *
  * @return bool
  */
 public function validate(Validator $validator, $object, $forScenario)
 {
     if (!$object instanceof $this->className) {
         return false;
     }
     if ($this->validate) {
         return $validator->validate($object);
     }
     return true;
 }
Beispiel #4
0
 public function testThatValidatorCallsRulesForScenario()
 {
     $std = new \stdClass();
     $std->prop = "foo";
     $std->other = "foo";
     $mockReader = $this->getMock(RuleReader::class, ['getObjectValidator']);
     $validator = new Validator($mockReader);
     $objectValidator = new ObjectValidator();
     $stringRule = new StringRule();
     $numberRule = new Number();
     $booleanRule = new BooleanRule();
     $stringRule->setScenario('string');
     $numberRule->setScenario('number');
     $objectValidator->addPropertyRule('prop', $stringRule);
     $objectValidator->addPropertyRule('prop', $numberRule);
     $mockReader->expects($this->any())->method('getObjectValidator')->with($this->equalTo($std))->will($this->returnValue($objectValidator));
     $this->assertFalse($validator->validate($std, "number"));
     $this->assertTrue($validator->validate($std, "string"));
     $booleanRule->setScenario(['string', 'number']);
     $objectValidator->addPropertyRule('other', $booleanRule);
     $this->assertFalse($validator->validate($std, "string"));
 }