Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Ensure all paths for route result generation are tested #28

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/AuraRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private function marshalFailedRoute(
return RouteResult::fromRouteFailure(Route::HTTP_METHOD_ANY);
}

return RouteResult::fromRouteFailure();
return RouteResult::fromRouteFailure($failedRoute->allows ?: []);
}

/**
Expand All @@ -179,7 +179,7 @@ private function marshalMatchedRoute(AuraRoute $auraRoute) : RouteResult
$route = $this->matchAuraRouteToRoute($auraRoute);
if (! $route) {
// This should likely never occur, but is present for completeness.
return RouteResult::fromRouteFailure();
return RouteResult::fromRouteFailure(Route::HTTP_METHOD_ANY);
}

return RouteResult::fromRoute($route, $auraRoute->attributes);
Expand Down
53 changes: 53 additions & 0 deletions test/AuraRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,4 +503,57 @@ public function testWhenRouteAllowsAnyHttpMethodRouterShouldResultInSuccess($met
$this->assertInstanceOf(RouteResult::class, $result);
$this->assertTrue($result->isSuccess(), 'Routing failed, but should have succeeded');
}

public function testFailedRoutingDueToUnknownCausesResultsInFailureRouteNotDueToMethod()
{
$uri = $this->prophesize(UriInterface::class);
$uri->getPath()->willReturn('/bar');

$request = $this->prophesize(ServerRequestInterface::class);
$request->getUri()->willReturn($uri);
$request->getMethod()->willReturn(RequestMethod::METHOD_GET);
$request->getServerParams()->willReturn([]);

// Not mocking the router container or Aura\Route; this particular test
// is testing how the parts integrate.
$router = new AuraRouter();
$router->addRoute(new Route('/foo', $this->getMiddleware(), []));

$result = $router->match($request->reveal());
$this->assertInstanceOf(RouteResult::class, $result);
$this->assertTrue($result->isFailure(), 'Routing did not fail, but should have');
$this->assertFalse($result->isMethodFailure(), 'Failure was due to HTTP method, but should NOT have been');
}

public function testReturnsRouteFailureForRouteInjectedManuallyIntoBaseRouterButNotRouterBridge()
{
$uri = $this->prophesize(UriInterface::class);
$uri->getPath()->willReturn('/foo');

$request = $this->prophesize(ServerRequestInterface::class);
$request->getUri()->willReturn($uri);
$request->getMethod()->willReturn(RequestMethod::METHOD_GET);
$request->getServerParams()->willReturn([]);

$auraRoute = new AuraRoute();
$auraRoute->name('/foo');
$auraRoute->path('/foo');
$auraRoute->handler('foo');
$auraRoute->allows([RequestMethod::METHOD_GET]);
$auraRoute->attributes([
'action' => 'foo',
'bar' => 'baz',
]);

$this->auraMatcher->match($request)->willReturn($auraRoute);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have $request->reveal() here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes... which is odd, as that should not work correctly otherwise...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see my PR:
https://github.com/zendframework/zend-expressive-zendrouter/pull/32/files
I'm not mocking the request, maybe you should do the same here?


$middleware = $this->getMiddleware();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The $middleware variable is not used

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will remove on merge; nice catch!

$router = $this->getRouter();

$result = $router->match($request->reveal());

$this->assertInstanceOf(RouteResult::class, $result);
$this->assertTrue($result->isFailure());
$this->assertFalse($result->isMethodFailure());
}
}