/**
  * @test
  * @expectedException Mcustiel\SimpleRequest\Exception\InvalidRequestException
  * @expectedExceptionRegExp /^firstName\:/
  */
 public function shouldFailIfTheValidatorFails()
 {
     $this->validator->expects($this->once())->method('validate')->with('Tomato')->will($this->throwException(new InvalidValueException()));
     $this->requestParser->addPropertyParser((new PropertyParserBuilder('firstName'))->addValidator($this->validator)->build($this->getMockBuilder(RequestBuilder::class)->disableOriginalConstructor()->getMock()));
     $this->requestParser->setRequestObject(new PersonRequest());
     $this->requestParser->parse(['firstName' => 'Tomato']);
 }
 /**
  * @test
  */
 public function isValidIfExtraItemsPresentAndValidate()
 {
     $validator = new Type();
     $validator->value = 'number';
     $this->validator->setSpecification(['items' => [$validator, new NotEmpty()], 'additionalItems' => $validator]);
     $this->assertTrue($this->validator->validate([2.3, '', 0]));
 }
 /**
  * @test
  */
 public function failWithPatternPropertiesAndPropertiesWithInvalidValuesAndExtraParamsValidated()
 {
     $validator = new Type();
     $validator->value = 'number';
     $this->validator->setSpecification(['patternProperties' => ['/a/' => $validator], 'properties' => ['otherLetters' => $validator], 'additionalProperties' => $validator]);
     $this->assertFalse($this->validator->validate(['a' => 1, 'hasAnA' => 2, 'otherLetters' => 3.4, 'plus' => 'fail']));
 }
 /**
  * @test
  */
 public function isNotValidIfItemMissingInObject()
 {
     $obj = new \stdClass();
     $obj->a = 1;
     $obj->c = 3;
     $this->validator->setSpecification(['a', 'b']);
     $this->assertFalse($this->validator->validate($obj));
 }
Exemple #5
0
 /**
  * {@inheritdoc}
  *
  * @see \Mcustiel\SimpleRequest\Validator\AbstractAnnotationSpecifiedValidator::validate()
  */
 public function validate($value)
 {
     return !$this->validator->validate($value);
 }
 /**
  * @test
  * @expectedException \Mcustiel\SimpleRequest\Exception\UnspecifiedValidatorException
  * @expectedExceptionMessage The validator is being initialized without an array
  */
 public function failWithNoIterableSpecification()
 {
     $this->validator->setSpecification('No iterable');
 }
 /**
  * Validates an array against a specific validator.
  *
  * @param array                                                 $array
  * @param \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $validator
  */
 private function validateArray(array $array, ValidatorInterface $validator)
 {
     foreach ($array as $item) {
         if (!$validator->validate($item)) {
             return false;
         }
     }
     return true;
 }
 /**
  * @param array                                                 $value
  * @param array                                                 $rest
  * @param string                                                $pattern
  * @param \Mcustiel\SimpleRequest\Interfaces\ValidatorInterface $validator
  *
  * @return bool
  */
 private function validateByPatternUsingValidator(array $value, array &$rest, $pattern, ValidatorInterface $validator)
 {
     foreach ($value as $propertyName => $propertyValue) {
         if (preg_match($pattern, $propertyName)) {
             unset($rest[$propertyName]);
             if (!$validator->validate($propertyValue)) {
                 return false;
             }
         }
     }
     return true;
 }