Add Static- and PrefixRoutes

This commit is contained in:
PJ Dietz 2015-04-02 20:10:13 -04:00
parent 506c37ffdd
commit d367f1de79
9 changed files with 250 additions and 0 deletions

View File

@ -0,0 +1,32 @@
<?php
namespace WellRESTed\Routing\Route;
class PrefixRoute extends Route implements PrefixRouteInterface
{
private $prefix;
public function __construct($prefix, $middleware)
{
parent::__construct($middleware);
$this->prefix = $prefix;
}
/**
* @param string $requestTarget
* @param array $captures
* @return bool
*/
public function matchesRequestTarget($requestTarget, &$captures = null)
{
return strrpos($requestTarget, $this->prefix, -strlen($requestTarget)) !== false;
}
/**
* @return string
*/
public function getPrefix()
{
return $this->prefix;
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace WellRESTed\Routing\Route;
interface PrefixRouteInterface extends RouteInterface
{
/**
* @return string
*/
public function getPrefix();
}

View File

@ -0,0 +1,23 @@
<?php
namespace WellRESTed\Routing\Route;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use WellRESTed\Routing\Dispatcher;
abstract class Route implements RouteInterface
{
private $middleware;
public function __construct($middleware)
{
$this->middleware = $middleware;
}
public function dispatch(ServerRequestInterface $request, ResponseInterface &$response)
{
$dispatcher = new Dispatcher();
$dispatcher->dispatch($this->middleware, $request, $response);
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace WellRESTed\Routing\Route;
use WellRESTed\Routing\MiddlewareInterface;
interface RouteInterface extends MiddlewareInterface
{
/**
* Examines a path (request target) and returns whether or not the route
* should handle the request providing the target.
*
* If a successful examination also extracts items (such as captures from
* matching a regular expression), store them to $captures.
*
* $captures should have no meaning for calls that return false.
*
* @param string $requestTarget
* @param array $captures
* @return boolean
*/
public function matchesRequestTarget($requestTarget, &$captures = null);
}

View File

@ -0,0 +1,32 @@
<?php
namespace WellRESTed\Routing\Route;
class StaticRoute extends Route implements StaticRouteInterface
{
private $path;
public function __construct($path, $middleware)
{
parent::__construct($middleware);
$this->path = $path;
}
/**
* @param string $requestTarget
* @param array $captures
* @return bool
*/
public function matchesRequestTarget($requestTarget, &$captures = null)
{
return $requestTarget == $this->path;
}
/**
* @return string
*/
public function getPath()
{
return $this->path;
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace WellRESTed\Routing\Route;
interface StaticRouteInterface extends RouteInterface
{
/**
* @return string
*/
public function getPath();
}

View File

@ -0,0 +1,42 @@
<?php
namespace WellRESTed\Test\Unit\Routing\Route;
use Prophecy\Argument;
use WellRESTed\Routing\Route\PrefixRoute;
/**
* @covers WellRESTed\Routing\Route\PrefixRoute
* @uses WellRESTed\Routing\Route\Route
*/
class PrefixRouteTest extends \PHPUnit_Framework_TestCase
{
private $request;
private $response;
private $middleware;
public function setUp()
{
$this->request = $this->prophesize("\\Psr\\Http\\Message\\ServerRequestInterface");
$this->response = $this->prophesize("\\Psr\\Http\\Message\\ResponseInterface");
$this->middleware = $this->prophesize("\\WellRESTed\\Routing\\MiddlewareInterface");
}
public function testMatchesPrefix()
{
$route = new PrefixRoute("/cats/", $this->middleware->reveal());
$this->assertTrue($route->matchesRequestTarget("/cats/molly"));
}
public function testFailsToMatchWrongPath()
{
$route = new PrefixRoute("/dogs/", $this->middleware->reveal());
$this->assertFalse($route->matchesRequestTarget("/cats/"));
}
public function testReturnsPrefix()
{
$route = new PrefixRoute("/cats/", $this->middleware->reveal());
$this->assertEquals("/cats/", $route->getPrefix());
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace WellRESTed\Test\Unit\Routing\Route;
use Prophecy\Argument;
use WellRESTed\Routing\Route\Route;
use WellRESTed\Routing\Route\StaticRoute;
/**
* @uses WellRESTed\Routing\Route\Route
* @uses WellRESTed\Routing\Route\StaticRoute
*/
class RouteTest extends \PHPUnit_Framework_TestCase
{
private $request;
private $response;
public function setUp()
{
$this->request = $this->prophesize("\\Psr\\Http\\Message\\ServerRequestInterface");
$this->response = $this->prophesize("\\Psr\\Http\\Message\\ResponseInterface");
$this->response->withStatus(Argument::any())->willReturn($this->response->reveal());
}
public function testDispatchesMiddleware()
{
$middleware = function ($request, &$response) {
$response = $response->withStatus(200);
};
$route = new StaticRoute("/", $middleware);
$route->dispatch($this->request->reveal(), $this->response->reveal());
$this->response->withStatus(200)->shouldHaveBeenCalled();
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace WellRESTed\Test\Unit\Routing\Route;
use Prophecy\Argument;
use WellRESTed\Routing\Route\StaticRoute;
/**
* @covers WellRESTed\Routing\Route\StaticRoute
* @uses WellRESTed\Routing\Route\Route
*/
class StaticRouteTest extends \PHPUnit_Framework_TestCase
{
private $request;
private $response;
private $middleware;
public function setUp()
{
$this->request = $this->prophesize("\\Psr\\Http\\Message\\ServerRequestInterface");
$this->response = $this->prophesize("\\Psr\\Http\\Message\\ResponseInterface");
$this->middleware = $this->prophesize("\\WellRESTed\\Routing\\MiddlewareInterface");
}
public function testMatchesPath()
{
$route = new StaticRoute("/cats/", $this->middleware->reveal());
$this->assertTrue($route->matchesRequestTarget("/cats/"));
}
public function testFailsToMatchWrongPath()
{
$route = new StaticRoute("/dogs/", $this->middleware->reveal());
$this->assertFalse($route->matchesRequestTarget("/cats/"));
}
public function testReturnsPaths()
{
$route = new StaticRoute("/cats/", $this->middleware->reveal());
$this->assertEquals("/cats/", $route->getPath());
}
}