Browse Source

Implement remaining tests for DispatcherFilter.

mark_story 12 years ago
parent
commit
3c86c3feb2
2 changed files with 46 additions and 17 deletions
  1. 2 2
      src/Routing/DispatcherFilter.php
  2. 44 15
      tests/TestCase/Routing/DispatcherFilterTest.php

+ 2 - 2
src/Routing/DispatcherFilter.php

@@ -24,7 +24,7 @@ use Cake\Event\EventListener;
  * 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 @@ use Cake\Event\EventListener;
  * 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:

+ 44 - 15
tests/TestCase/Routing/DispatcherFilterTest.php

@@ -117,25 +117,27 @@ class DispatcherFilterTest extends TestCase {
 	}
 
 /**
- * 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 @@ class DispatcherFilterTest extends TestCase {
  * @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 @@ class DispatcherFilterTest extends TestCase {
  * @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);
 	}
 
 }