/** * @param boolean $decode use decoder provider * @param Request $request the original request * @param string $method a http method (e.g. POST, GET, PUT, ...) * @param string $contentType the request header content type * @param array $expectedParameters the http parameters of the updated request * * @dataProvider testOnKernelRequestDataProvider */ public function testOnKernelRequest($decode, $request, $method, $contentType, $expectedParameters) { $decoder = $this->getMockBuilder('FOS\\RestBundle\\Decoder\\DecoderInterface')->disableOriginalConstructor()->getMock(); $decoder->expects($this->any())->method('decode')->will($this->returnValue($request->getContent())); $decoderProvider = new ContainerDecoderProvider(array('json' => 'foo')); $listener = new BodyListener($decoderProvider); if ($decode) { $container = $this->getMock('Symfony\\Component\\DependencyInjection\\Container', array('get')); $container->expects($this->once())->method('get')->with('foo')->will($this->returnValue($decoder)); $decoderProvider->setContainer($container); } $request->setMethod($method); $request->headers = new HeaderBag(array('Content-Type' => $contentType)); $event = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent')->disableOriginalConstructor()->getMock(); $event->expects($this->once())->method('getRequest')->will($this->returnValue($request)); $listener->onKernelRequest($event); $this->assertEquals($request->request->all(), $expectedParameters); }
/** * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException */ public function testOnKernelRequestNormalizationException() { $decoder = $this->getMock(DecoderInterface::class); $decoder->expects($this->any())->method('decode')->will($this->returnValue([])); $decoderProvider = $this->getMock(DecoderProviderInterface::class); $decoderProvider->expects($this->any())->method('getDecoder')->will($this->returnValue($decoder)); $decoderProvider->expects($this->any())->method('supports')->will($this->returnValue(true)); $normalizer = $this->getMock(ArrayNormalizerInterface::class); $normalizer->expects($this->once())->method('normalize')->will($this->throwException(new NormalizationException())); $request = new Request([], [], [], [], [], [], 'foo'); $request->setMethod('POST'); $event = $this->getMockBuilder(GetResponseEvent::class)->disableOriginalConstructor()->getMock(); $event->expects($this->once())->method('getRequest')->will($this->returnValue($request)); $listener = new BodyListener($decoderProvider, false, $normalizer); $listener->onKernelRequest($event); }
/** * @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException */ public function testOnKernelRequestNormalizationException() { $decoder = $this->getMock('FOS\\RestBundle\\Decoder\\DecoderInterface'); $decoder->expects($this->any())->method('decode')->will($this->returnValue(array())); $decoderProvider = $this->getMock('FOS\\RestBundle\\Decoder\\DecoderProviderInterface'); $decoderProvider->expects($this->any())->method('getDecoder')->will($this->returnValue($decoder)); $decoderProvider->expects($this->any())->method('supports')->will($this->returnValue(true)); $normalizer = $this->getMock('FOS\\RestBundle\\Normalizer\\ArrayNormalizerInterface'); $normalizer->expects($this->once())->method('normalize')->will($this->throwException(new NormalizationException())); $request = new Request(array(), array(), array(), array(), array(), array(), 'foo'); $request->setMethod('POST'); $event = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent')->disableOriginalConstructor()->getMock(); $event->expects($this->once())->method('getRequest')->will($this->returnValue($request)); $listener = new BodyListener($decoderProvider, false); $listener->setArrayNormalizer($normalizer); $listener->onKernelRequest($event); }