|
24 | 24 | use Symfony\Component\HttpKernel\EventListener\RouterListener;
|
25 | 25 | use Symfony\Component\HttpKernel\EventListener\ValidateRequestListener;
|
26 | 26 | use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
| 27 | +use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; |
| 28 | +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
27 | 29 | use Symfony\Component\HttpKernel\HttpKernel;
|
28 | 30 | use Symfony\Component\HttpKernel\HttpKernelInterface;
|
| 31 | +use Symfony\Component\Routing\Exception\MethodNotAllowedException; |
29 | 32 | use Symfony\Component\Routing\Exception\NoConfigurationException;
|
| 33 | +use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
30 | 34 | use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
|
31 | 35 | use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
|
32 | 36 | use Symfony\Component\Routing\RequestContext;
|
@@ -217,4 +221,57 @@ public function testRequestWithBadHost()
|
217 | 221 | $listener = new RouterListener($requestMatcher, $this->requestStack, new RequestContext());
|
218 | 222 | $listener->onKernelRequest($event);
|
219 | 223 | }
|
| 224 | + |
| 225 | + public function testResourceNotFoundException() |
| 226 | + { |
| 227 | + $this->expectException(NotFoundHttpException::class); |
| 228 | + $this->expectExceptionMessage('No route found for "GET https://www.symfony.com/path" (from "https://www.google.com")'); |
| 229 | + |
| 230 | + $context = new RequestContext(); |
| 231 | + |
| 232 | + $urlMatcher = $this->createMock(UrlMatcherInterface::class); |
| 233 | + |
| 234 | + $urlMatcher->expects($this->any()) |
| 235 | + ->method('getContext') |
| 236 | + ->willReturn($context); |
| 237 | + |
| 238 | + $urlMatcher->expects($this->any()) |
| 239 | + ->method('match') |
| 240 | + ->willThrowException(new ResourceNotFoundException()); |
| 241 | + |
| 242 | + $kernel = $this->createMock(HttpKernelInterface::class); |
| 243 | + $request = Request::create('https://www.symfony.com/path'); |
| 244 | + $request->headers->set('referer', 'https://www.google.com'); |
| 245 | + |
| 246 | + $event = new RequestEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST); |
| 247 | + |
| 248 | + $listener = new RouterListener($urlMatcher, $this->requestStack); |
| 249 | + $listener->onKernelRequest($event); |
| 250 | + } |
| 251 | + |
| 252 | + public function testMethodNotAllowedException() |
| 253 | + { |
| 254 | + $this->expectException(MethodNotAllowedHttpException::class); |
| 255 | + $this->expectExceptionMessage('No route found for "GET https://www.symfony.com/path": Method Not Allowed (Allow: POST)'); |
| 256 | + |
| 257 | + $context = new RequestContext(); |
| 258 | + |
| 259 | + $urlMatcher = $this->createMock(UrlMatcherInterface::class); |
| 260 | + |
| 261 | + $urlMatcher->expects($this->any()) |
| 262 | + ->method('getContext') |
| 263 | + ->willReturn($context); |
| 264 | + |
| 265 | + $urlMatcher->expects($this->any()) |
| 266 | + ->method('match') |
| 267 | + ->willThrowException(new MethodNotAllowedException(['POST'])); |
| 268 | + |
| 269 | + $kernel = $this->createMock(HttpKernelInterface::class); |
| 270 | + $request = Request::create('https://www.symfony.com/path'); |
| 271 | + |
| 272 | + $event = new RequestEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST); |
| 273 | + |
| 274 | + $listener = new RouterListener($urlMatcher, $this->requestStack); |
| 275 | + $listener->onKernelRequest($event); |
| 276 | + } |
220 | 277 | }
|
0 commit comments