Skip to content

Commit

Permalink
Ensure preview works by persisting preview url var
Browse files Browse the repository at this point in the history
Also adding 'cortex.matched-vars' filter to allow customize final vars
array
  • Loading branch information
gmazzap committed Feb 19, 2016
1 parent cb4e856 commit 65d1b84
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 16 deletions.
52 changes: 39 additions & 13 deletions src/Cortex/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function match(UriInterface $uri, $httpMethod)
return $this->results;
}

if (! count($this->routes) || !$this->parseRoutes($uri, $httpMethod)) {
if (! count($this->routes) || ! $this->parseRoutes($uri, $httpMethod)) {
$this->results = new MatchingResult(['route' => null]);

return $this->results;
Expand Down Expand Up @@ -129,7 +129,7 @@ private function parseRoutes(UriInterface $uri, $httpMethod)
/** @var \Brain\Cortex\Route\RouteInterface $route */
foreach ($iterator as $route) {
$route = $this->sanitizeRouteMethod($this->groups->mergeGroup($route), $httpMethod);
if (!$this->validateRoute($route, $uri, $httpMethod)) {
if (! $this->validateRoute($route, $uri, $httpMethod)) {
continue;
}

Expand Down Expand Up @@ -159,7 +159,7 @@ private function parseRoutes(UriInterface $uri, $httpMethod)
*/
private function sanitizeRouteMethod(RouteInterface $route, $httpMethod)
{
if (empty($route['method']) || !(is_string($route['method']) || is_array($route['method']))) {
if (empty($route['method']) || ! (is_string($route['method']) || is_array($route['method']))) {
$route['method'] = $httpMethod;
}

Expand Down Expand Up @@ -227,7 +227,9 @@ private function finalizeRoute(RouteInterface $route, array $vars, UriInterface
{
is_null($route['merge_query_string']) and $route['merge_query_string'] = true;
$merge = filter_var($route['merge_query_string'], FILTER_VALIDATE_BOOLEAN);
$merge and $vars = array_merge($vars, $uri->vars());
$vars = $merge
? array_merge($vars, $uri->vars())
: $this->ensurePreviewVar($vars, $uri, $route);
$result = null;
if (is_callable($route['vars'])) {
$cb = $route['vars'];
Expand All @@ -240,14 +242,38 @@ private function finalizeRoute(RouteInterface $route, array $vars, UriInterface
}
}

return $result instanceof MatchingResult ? $result : new MatchingResult([
'vars' => $vars,
'route' => $route->id(),
'path' => $route['path'],
'handler' => $route['handler'],
'before' => $route['before'],
'after' => $route['after'],
'template' => $route['template'],
]);
$vars = $this->ensurePreviewVar($vars, $uri, $route);
$vars = apply_filters('cortex.matched-vars', $vars, $route, $uri);

return $result instanceof MatchingResult
? $result
: new MatchingResult([
'vars' => (array) $vars,
'route' => $route->id(),
'path' => $route['path'],
'handler' => $route['handler'],
'before' => $route['before'],
'after' => $route['after'],
'template' => $route['template'],
]);
}

/**
* We need this to ensure preview works.
*
* @param array $vars
* @param \Brain\Cortex\Uri\UriInterface $uri
* @param \Brain\Cortex\Route\RouteInterface $route
* @return array
*/
private function ensurePreviewVar(array $vars, UriInterface $uri, RouteInterface $route)
{
$uriVars = $uri->vars();

if (! isset($vars['preview']) && isset($uriVars['preview']) && is_user_logged_in()) {
$vars['preview'] = $uriVars['preview'];
}

return $vars;
}
}
71 changes: 68 additions & 3 deletions tests/src/Unit/Router/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Brain\Cortex\Router\Router;
use Brain\Cortex\Tests\TestCase;
use Brain\Cortex\Uri\UriInterface;
use Brain\Monkey\Functions;
use FastRoute\Dispatcher;
use FastRoute\RouteCollector;

Expand Down Expand Up @@ -222,7 +223,7 @@ public function testMatchMatchingExactMatch()
$uri->shouldReceive('scheme')->andReturn('http');
$uri->shouldReceive('host')->andReturn('example.com');
$uri->shouldReceive('path')->andReturn('foo');
$uri->shouldReceive('vars')->once()->andReturn(['c' => 'C']);
$uri->shouldReceive('vars')->atLeast()->once()->andReturn(['c' => 'C']);
$uri->shouldReceive('chunks')->andReturn(['foo']);

$collector = \Mockery::mock(RouteCollector::class);
Expand Down Expand Up @@ -282,7 +283,7 @@ public function testMatchDynamicMatch()
$uri->shouldReceive('scheme')->andReturn('http');
$uri->shouldReceive('host')->andReturn('example.com');
$uri->shouldReceive('path')->andReturn('foo/i-am-bar');
$uri->shouldReceive('vars')->once()->andReturn(['c' => 'C']);
$uri->shouldReceive('vars')->atLeast()->once()->andReturn(['c' => 'C']);
$uri->shouldReceive('chunks')->andReturn(['foo', 'meh']);

$collector = \Mockery::mock(RouteCollector::class);
Expand Down Expand Up @@ -352,6 +353,7 @@ public function testMatchMatchingExactMatchNoQueryVars()
$uri->shouldReceive('host')->andReturn('example.com');
$uri->shouldReceive('path')->andReturn('foo');
$uri->shouldReceive('chunks')->andReturn(['foo']);
$uri->shouldReceive('vars')->andReturn(['foo' => 'no-way']);

$collector = \Mockery::mock(RouteCollector::class);
$collector->shouldReceive('addRoute')->never();
Expand Down Expand Up @@ -386,6 +388,69 @@ public function testMatchMatchingExactMatchNoQueryVars()
assertSame($expected, $data);
}

public function testMatchMatchingNoQueryVarsMaintainPreviewVar()
{
Functions::when('is_user_logged_in')->justReturn(true);

$handler = function () {
return func_get_args();
};

$route = new Route([
'id' => 'r1',
'path' => '/foo',
'handler' => $handler,
'vars' => ['d' => 'D'],
'method' => 'POST',
'merge_query_string' => false,
]);
$routes = new PriorityRouteCollection();

$routes->addRoute($route);

$groups = \Mockery::mock(GroupCollectionInterface::class);
$groups->shouldReceive('mergeGroup')->once()->with($route)->andReturn($route);

$uri = \Mockery::mock(UriInterface::class);
$uri->shouldReceive('scheme')->andReturn('http');
$uri->shouldReceive('host')->andReturn('example.com');
$uri->shouldReceive('path')->andReturn('foo');
$uri->shouldReceive('chunks')->andReturn(['foo']);
$uri->shouldReceive('vars')->andReturn(['foo' => 'no-way', 'preview' => 1]);

$collector = \Mockery::mock(RouteCollector::class);
$collector->shouldReceive('addRoute')->never();

$dispatcher = \Mockery::mock(Dispatcher::class);
$dispatcher->shouldReceive('dispatch')->never();

$factory = function (array $args) use ($dispatcher) {
assertSame($args, ['foo' => 'bar']);

return $dispatcher;
};

$router = new Router($routes, $groups, $collector, $factory);
$result = $router->match($uri, 'POST');

$expected = [
'route' => 'r1',
'path' => '/foo',
'vars' => ['preview' => 1, 'd' => 'D'],
'handler' => $handler,
'before' => null,
'after' => null,
'template' => null,
];

$proxy = new Proxy($result);
/** @noinspection PhpUndefinedFieldInspection */
$data = $proxy->data;

assertTrue($result->matched());
assertSame($expected, $data);
}

public function testMatchMatchingExactMatchCallableVars()
{
$handler = function () {
Expand All @@ -412,7 +477,7 @@ public function testMatchMatchingExactMatchCallableVars()
$uri->shouldReceive('scheme')->andReturn('http');
$uri->shouldReceive('host')->andReturn('example.com');
$uri->shouldReceive('path')->andReturn('foo');
$uri->shouldReceive('vars')->once()->andReturn(['c' => 'C']);
$uri->shouldReceive('vars')->atLeast()->once()->andReturn(['c' => 'C']);
$uri->shouldReceive('chunks')->andReturn(['foo']);

$collector = \Mockery::mock(RouteCollector::class);
Expand Down

0 comments on commit 65d1b84

Please sign in to comment.