/** * @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); }
/** * @param bool $decode use decoder provider * @param Request $request the original request * @param string $method a http method (e.g. POST, GET, PUT, ...) * @param array $expectedParameters the http parameters of the updated request * @param string $contentType the request header content type * @param bool $throwExceptionOnUnsupportedContentType * * @dataProvider testOnKernelRequestDataProvider */ public function testOnKernelRequest($decode, Request $request, $method, $expectedParameters, $contentType = null, $throwExceptionOnUnsupportedContentType = false) { $decoder = $this->getMockBuilder(DecoderInterface::class)->disableOriginalConstructor()->getMock(); $decoder->expects($this->any())->method('decode')->will($this->returnValue($request->getContent())); $decoderProvider = new ContainerDecoderProvider(['json' => 'foo']); $listener = new BodyListener($decoderProvider, $throwExceptionOnUnsupportedContentType); if ($decode) { $container = $this->getMock('Symfony\\Component\\DependencyInjection\\Container', ['get']); $container->expects($this->once())->method('get')->with('foo')->will($this->returnValue($decoder)); $decoderProvider->setContainer($container); } $request->setMethod($method); if ($contentType) { $request->headers = new HeaderBag(['Content-Type' => $contentType]); } $event = $this->getMockBuilder(GetResponseEvent::class)->disableOriginalConstructor()->getMock(); $event->expects($this->once())->method('getRequest')->will($this->returnValue($request)); $listener->onKernelRequest($event); $this->assertEquals($request->request->all(), $expectedParameters); }