diff --git a/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php b/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php index c10897a9e2..9c36f7cb58 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/RouterListener.php @@ -127,7 +127,7 @@ class RouterListener implements EventSubscriberInterface unset($parameters['_route'], $parameters['_controller']); $request->attributes->set('_route_params', $parameters); } catch (ResourceNotFoundException $e) { - $message = sprintf('No route found for "%s %s"', $request->getMethod(), $request->getPathInfo()); + $message = sprintf('No route found for "%s %s"', $request->getMethod(), $request->getUriForPath($request->getPathInfo())); if ($referer = $request->headers->get('referer')) { $message .= sprintf(' (from "%s")', $referer); @@ -135,7 +135,7 @@ class RouterListener implements EventSubscriberInterface throw new NotFoundHttpException($message, $e); } catch (MethodNotAllowedException $e) { - $message = sprintf('No route found for "%s %s": Method Not Allowed (Allow: %s)', $request->getMethod(), $request->getPathInfo(), implode(', ', $e->getAllowedMethods())); + $message = sprintf('No route found for "%s %s": Method Not Allowed (Allow: %s)', $request->getMethod(), $request->getUriForPath($request->getPathInfo()), implode(', ', $e->getAllowedMethods())); throw new MethodNotAllowedHttpException($e->getAllowedMethods(), $message, $e); } diff --git a/src/Symfony/Component/HttpKernel/Tests/EventListener/RouterListenerTest.php b/src/Symfony/Component/HttpKernel/Tests/EventListener/RouterListenerTest.php index cc46fd8db5..453bf5b0e3 100644 --- a/src/Symfony/Component/HttpKernel/Tests/EventListener/RouterListenerTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/EventListener/RouterListenerTest.php @@ -24,9 +24,13 @@ use Symfony\Component\HttpKernel\EventListener\ErrorListener; use Symfony\Component\HttpKernel\EventListener\RouterListener; use Symfony\Component\HttpKernel\EventListener\ValidateRequestListener; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; +use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\HttpKernel; use Symfony\Component\HttpKernel\HttpKernelInterface; +use Symfony\Component\Routing\Exception\MethodNotAllowedException; use Symfony\Component\Routing\Exception\NoConfigurationException; +use Symfony\Component\Routing\Exception\ResourceNotFoundException; use Symfony\Component\Routing\Matcher\RequestMatcherInterface; use Symfony\Component\Routing\Matcher\UrlMatcherInterface; use Symfony\Component\Routing\RequestContext; @@ -217,4 +221,57 @@ class RouterListenerTest extends TestCase $listener = new RouterListener($requestMatcher, $this->requestStack, new RequestContext()); $listener->onKernelRequest($event); } + + public function testResourceNotFoundException() + { + $this->expectException(NotFoundHttpException::class); + $this->expectExceptionMessage('No route found for "GET https://www.symfony.com/path" (from "https://www.google.com")'); + + $context = new RequestContext(); + + $urlMatcher = $this->createMock(UrlMatcherInterface::class); + + $urlMatcher->expects($this->any()) + ->method('getContext') + ->willReturn($context); + + $urlMatcher->expects($this->any()) + ->method('match') + ->willThrowException(new ResourceNotFoundException()); + + $kernel = $this->createMock(HttpKernelInterface::class); + $request = Request::create('https://www.symfony.com/path'); + $request->headers->set('referer', 'https://www.google.com'); + + $event = new RequestEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST); + + $listener = new RouterListener($urlMatcher, $this->requestStack); + $listener->onKernelRequest($event); + } + + public function testMethodNotAllowedException() + { + $this->expectException(MethodNotAllowedHttpException::class); + $this->expectExceptionMessage('No route found for "GET https://www.symfony.com/path": Method Not Allowed (Allow: POST)'); + + $context = new RequestContext(); + + $urlMatcher = $this->createMock(UrlMatcherInterface::class); + + $urlMatcher->expects($this->any()) + ->method('getContext') + ->willReturn($context); + + $urlMatcher->expects($this->any()) + ->method('match') + ->willThrowException(new MethodNotAllowedException(['POST'])); + + $kernel = $this->createMock(HttpKernelInterface::class); + $request = Request::create('https://www.symfony.com/path'); + + $event = new RequestEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST); + + $listener = new RouterListener($urlMatcher, $this->requestStack); + $listener->onKernelRequest($event); + } }