Browse Source

Allow groups to be redefined.

Mark Story 8 years ago
parent
commit
be84ee8900
2 changed files with 12 additions and 19 deletions
  1. 3 6
      src/Routing/RouteCollection.php
  2. 9 13
      tests/TestCase/Routing/RouteCollectionTest.php

+ 3 - 6
src/Routing/RouteCollection.php

@@ -444,10 +444,6 @@ class RouteCollection
             $message = "Cannot add middleware group '$name'. A middleware by this name has already been registered.";
             throw new RuntimeException($message);
         }
-        if ($this->hasMiddlewareGroup($name)) {
-            $message = "Cannot add middleware group '$name'. A middleware group by this name already exists.";
-            throw new RuntimeException($message);
-        }
 
         foreach ($middlewareNames as $middlewareName) {
             if (!$this->hasMiddleware($middlewareName)) {
@@ -524,8 +520,9 @@ class RouteCollection
     /**
      * Get an array of middleware given a list of names
      *
-     * @param array $names The names of the middleware to fetch
-     * @return array
+     * @param array $names The names of the middleware or groups to fetch
+     * @return array An array of middleware. If any of the passed names are groups,
+     *   the groups middleware will be flattened into the returned list.
      * @throws \RuntimeException when a requested middleware does not exist.
      */
     public function getMiddleware(array $names)

+ 9 - 13
tests/TestCase/Routing/RouteCollectionTest.php

@@ -686,25 +686,21 @@ class RouteCollectionTest extends TestCase
     }
 
     /**
-     * Test adding a middleware group with the same name twice fails.
+     * Test adding a middleware group with the same name overwrites the original list
      *
-     * @expectedException \RuntimeException
-     * @expectedExceptionMessage Cannot add middleware group 'group'. A middleware group by this name already exists.
      * @return void
      */
-    public function testMiddlewareGroupDuplicate()
+    public function testMiddlewareGroupOverwrite()
     {
-        $this->collection->registerMiddleware('closure', function () {
-        });
-
-        $mock = $this->getMockBuilder('\stdClass')
-            ->setMethods(['__invoke'])
-            ->getMock();
-        $result = $this->collection->registerMiddleware('callable', $mock);
-        $this->collection->registerMiddleware('callable', $mock);
+        $stub = function () {
+        };
+        $this->collection->registerMiddleware('closure', $stub);
+        $result = $this->collection->registerMiddleware('callable', $stub);
+        $this->collection->registerMiddleware('callable', $stub);
 
+        $this->collection->middlewareGroup('group', ['callable']);
         $this->collection->middlewareGroup('group', ['closure', 'callable']);
-        $this->collection->middlewareGroup('group', ['closure', 'callable']);
+        $this->assertSame([$stub, $stub], $this->collection->getMiddleware(['group']));
     }
 
     /**