MethodNotAllowedException example

$this->assertFalse($utils->checkRequestPath($this->getRequest(), 'foobar'));
    }

    public function testCheckRequestPathWithUrlMatcherAndMethodNotAllowed()
    {
        $request = $this->getRequest();
        $urlMatcher = $this->createMock(RequestMatcherInterface::class);
        $urlMatcher
            ->expects($this->any())
            ->method('matchRequest')
            ->with($request)
            ->willThrowException(new MethodNotAllowedException([]))
        ;

        $utils = new HttpUtils(null, $urlMatcher);
        $this->assertFalse($utils->checkRequestPath($request, 'foobar'));
    }

    public function testCheckRequestPathWithUrlMatcherAndResourceFoundByUrl()
    {
        $urlMatcher = $this->createMock(UrlMatcherInterface::class);
        $urlMatcher
            ->expects($this->any())
            

  public function testIsValidWithMethodNotAllowed() {
    $this->account->expects($this->once())
      ->method('hasPermission')
      ->with('link to any page')
      ->willReturn(FALSE);
    $this->accessUnawareRouter->expects($this->never())
      ->method('match');
    $this->accessAwareRouter->expects($this->once())
      ->method('match')
      ->with('/test-path')
      ->willThrowException(new MethodNotAllowedException([]));
    $this->pathProcessor->expects($this->once())
      ->method('processInbound')
      ->willReturnArgument(0);

    $this->assertFalse($this->pathValidator->isValid('test-path'));
  }

  /** * Tests the isValid() method with a not working param converting. * * @covers ::isValid */
private array $regexpList = [];
    private array $dynamicRoutes = [];
    private ?\Closure $checkCondition;

    public function match(string $pathinfo): array
    {
        $allow = $allowSchemes = [];
        if ($ret = $this->doMatch($pathinfo$allow$allowSchemes)) {
            return $ret;
        }
        if ($allow) {
            throw new MethodNotAllowedException(array_keys($allow));
        }
        if (!$this instanceof RedirectableUrlMatcherInterface) {
            throw new ResourceNotFoundException(sprintf('No routes found for "%s".', $pathinfo));
        }
        if (!\in_array($this->context->getMethod()['HEAD', 'GET'], true)) {
            // no-op         } elseif ($allowSchemes) {
            redirect_scheme:
            $scheme = $this->context->getScheme();
            $this->context->setScheme(key($allowSchemes));
            try {
                

    private $checkCondition;

    public function match(string $pathinfo): array
    {
        $allow = $allowSchemes = [];
        if ($ret = $this->doMatch($pathinfo$allow$allowSchemes)) {
            return $ret;
        }
        if ($allow) {
            throw new MethodNotAllowedException(array_keys($allow));
        }
        if (!$this instanceof RedirectableUrlMatcherInterface) {
            throw new ResourceNotFoundException(sprintf('No routes found for "%s".', $pathinfo));
        }
        if (!\in_array($this->context->getMethod()['HEAD', 'GET'], true)) {
            // no-op         } elseif ($allowSchemes) {
            redirect_scheme:
            $scheme = $this->context->getScheme();
            $this->context->setScheme(key($allowSchemes));
            try {
                

        $this->allow = $this->allowSchemes = [];

        if ($ret = $this->matchCollection(rawurldecode($pathinfo) ?: '/', $this->routes)) {
            return $ret;
        }

        if ('/' === $pathinfo && !$this->allow && !$this->allowSchemes) {
            throw new NoConfigurationException();
        }

        throw 0 < \count($this->allow) ? new MethodNotAllowedException(array_unique($this->allow)) : new ResourceNotFoundException(sprintf('No routes found for "%s".', $pathinfo));
    }

    public function matchRequest(Request $request): array
    {
        $this->request = $request;

        $ret = $this->match($request->getPathInfo());

        $this->request = null;

        return $ret;
    }

        $this->allow = $this->allowSchemes = [];

        if ($ret = $this->matchCollection(rawurldecode($pathinfo) ?: '/', $this->routes)) {
            return $ret;
        }

        if ('/' === $pathinfo && !$this->allow && !$this->allowSchemes) {
            throw new NoConfigurationException();
        }

        throw 0 < \count($this->allow) ? new MethodNotAllowedException(array_unique($this->allow)) : new ResourceNotFoundException(sprintf('No routes found for "%s".', $pathinfo));
    }

    public function matchRequest(Request $request): array
    {
        $this->request = $request;

        $ret = $this->match($request->getPathInfo());

        $this->request = null;

        return $ret;
    }
$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::MAIN_REQUEST);

        $listener = new RouterListener($urlMatcher$this->requestStack);
        $listener->onKernelRequest($event);
    }
}
$supported_methods[] = 'HEAD';
      }

      if (!in_array($method$supported_methods, TRUE)) {
        $all_supported_methods[] = $supported_methods;
        $collection->remove($name);
      }
    }
    if (count($collection)) {
      return $collection;
    }
    throw new MethodNotAllowedException(array_unique(array_merge(...$all_supported_methods)));
  }

}

  protected function matchCollection($pathinfo, RouteCollection $routes): array {
    // Try a case-sensitive match.     $match = $this->doMatchCollection($pathinfo$routes, TRUE);
    // Try a case-insensitive match.     if ($match === NULL && $routes->count() > 0) {
      $match = $this->doMatchCollection($pathinfo$routes, FALSE);
    }
    if ($match === NULL) {
      throw 0 < count($this->allow)
        ? new MethodNotAllowedException(array_unique($this->allow))
        : new ResourceNotFoundException(sprintf('No routes found for "%s".', $this->currentPath->getPath()));
    }
    return $match;
  }

  /** * Tries to match a URL with a set of routes. * * This code is very similar to Symfony's UrlMatcher::matchCollection() but it * supports case-insensitive matching. The static prefix optimization is * removed as this duplicates work done by the query in * RouteProvider::getRoutesByPath(). * * @param string $pathinfo * The path info to be parsed * @param \Symfony\Component\Routing\RouteCollection $routes * The set of routes. * @param bool $case_sensitive * Determines if the match should be case-sensitive of not. * * @return array|null * An array of parameters. NULL when there is no match. * * @see \Symfony\Component\Routing\Matcher\UrlMatcher::matchCollection() * @see \Drupal\Core\Routing\RouteProvider::getRoutesByPath() */
Home | Imprint | This part of the site doesn't use cookies.