Skip to content

Commit

Permalink
Implement remaining tests for DispatcherFilter.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed May 15, 2014
1 parent 7a808d0 commit 3c86c3f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/Routing/DispatcherFilter.php
Expand Up @@ -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
Expand All @@ -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:
Expand Down
59 changes: 44 additions & 15 deletions tests/TestCase/Routing/DispatcherFilterTest.php
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

}

0 comments on commit 3c86c3f

Please sign in to comment.