示例#1
0
 /**
  * @param Request $request
  * @param array   $operationDefinition
  *
  * @throws MalformedContentException
  * @throws UnsupportedException
  */
 public function coerceRequest(Request $request, array $operationDefinition)
 {
     $content = $this->contentDecoder->decodeContent($request, $operationDefinition);
     if (!isset($operationDefinition['parameters'])) {
         return;
     }
     $paramBagMapping = ['query' => 'query', 'path' => 'attributes', 'header' => 'headers'];
     foreach ($operationDefinition['parameters'] as $paramDefinition) {
         $paramName = $paramDefinition['name'];
         if ($paramDefinition['in'] === 'body') {
             if ($content !== null) {
                 $request->attributes->set($paramName, $content);
             }
             continue;
         }
         if (!isset($paramBagMapping[$paramDefinition['in']])) {
             throw new UnsupportedException("Unsupported parameter 'in' value in definition '{$paramDefinition['in']}'");
         }
         $paramBagName = $paramBagMapping[$paramDefinition['in']];
         if (!$request->{$paramBagName}->has($paramName)) {
             continue;
         }
         $request->attributes->set($paramName, ParameterCoercer::coerceParameter($paramDefinition, $request->{$paramBagName}->get($paramName)));
     }
 }
 protected function setUp()
 {
     $this->contentDecoderMock = $this->getMockBuilder('KleijnWeb\\SwaggerBundle\\Request\\ContentDecoder')->disableOriginalConstructor()->getMock();
     $this->contentDecoderMock->expects($this->any())->method('decodeContent')->willReturnCallback(function (Request $request) {
         return json_decode($request->getContent());
     });
 }
 /**
  * @test
  *
  * @expectedException \KleijnWeb\SwaggerBundle\Exception\MalformedContentException
  */
 public function willThrowMalformedContentExceptionWhenDecodingFails()
 {
     $content = 'NOT VALID JSON';
     $request = new Request([], [], [], [], [], [], $content);
     $request->headers->set('Content-Type', 'application/json');
     $operationDefinition = [];
     $this->contentDecoder->decodeContent($request, $operationDefinition);
 }
 /**
  * @test
  *
  * @expectedException \KleijnWeb\SwaggerBundle\Exception\MalformedContentException
  */
 public function willThrowMalformedContentExceptionWhenDecodingFails()
 {
     $content = 'lkjhlkj';
     $request = new Request([], [], [], [], [], [], $content);
     $request->headers->set('Content-Type', 'application/json');
     $operationDefinition = [];
     $this->jsonDecoderMock->expects($this->once())->method('decode')->with($request->getContent(), 'json');
     $this->contentDecoder->decodeContent($request, $operationDefinition);
 }