Browse Source

Sketch in RouteBuilder methods for adding scoped middleware.

No tests yet as there is a bunch of RouteCollection work to do.
Mark Story 9 years ago
parent
commit
600439d6ed
2 changed files with 84 additions and 0 deletions
  1. 28 0
      src/Routing/RouteBuilder.php
  2. 56 0
      tests/TestCase/Routing/RouteBuilderTest.php

+ 28 - 0
src/Routing/RouteBuilder.php

@@ -714,4 +714,32 @@ class RouteBuilder
         $this->connect('/:controller', ['action' => 'index'], compact('routeClass'));
         $this->connect('/:controller/:action/*', [], compact('routeClass'));
     }
+
+    /**
+     * Register a middleware with the RouteCollection.
+     *
+     * Once middleware has been registered, it can be applied to the current routing
+     * scope or any child scopes that share the same RoutingCollection.
+     *
+     * @param string $name The name of the middleware. Used when applying middleware to a scope.
+     * @param callable $middleware The middleware object to register.
+     * @return $this
+     * @see \Cake\Routing\RouteCollection
+     */
+    public function registerMiddleware($name, $middleware)
+    {
+    }
+
+    /**
+     * Apply a middleware to the current route scope.
+     *
+     * Requires middleware to be registered via `registerMiddleware()`
+     *
+     * @param string[] ...$names The names of the middleware to apply to the current scope.
+     * @return $this
+     * @see \Cake\Routing\RouteCollection::addMiddlewareToScope()
+     */
+    public function middleware(...$names)
+    {
+    }
 }

+ 56 - 0
tests/TestCase/Routing/RouteBuilderTest.php

@@ -778,4 +778,60 @@ class RouteBuilderTest extends TestCase
         $this->assertArrayHasKey('api:v1:ping', $all);
         $this->assertArrayHasKey('web:pong', $all);
     }
+
+    /**
+     * Test adding middleware to the collection.
+     *
+     * @return void
+     */
+    public function testRegisterMiddleware()
+    {
+        $this->markTestIncomplete();
+    }
+
+    /**
+     * Test registering invalid middleware
+     *
+     * @expectedException \RuntimeException
+     * @expectedExceptionMessage The 'bad' middleware is not callable.
+     * @return void
+     */
+    public function testRegisterMiddlewareObject()
+    {
+        $this->markTestIncomplete();
+    }
+
+    /**
+     * Test registering invalid middleware
+     *
+     * @expectedException \RuntimeException
+     * @expectedExceptionMessage The 'bad' middleware is not callable.
+     * @return void
+     */
+    public function testRegisterMiddlewareString()
+    {
+        $this->markTestIncomplete();
+    }
+
+    /**
+     * Test applying middleware to a scope when it doesn't exist
+     *
+     * @expectedException \RuntimeException
+     * @expectedExceptionMessage Cannot apply 'bad' middleware to /api path. It has not been registered.
+     * @return void
+     */
+    public function testMiddlewareInvalidName()
+    {
+        $this->markTestIncomplete();
+    }
+
+    /**
+     * Test applying middleware to a scope
+     *
+     * @return void
+     */
+    public function testMiddleware()
+    {
+        $this->markTestIncomplete();
+    }
 }