Exemplo n.º 1
0
 /**
  * Validate a Response message
  * If the config option "withResponseValidation" is set to FALSE it won't validate the Response
  *
  * @param ResponseInterface $response
  * @param RequestDefinition $definition
  *
  * @throws ConstraintViolations
  */
 private function validateResponse(ResponseInterface $response, RequestDefinition $definition)
 {
     if ($this->config['validateResponse'] === false) {
         return;
     }
     $this->messageValidator->validateResponse($response, $definition);
     if ($this->messageValidator->hasViolations()) {
         throw new ResponseViolations($this->messageValidator->getViolations());
     }
 }
 /** @test */
 public function itValidateAResponse()
 {
     $expectedViolations = [new ConstraintViolation('id', 'String value found, but an integer is required', 'type', 'body'), new ConstraintViolation('X-Required-Header', 'The property X-Required-Header is required', 'required', 'header')];
     $headersSchema = $this->toObject(['type' => 'object', 'required' => ['X-Required-Header'], 'properties' => ['X-Required-Header' => ['type' => 'string']]]);
     $bodySchema = $this->toObject(['type' => 'object', 'properties' => ['id' => ['type' => 'integer', 'format' => 'int32']]]);
     $response = $this->prophesize(ResponseInterface::class);
     $response->getStatusCode()->willReturn('200');
     $response->getBody()->willReturn('{"id": "invalid"}');
     $response->getHeaderLine('Content-Type')->willReturn('application/json');
     $response->getHeaders()->willReturn([]);
     $responseDefinition = $this->prophesize(RequestDefinition::class);
     $responseDefinition->getContentTypes()->willReturn(['application/json']);
     $responseDefinition->hasBodySchema()->willReturn(true);
     $responseDefinition->getBodySchema()->willReturn($bodySchema);
     $responseDefinition->hasHeadersSchema()->willReturn(true);
     $responseDefinition->getHeadersSchema()->willReturn($headersSchema);
     $definition = $this->prophesize(RequestDefinition::class);
     $definition->getResponseDefinition('200')->willReturn($responseDefinition);
     $this->messageValidator->validateResponse($response->reveal(), $definition->reveal());
     assertThat($this->messageValidator->hasViolations(), isTrue());
     assertThat($this->messageValidator->getViolations(), containsOnlyInstancesOf(ConstraintViolation::class));
     assertThat($this->messageValidator->getViolations(), equalTo($expectedViolations));
 }