|
|
@@ -20,6 +20,7 @@ use Cake\Routing\RouteBuilder;
|
|
|
use Cake\Routing\RouteCollection;
|
|
|
use Cake\Routing\Route\Route;
|
|
|
use Cake\TestSuite\TestCase;
|
|
|
+use \stdClass;
|
|
|
|
|
|
class RouteCollectionTest extends TestCase
|
|
|
{
|
|
|
@@ -609,4 +610,166 @@ class RouteCollectionTest extends TestCase
|
|
|
$this->collection->extensions(['csv'], false);
|
|
|
$this->assertEquals(['csv'], $this->collection->extensions());
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * String methods are not acceptable.
|
|
|
+ *
|
|
|
+ * @expectedException \RuntimeException
|
|
|
+ * @expectedExceptionMessage The 'bad' middleware is not a callable object.
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testRegisterMiddlewareNoCallableString()
|
|
|
+ {
|
|
|
+ $this->collection->registerMiddleware('bad', 'strlen');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test adding middleware to the collection.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testRegisterMiddleware()
|
|
|
+ {
|
|
|
+ $result = $this->collection->registerMiddleware('closure', function () {
|
|
|
+ });
|
|
|
+ $this->assertSame($result, $this->collection);
|
|
|
+
|
|
|
+ $mock = $this->getMockBuilder('\stdClass')
|
|
|
+ ->setMethods(['__invoke'])
|
|
|
+ ->getMock();
|
|
|
+ $result = $this->collection->registerMiddleware('callable', $mock);
|
|
|
+ $this->assertSame($result, $this->collection);
|
|
|
+
|
|
|
+ $this->assertTrue($this->collection->hasMiddleware('closure'));
|
|
|
+ $this->assertTrue($this->collection->hasMiddleware('callable'));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test adding middleware with a placeholder in the path.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testEnableMiddlewareBasic()
|
|
|
+ {
|
|
|
+ $mock = $this->getMockBuilder('\stdClass')
|
|
|
+ ->setMethods(['__invoke'])
|
|
|
+ ->getMock();
|
|
|
+ $this->collection->registerMiddleware('callable', $mock);
|
|
|
+ $this->collection->registerMiddleware('callback_two', $mock);
|
|
|
+
|
|
|
+ $result = $this->collection->enableMiddleware('/api', ['callable', 'callback_two']);
|
|
|
+ $this->assertSame($result, $this->collection);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test adding middleware with a placeholder in the path.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testGetMatchingMiddlewareBasic()
|
|
|
+ {
|
|
|
+ $mock = $this->getMockBuilder('\stdClass')
|
|
|
+ ->setMethods(['__invoke'])
|
|
|
+ ->getMock();
|
|
|
+ $this->collection->registerMiddleware('callable', $mock);
|
|
|
+ $this->collection->registerMiddleware('callback_two', $mock);
|
|
|
+
|
|
|
+ $result = $this->collection->enableMiddleware('/api', ['callable']);
|
|
|
+ $middleware = $this->collection->getMatchingMiddleware('/api/v1/articles');
|
|
|
+ $this->assertCount(1, $middleware);
|
|
|
+ $this->assertSame($middleware[0], $mock);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test enabling and matching
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testGetMatchingMiddlewareMultiplePaths()
|
|
|
+ {
|
|
|
+ $mock = $this->getMockBuilder('\stdClass')
|
|
|
+ ->setMethods(['__invoke'])
|
|
|
+ ->getMock();
|
|
|
+ $mockTwo = $this->getMockBuilder('\stdClass')
|
|
|
+ ->setMethods(['__invoke'])
|
|
|
+ ->getMock();
|
|
|
+ $this->collection->registerMiddleware('callable', $mock);
|
|
|
+ $this->collection->registerMiddleware('callback_two', $mockTwo);
|
|
|
+
|
|
|
+ $this->collection->enableMiddleware('/api', ['callable']);
|
|
|
+ $this->collection->enableMiddleware('/api/v1/articles', ['callback_two']);
|
|
|
+
|
|
|
+ $middleware = $this->collection->getMatchingMiddleware('/articles');
|
|
|
+ $this->assertCount(0, $middleware);
|
|
|
+
|
|
|
+ $middleware = $this->collection->getMatchingMiddleware('/api/v1/articles/1');
|
|
|
+ $this->assertCount(2, $middleware);
|
|
|
+ $this->assertEquals([$mock, $mockTwo], $middleware, 'Both middleware match');
|
|
|
+
|
|
|
+ $middleware = $this->collection->getMatchingMiddleware('/api/v1/comments');
|
|
|
+ $this->assertCount(1, $middleware);
|
|
|
+ $this->assertEquals([$mock], $middleware, 'Should not match /articles middleware');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test enabling and matching
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testGetMatchingMiddlewareDeduplicate()
|
|
|
+ {
|
|
|
+ $mock = $this->getMockBuilder('\stdClass')
|
|
|
+ ->setMethods(['__invoke'])
|
|
|
+ ->getMock();
|
|
|
+ $mockTwo = $this->getMockBuilder('\stdClass')
|
|
|
+ ->setMethods(['__invoke'])
|
|
|
+ ->getMock();
|
|
|
+ $this->collection->registerMiddleware('callable', $mock);
|
|
|
+ $this->collection->registerMiddleware('callback_two', $mockTwo);
|
|
|
+
|
|
|
+ $this->collection->enableMiddleware('/api', ['callable']);
|
|
|
+ $this->collection->enableMiddleware('/api/v1/articles', ['callback_two', 'callable']);
|
|
|
+
|
|
|
+ $middleware = $this->collection->getMatchingMiddleware('/api/v1/articles/1');
|
|
|
+ $this->assertCount(2, $middleware);
|
|
|
+ $this->assertEquals([$mock, $mockTwo], $middleware, 'Both middleware match');
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test adding middleware with a placeholder in the path.
|
|
|
+ *
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testEnableMiddlewareWithPlaceholder()
|
|
|
+ {
|
|
|
+ $mock = $this->getMockBuilder('\stdClass')
|
|
|
+ ->setMethods(['__invoke'])
|
|
|
+ ->getMock();
|
|
|
+ $this->collection->registerMiddleware('callable', $mock);
|
|
|
+
|
|
|
+ $this->collection->enableMiddleware('/articles/:article_id/comments', ['callable']);
|
|
|
+ $this->markTestIncomplete();
|
|
|
+
|
|
|
+ $middleware = $this->collection->getMatchingMiddleware('/articles/123/comments');
|
|
|
+ $this->assertEquals([$mock], $middleware);
|
|
|
+
|
|
|
+ $middleware = $this->collection->getMatchingMiddleware('/articles/abc-123/comments/99');
|
|
|
+ $this->assertEquals([$mock], $middleware);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test applying middleware to a scope when it doesn't exist
|
|
|
+ *
|
|
|
+ * @expectedException \RuntimeException
|
|
|
+ * @expectedExceptionMessage Cannot apply 'bad' middleware to path '/api'. It has not been registered.
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testEnableMiddlewareUnregistered()
|
|
|
+ {
|
|
|
+ $mock = $this->getMockBuilder('\stdClass')
|
|
|
+ ->setMethods(['__invoke'])
|
|
|
+ ->getMock();
|
|
|
+ $this->collection->registerMiddleware('callable', $mock);
|
|
|
+ $this->collection->enableMiddleware('/api', ['callable', 'bad']);
|
|
|
+ }
|
|
|
}
|