예제 #1
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"));
 }
예제 #2
0
 public function testNumberRangeValidator()
 {
     $validator = new Number(2, 3);
     $this->assertFalse($validator->validate($this->validator, 1, Validator::SCENARIO_ALL));
     $this->assertTrue($validator->validate($this->validator, 2, Validator::SCENARIO_ALL));
     $this->assertTrue($validator->validate($this->validator, 3, Validator::SCENARIO_ALL));
     $this->assertFalse($validator->validate($this->validator, 3.2, Validator::SCENARIO_ALL));
     $this->assertFalse($validator->validate($this->validator, 4, Validator::SCENARIO_ALL));
 }