3 namespace Drupal\Tests\Core\Routing;
5 use Drupal\Core\Routing\MethodFilter;
6 use Drupal\Tests\UnitTestCase;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\Routing\Exception\MethodNotAllowedException;
9 use Symfony\Component\Routing\Route;
10 use Symfony\Component\Routing\RouteCollection;
13 * @coversDefaultClass \Drupal\Core\Routing\MethodFilter
16 class MethodFilterTest extends UnitTestCase {
20 * @dataProvider providerApplies
22 public function testApplies(array $route_methods, $expected_applies) {
23 $route = new Route('/test', [], [], [], '', [], $route_methods);
24 $method_filter = new MethodFilter();
26 $this->assertSame($expected_applies, $method_filter->applies($route));
30 * Data provider for testApplies().
34 public function providerApplies() {
36 'only GET' => [['GET'], TRUE],
37 'only PATCH' => [['PATCH'], TRUE],
38 'only POST' => [['POST'], TRUE],
39 'only DELETE' => [['DELETE'], TRUE],
40 'only HEAD' => [['HEAD'], TRUE],
41 'all' => [['GET', 'PATCH', 'POST', 'DELETE', 'HEAD'], TRUE],
42 'none' => [[], FALSE],
49 public function testWithAllowedMethod() {
50 $request = Request::create('/test', 'GET');
51 $collection = new RouteCollection();
52 $collection->add('test_route.get', new Route('/test', [], [], [], '', [], ['GET']));
53 $collection_before = clone $collection;
55 $method_filter = new MethodFilter();
56 $result_collection = $method_filter->filter($collection, $request);
58 $this->assertEquals($collection_before, $result_collection);
64 public function testWithAllowedMethodAndMultipleMatchingRoutes() {
65 $request = Request::create('/test', 'GET');
66 $collection = new RouteCollection();
67 $collection->add('test_route.get', new Route('/test', [], [], [], '', [], ['GET']));
68 $collection->add('test_route2.get', new Route('/test', [], [], [], '', [], ['GET']));
69 $collection->add('test_route3.get', new Route('/test', [], [], [], '', [], ['GET']));
71 $collection_before = clone $collection;
73 $method_filter = new MethodFilter();
74 $result_collection = $method_filter->filter($collection, $request);
76 $this->assertEquals($collection_before, $result_collection);
82 public function testMethodNotAllowedException() {
83 $request = Request::create('/test', 'PATCH');
84 $collection = new RouteCollection();
85 $collection->add('test_route.get', new Route('/test', [], [], [], '', [], ['GET']));
87 $this->setExpectedException(MethodNotAllowedException::class);
89 $method_filter = new MethodFilter();
90 $method_filter->filter($collection, $request);
96 public function testMethodNotAllowedExceptionWithMultipleRoutes() {
97 $request = Request::create('/test', 'PATCH');
98 $collection = new RouteCollection();
99 $collection->add('test_route.get', new Route('/test', [], [], [], '', [], ['GET']));
100 $collection->add('test_route2.get', new Route('/test', [], [], [], '', [], ['GET']));
101 $collection->add('test_route3.get', new Route('/test', [], [], [], '', [], ['GET']));
103 $this->setExpectedException(MethodNotAllowedException::class);
105 $method_filter = new MethodFilter();
106 $method_filter->filter($collection, $request);
112 public function testFilteredMethods() {
113 $request = Request::create('/test', 'PATCH');
114 $collection = new RouteCollection();
115 $collection->add('test_route.get', new Route('/test', [], [], [], '', [], ['GET']));
116 $collection->add('test_route2.get', new Route('/test', [], [], [], '', [], ['PATCH']));
117 $collection->add('test_route3.get', new Route('/test', [], [], [], '', [], ['POST']));
119 $expected_collection = new RouteCollection();
120 $expected_collection->add('test_route2.get', new Route('/test', [], [], [], '', [], ['PATCH']));
122 $method_filter = new MethodFilter();
123 $result_collection = $method_filter->filter($collection, $request);
125 $this->assertEquals($expected_collection, $result_collection);