/**
  * @param \stdClass $responseBody
  *
  * @return Validator
  */
 protected function getValidator($responseBody)
 {
     $responseSchema = $this->schemaManager->getResponseSchema($this->path, $this->httpMethod, $this->httpCode);
     $validator = new Validator();
     $validator->check($responseBody, $responseSchema);
     return $validator;
 }
 /**
  * Asserts response headers match with the media types defined.
  *
  * @param string[] $headers
  * @param SchemaManager $schemaManager
  * @param string $path percent-encoded path used on the request.
  * @param string $httpMethod
  * @param int $httpCode
  * @param string $message
  */
 public function assertResponseHeadersMatch(array $headers, SchemaManager $schemaManager, $path, $httpMethod, $httpCode, $message = '')
 {
     if (!$schemaManager->findPathInTemplates($path, $template, $params)) {
         throw new \RuntimeException('Request URI does not match with any swagger path definition');
     }
     $constraint = new ResponseHeadersConstraint($schemaManager, $template, $httpMethod, $httpCode);
     Assert::assertThat($headers, $constraint, $message);
 }
 /**
  * @param \stdClass $headers
  *
  * @return Validator
  */
 protected function getValidator($headers)
 {
     $schema = new \stdClass();
     $headers = (object) array_change_key_case((array) $headers, CASE_LOWER);
     $properties = $this->schemaManager->getResponseHeaders($this->path, $this->httpMethod, $this->httpCode);
     $schema->properties = (object) array_change_key_case((array) $properties, CASE_LOWER);
     $schema->required = array_keys((array) $schema->properties);
     $validator = new Validator();
     $validator->check($headers, $schema);
     return $validator;
 }
Example #4
0
 /**
  * @param string $fullPath
  * @param string $method
  *
  * @return object
  * @throws ApiResponseErrorException
  */
 private function getJsonForLastRequest($fullPath, $method)
 {
     $method = strtolower($method);
     $response = $this->client->getResponse();
     $responseContent = $response->getContent();
     if ($response->getStatusCode() === 204 && !$responseContent) {
         return null;
     }
     $basePath = isset(self::$document->getDefinition()->basePath) ? self::$document->getDefinition()->basePath : '';
     $relativePath = !$basePath ? $fullPath : substr($fullPath, strlen($basePath));
     $json = json_decode($responseContent);
     if ($response->getStatusCode() !== 200) {
         if (!isset($this->validateErrorResponse) || !$this->validateErrorResponse) {
             $this->assertResponseBodyMatch($json, self::$schemaManager, $fullPath, $method, $response->getStatusCode());
         }
         throw new ApiResponseErrorException($json, $response->getStatusCode());
     }
     if (self::$schemaManager->hasPath(['paths', $relativePath, $method, 'responses', '200'])) {
         $this->assertNotNull($json, "Not valid JSON: {$responseContent}");
         $headers = [];
         foreach ($response->headers->all() as $key => $values) {
             $headers[str_replace(' ', '-', ucwords(str_replace('-', ' ', $key)))] = $values[0];
         }
         $this->assertResponseHeadersMatch($headers, self::$schemaManager, $fullPath, $method, 200);
         $this->assertResponseBodyMatch($json, self::$schemaManager, $fullPath, $method, 200);
         return $json;
     }
     // If there is no response definition, the API should return 204 No Content.
     // With the current spec the behavior is undefined, this must be fixed.
     throw new \UnexpectedValueException("There is no 200 response definition for {$relativePath}:{$method}. For empty responses, use 204.");
 }
 /**
  * @dataProvider validPathsProvider
  *
  * @param string $requestPath
  * @param string $expectedTemplate
  * @param array $expectedParameters
  */
 public function testFindPathInTemplatesValid($requestPath, $expectedTemplate, array $expectedParameters)
 {
     self::assertTrue($this->schemaManager->findPathInTemplates($requestPath, $path, $parameters));
     self::assertEquals($expectedTemplate, $path);
     self::assertEquals($expectedParameters, $parameters);
 }
 /**
  * @param SchemaManager $schemaManager
  * @param string $path Swagger path template.
  * @param string $httpMethod
  */
 public function __construct(SchemaManager $schemaManager, $path, $httpMethod)
 {
     parent::__construct();
     $this->mediaTypes = $schemaManager->getResponseMediaTypes($path, $httpMethod);
 }