Commits

Thomas Weinert  committed 49a2228

Documented and tested Carica\Io\Network\Http\Route

  • Participants
  • Parent commits 58ca2ff

Comments (0)

Files changed (2)

File src/Carica/Io/Network/Http/Route.php

 
   use Carica\Io;
 
-  class Route {
+  /**
+   * Simple routing class allo to match a path against several targets
+   *
+   */
+  class Route implements \IteratorAggregate {
 
     private $_targets = array();
 
+    /**
+     * Attach a callback as an route target for any path
+     *
+     * @param Callable $callback
+     * @return \Carica\Io\Network\Http\Route\Target\Any
+     */
     public function any(Callable $callback) {
       $this->_targets[] = $target = new Route\Target\Any($callback);
       return $target;
     }
 
+    /**
+     * Attach a callback as a route target for the given path. Each part of the
+     * path can be an string or a named match using {} around the name.
+     *
+     * Example: /some/path/{name}
+     *
+     * @param string $path
+     * @param Callable $callback
+     * @return \Carica\Io\Network\Http\Route\Target\Match
+     */
     public function match($path, Callable $callback) {
       $this->_targets[] = $target = new Route\Target\Match($callback, $path);
       return $target;
     }
 
+    /**
+     * Attach a callback as a route target starting with the given path. This
+     * is not unlike match but ignores if here is path has additional parts.
+     *
+     * @param unknown $path
+     * @param callable $callback
+     * @return \Carica\Io\Network\Http\Route\Target\StartsWith
+     */
     public function startsWith($path, Callable $callback) {
       $this->_targets[] = $target = new Route\Target\StartsWith($callback, $path);
       return $target;
     }
 
+    /**
+     * Allow to trigger the route
+     * @param unknown $request
+     */
     public function __invoke($request) {
       return $this->fire($request);
     }
 
+    /**
+     * Let each attached target executed for the request
+     *
+     * @param Request $request
+     * @return Response|NULL
+     */
     public function fire(Request $request) {
       foreach ($this->_targets as $target) {
         if ($result = $target($request)) {
       }
       return NULL;
     }
+
+    /**
+     * Allow to iterate the attached route targets
+     *
+     * @see IteratorAggregate::getIterator()
+     * @return Iterator
+     */
+    public function getIterator() {
+      return new \ArrayIterator($this->_targets);
+    }
   }
 }

File tests/Carica/Io/Network/Http/RouteTest.php

+<?php
+
+namespace Carica\Io\Network\Http {
+
+  include_once(__DIR__.'/../../Bootstrap.php');
+
+  class RouteTest extends \PHPUnit_Framework_TestCase {
+
+    /**
+     * @covers Carica\Io\Network\Http\Route
+     */
+    public function testAny() {
+      $route = new Route();
+      $route->any($function = function() {});
+      $this->assertEquals(
+        array(
+          new Route\Target\Any($function)
+        ),
+        iterator_to_array($route)
+      );
+    }
+
+    /**
+     * @covers Carica\Io\Network\Http\Route
+     */
+    public function testMatch() {
+      $route = new Route();
+      $route->match('/path', $function = function() {});
+      $this->assertEquals(
+        array(
+          new Route\Target\Match($function, '/path')
+        ),
+        iterator_to_array($route)
+      );
+    }
+
+    /**
+     * @covers Carica\Io\Network\Http\Route
+     */
+    public function testStartsWith() {
+      $route = new Route();
+      $route->startsWith('/path', $function = function() {});
+      $this->assertEquals(
+        array(
+          new Route\Target\StartsWith($function, '/path')
+        ),
+        iterator_to_array($route)
+      );
+    }
+
+    /**
+     * @covers Carica\Io\Network\Http\Route
+     */
+    public function testRoutingOneMatchingTarget() {
+      $request = $this
+        ->getMockBuilder('Carica\Io\Network\Http\Request')
+        ->disableOriginalConstructor()
+        ->getMock();
+      $response = $this
+        ->getMockBuilder('Carica\Io\Network\Http\Response')
+        ->disableOriginalConstructor()
+        ->getMock();
+
+      $route = new Route();
+      $route->any(
+        function() use ($response) {
+          return $response;
+        }
+      );
+      $this->assertSame($response, $route($request));
+    }
+
+    /**
+     * @covers Carica\Io\Network\Http\Route
+     */
+    public function testRoutingNoMatchingTarget() {
+      $request = $this
+        ->getMockBuilder('Carica\Io\Network\Http\Request')
+        ->disableOriginalConstructor()
+        ->getMock();
+
+      $route = new Route();
+      $route->any(
+        function() {
+          return NULL;
+        }
+      );
+      $this->assertNull($route($request));
+    }
+  }
+}