diff --git a/src/Routing/DispatcherFilter.php b/src/Routing/DispatcherFilter.php index 8b3237c0ee7..abd68d296bd 100644 --- a/src/Routing/DispatcherFilter.php +++ b/src/Routing/DispatcherFilter.php @@ -24,7 +24,7 @@ * event listener with the ability to alter the request or response as needed before it is handled * by a controller or after the response body has already been built. * - * ### Limiting middleware to specific paths + * ### Limiting filters to specific paths * * By using the `for` option you can limit with request paths a filter is applied to. * Both the before and after event will have the same conditions applied to them. For @@ -37,7 +37,7 @@ * When the above middleware is connected to a dispatcher it will only fire * its `beforeDispatch` and `afterDispatch` methods on requests that start with `/blog`. * - * ### Limiting middleware based on conditions + * ### Limiting filters based on conditions * * In addition to simple path based matching you can use a closure to match on arbitrary request * or response conditions. For example: diff --git a/tests/TestCase/Routing/DispatcherFilterTest.php b/tests/TestCase/Routing/DispatcherFilterTest.php index 41ae933385d..bf8aee2d8bf 100644 --- a/tests/TestCase/Routing/DispatcherFilterTest.php +++ b/tests/TestCase/Routing/DispatcherFilterTest.php @@ -116,26 +116,28 @@ public function testMatchesWithForAndWhen() { $this->assertFalse($filter->matches($event)); } -/** - * Test matching with when option. - * - * @expectedException \RuntimeException - * @expectedExceptionMessage 'when' conditions must be a callable. - * @return void - */ - public function testMatchesWithWhenInvalid() { - $this->markTestIncomplete('not done'); - - } - /** * Test event bindings have use condition checker * * @return void */ public function testImplementedEventsMethodName() { - $this->markTestIncomplete('not done'); + $request = new Request(['url' => '/articles/view']); + $response = new Response(); + $beforeEvent = new Event('Dispatcher.beforeDispatch', $this, compact('response', 'request')); + $afterEvent = new Event('Dispatcher.afterDispatch', $this, compact('response', 'request')); + + $filter = $this->getMock('Cake\Routing\DispatcherFilter', ['beforeDispatch', 'afterDispatch']); + $filter->expects($this->at(0)) + ->method('beforeDispatch') + ->with($beforeEvent); + $filter->expects($this->at(1)) + ->method('afterDispatch') + ->with($afterEvent); + + $filter->handle($beforeEvent); + $filter->handle($afterEvent); } /** @@ -144,8 +146,20 @@ public function testImplementedEventsMethodName() { * @return void */ public function testHandleAppliesFor() { - $this->markTestIncomplete('not done'); + $request = new Request(['url' => '/articles/view']); + $response = new Response(); + + $event = new Event('Dispatcher.beforeDispatch', $this, compact('response', 'request')); + $filter = $this->getMock( + 'Cake\Routing\DispatcherFilter', + ['beforeDispatch'], + [['for' => '/admin']] + ); + $filter->expects($this->never()) + ->method('beforeDispatch'); + + $filter->handle($event); } /** @@ -154,8 +168,23 @@ public function testHandleAppliesFor() { * @return void */ public function testHandleAppliesWhen() { - $this->markTestIncomplete('not done'); + $request = new Request(['url' => '/articles/view']); + $response = new Response(); + + $event = new Event('Dispatcher.beforeDispatch', $this, compact('response', 'request')); + $matcher = function() { + return false; + }; + + $filter = $this->getMock( + 'Cake\Routing\DispatcherFilter', + ['beforeDispatch'], + [['when' => $matcher]] + ); + $filter->expects($this->never()) + ->method('beforeDispatch'); + $filter->handle($event); } }