/** * @param GetResponseForControllerResultEvent $event * * @throws MalformedContentException * @throws UnsupportedContentTypeException */ public function onKernelView(GetResponseForControllerResultEvent $event) { $event->setResponse($this->responseFactory->createResponse($event->getRequest(), $event->getControllerResult())); }
/** * @param array $statusCodes * @param mixed $data * * @param bool $useValidator * * @param bool $stubValid * * @return \Symfony\Component\HttpFoundation\Response * @throws \KleijnWeb\SwaggerBundle\Exception\ValidationException */ private function createResponse(array $statusCodes, $data = null, $useValidator = false, $stubValid = true) { $operationMock = $this->getMockBuilder(Operation::class)->disableOriginalConstructor()->getMock(); $operationMock->expects($this->any())->method('getStatusCodes')->willReturn($statusCodes); $metaMock = $this->getMockBuilder(RequestMeta::class)->disableOriginalConstructor()->getMock(); $metaMock->expects($this->any())->method('getOperation')->willReturn($operationMock); $mockValidator = null; if ($useValidator) { $mockValidator = $this->getMockBuilder(SchemaValidator::class)->disableOriginalConstructor()->getMock(); $mockValidator->expects($this->once())->method('validate')->with($this->isInstanceOf(Schema::class), $data)->willReturn($stubValid ? new ValidationResult(true) : new ValidationResult(false, ['foo' => 'invalid'])); } $hydrator = $this->getMockBuilder(ObjectHydrator::class)->disableOriginalConstructor()->getMock(); /** @var ObjectHydrator $hydrator */ $factory = new ResponseFactory($hydrator, $mockValidator); $request = new Request(); $request->attributes->set(RequestMeta::ATTRIBUTE_URI, 'tests/Functional/PetStore/app/swagger/composite.yml'); $request->attributes->set(RequestMeta::ATTRIBUTE, $metaMock); return $factory->createResponse($request, $data); }