Skip to content

Commit

Permalink
Rename method to be consistent and use a verb.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Apr 26, 2017
1 parent 0b2ae63 commit c2b9a42
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/Routing/RouteBuilder.php
Expand Up @@ -742,9 +742,9 @@ public function registerMiddleware($name, $middleware)
* @return $this
* @see \Cake\Routing\RouteCollection::addMiddlewareToScope()
*/
public function middleware(...$names)
public function applyMiddleware(...$names)
{
$this->_collection->enableMiddleware($this->_path, $names);
$this->_collection->applyMiddleware($this->_path, $names);

return $this;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Routing/RouteCollection.php
Expand Up @@ -411,13 +411,13 @@ public function hasMiddleware($name)
}

/**
* Enable a registered middleware(s) for the provided path
* Apply a registered middleware(s) for the provided path
*
* @param string $path The URL path to register middleware for.
* @param string[] $middleware The middleware names to add for the path.
* @return $this
*/
public function enableMiddleware($path, array $middleware)
public function applyMiddleware($path, array $middleware)
{
foreach ($middleware as $name) {
if (!$this->hasMiddleware($name)) {
Expand Down
8 changes: 4 additions & 4 deletions tests/TestCase/Routing/RouteBuilderTest.php
Expand Up @@ -816,25 +816,25 @@ public function testRegisterMiddlewareString()
* @expectedExceptionMessage Cannot apply 'bad' middleware to path '/api'. It has not been registered.
* @return void
*/
public function testMiddlewareInvalidName()
public function testApplyMiddlewareInvalidName()
{
$routes = new RouteBuilder($this->collection, '/api');
$routes->middleware('bad');
$routes->applyMiddleware('bad');
}

/**
* Test applying middleware to a scope
*
* @return void
*/
public function testMiddleware()
public function testApplyMiddleware()
{
$func = function () {
};
$routes = new RouteBuilder($this->collection, '/api');
$routes->registerMiddleware('test', $func)
->registerMiddleware('test2', $func);
$result = $routes->middleware('test', 'test2');
$result = $routes->applyMiddleware('test', 'test2');

$this->assertSame($result, $routes);
$this->assertEquals(
Expand Down
22 changes: 11 additions & 11 deletions tests/TestCase/Routing/RouteCollectionTest.php
Expand Up @@ -649,15 +649,15 @@ public function testRegisterMiddleware()
*
* @return void
*/
public function testEnableMiddlewareBasic()
public function testApplyMiddlewareBasic()
{
$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']);
$result = $this->collection->applyMiddleware('/api', ['callable', 'callback_two']);
$this->assertSame($result, $this->collection);
}

Expand All @@ -674,7 +674,7 @@ public function testGetMatchingMiddlewareBasic()
$this->collection->registerMiddleware('callable', $mock);
$this->collection->registerMiddleware('callback_two', $mock);

$result = $this->collection->enableMiddleware('/api', ['callable']);
$result = $this->collection->applyMiddleware('/api', ['callable']);
$middleware = $this->collection->getMatchingMiddleware('/api/v1/articles');
$this->assertCount(1, $middleware);
$this->assertSame($middleware[0], $mock);
Expand All @@ -696,8 +696,8 @@ public function testGetMatchingMiddlewareMultiplePaths()
$this->collection->registerMiddleware('callable', $mock);
$this->collection->registerMiddleware('callback_two', $mockTwo);

$this->collection->enableMiddleware('/api', ['callable']);
$this->collection->enableMiddleware('/api/v1/articles', ['callback_two']);
$this->collection->applyMiddleware('/api', ['callable']);
$this->collection->applyMiddleware('/api/v1/articles', ['callback_two']);

$middleware = $this->collection->getMatchingMiddleware('/articles');
$this->assertCount(0, $middleware);
Expand Down Expand Up @@ -727,8 +727,8 @@ public function testGetMatchingMiddlewareDeduplicate()
$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']);
$this->collection->applyMiddleware('/api', ['callable']);
$this->collection->applyMiddleware('/api/v1/articles', ['callback_two', 'callable']);

$middleware = $this->collection->getMatchingMiddleware('/api/v1/articles/1');
$this->assertCount(2, $middleware);
Expand All @@ -740,14 +740,14 @@ public function testGetMatchingMiddlewareDeduplicate()
*
* @return void
*/
public function testEnableMiddlewareWithPlaceholder()
public function testApplyMiddlewareWithPlaceholder()
{
$mock = $this->getMockBuilder('\stdClass')
->setMethods(['__invoke'])
->getMock();
$this->collection->registerMiddleware('callable', $mock);

$this->collection->enableMiddleware('/api-:version/articles/:article_id/comments', ['callable']);
$this->collection->applyMiddleware('/api-:version/articles/:article_id/comments', ['callable']);

$middleware = $this->collection->getMatchingMiddleware('/api-1/articles/comments');
$this->assertEmpty($middleware);
Expand All @@ -769,12 +769,12 @@ public function testEnableMiddlewareWithPlaceholder()
* @expectedExceptionMessage Cannot apply 'bad' middleware to path '/api'. It has not been registered.
* @return void
*/
public function testEnableMiddlewareUnregistered()
public function testApplyMiddlewareUnregistered()
{
$mock = $this->getMockBuilder('\stdClass')
->setMethods(['__invoke'])
->getMock();
$this->collection->registerMiddleware('callable', $mock);
$this->collection->enableMiddleware('/api', ['callable', 'bad']);
$this->collection->applyMiddleware('/api', ['callable', 'bad']);
}
}

0 comments on commit c2b9a42

Please sign in to comment.