Skip to content

Commit

Permalink
Apply fixtures to group routing
Browse files Browse the repository at this point in the history
  • Loading branch information
divineniiquaye committed May 21, 2020
1 parent f116079 commit 7d9af13
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 5 deletions.
71 changes: 68 additions & 3 deletions Tests/RouterIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,28 @@
*/
abstract class RouterIntegrationTest extends TestCase
{
/**
* Delegate dispatcher selection to routing functionality
* Based on implemented Router.
*
* @return RouterInterface
*/
abstract public function getRouter(): RouterInterface;

/**
* Delegate psr factories on selected router.
*
* @see getRouter() method.
* @return array
*/
abstract public function psrServerResponseFactory(): array;

/**
* The Delegate RouteCollector
*
* @param ContainerInterface $container
* @return RouteCollectorInterface
*/
public function getRouteCollection(ContainerInterface $container = null): RouteCollectorInterface
{
[$serverRequest, $responseFactory] = $this->psrServerResponseFactory();
Expand Down Expand Up @@ -95,6 +113,21 @@ public function method(): Generator
[HttpMethods::METHOD_GET, HttpMethods::METHOD_HEAD],
];

yield 'PUT: put, patch' => [
HttpMethods::METHOD_PATCH,
[HttpMethods::METHOD_PUT, HttpMethods::METHOD_PATCH],
];

yield 'PATCH: patch, put' => [
HttpMethods::METHOD_PUT,
[HttpMethods::METHOD_PUT, HttpMethods::METHOD_PATCH],
];

yield 'DELETE: patch, delete' => [
HttpMethods::METHOD_DELETE,
[HttpMethods::METHOD_DELETE, HttpMethods::METHOD_PATCH],
];

yield 'OPTIONS: options, post' => [
HttpMethods::METHOD_OPTIONS,
[HttpMethods::METHOD_OPTIONS, HttpMethods::METHOD_POST],
Expand All @@ -119,12 +152,12 @@ public function method(): Generator
/**
* @dataProvider method
*/
public function testExplicitRequest(string $method, array $routes)
public function testExplicitWithRouteCollector(string $method, array $routes)
{
$router = $this->getRouteCollection();
[$serverRequest, $responseFactory] = $this->psrServerResponseFactory();

$finalResponse = (new $responseFactory())->createResponse();
$finalResponse = $responseFactory->createResponse();
$finalResponse = $finalResponse->withHeader('foo-bar', 'baz');
$finalResponse->getBody()->write('FOO BAR BODY');

Expand Down Expand Up @@ -169,6 +202,38 @@ public function testExplicitRequest(string $method, array $routes)
$this->assertSame('baz', $response->getHeaderLine('foo-bar'));
}

/**
* @dataProvider method
*/
public function testExplicitWithoutCollector(string $method, array $routes)
{
$router = $this->getRouter();
[$serverRequest, $responseFactory] = $this->psrServerResponseFactory();

$finalHandler = $this->prophesize(RequestHandlerInterface::class);
$finalHandler->handle(Argument::any())->shouldNotBeCalled();

foreach ($routes as $routeMethod) {
$route = new Route(
[$routeMethod],
'/api/v1/me',
$finalHandler->reveal(),
[$responseFactory, 'createResponse'],
new CallableResolver()
);

if ($routeMethod === $method) {
$router->addRoute($route);
}
}

$path = $serverRequest->getUri()->withPath('/api/v1/me');
$results = $router->match($serverRequest->withMethod($method)->withUri($path));

$this->assertTrue($results::FOUND === $results->getRouteStatus());
$this->assertNotFalse($results->getMatchedRoute());
}

public function withoutImplicitMiddleware()
{
// @codingStandardsIgnoreStart
Expand Down Expand Up @@ -402,7 +467,7 @@ public function testWithImplicitRouteGroup(array $groupAttributes, array $assert
$router->setNamespace('Flight\\Routing\\Tests\\');

[$serverRequest,] = $this->psrServerResponseFactory();
$path = $serverRequest->getUri()->withPath('/'.$asserts['path'] ?? 'group/test');
$path = $serverRequest->getUri()->withPath('/'.($asserts['path'] ?? 'group/test'));

$router->group($groupAttributes, function (RouterProxyInterface $route) use ($asserts): void {
$route->get(
Expand Down
28 changes: 26 additions & 2 deletions src/RouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ public function group(array $attributes, $callable): RouteGroupInterface

// Add goups to RouteCollection
$this->routeGroups[] = $routeGroup;
$this->groupOptions = $this->resolveGlobals($routeGroup->getOptions());
$this->groupOptions = $this->resolveGlobals($routeGroup->getOptions(), $oldGroupOption);

// Returns routes on closure, file or on callble
$routeGroup->collectRoutes();
Expand Down Expand Up @@ -660,21 +660,45 @@ protected function setGroupOption(string $name, $value): void
* Resolving patterns and defaults to group.
*
* @param array $groupOptions
* @param array $previousOptions
*
* @return array
*/
protected function resolveGlobals(array $groupOptions): array
protected function resolveGlobals(array $groupOptions, array $previousOptions): array
{
$groupOptions[RouteGroup::REQUIREMENTS] = array_merge(
$previousOptions[RouteGroup::REQUIREMENTS] ?? [],
$this->getGroupOption(RouteGroup::REQUIREMENTS) ?? [],
$groupOptions[RouteGroup::REQUIREMENTS] ?? []
);

$groupOptions[RouteGroup::DEFAULTS] = array_merge(
$previousOptions[RouteGroup::DEFAULTS] ?? [],
$this->getGroupOption(RouteGroup::DEFAULTS) ?? [],
$groupOptions[RouteGroup::DEFAULTS] ?? []
);

$groupOptions[RouteGroup::MIDDLEWARES] = array_merge(
$previousOptions[RouteGroup::MIDDLEWARES] ?? [],
$groupOptions[RouteGroup::MIDDLEWARES] ?? []
);

if (isset($previousOptions[RouteGroup::SCHEMES], $groupOptions[RouteGroup::SCHEMES])) {
$groupOptions[RouteGroup::SCHEMES] = array_merge(
$previousOptions[RouteGroup::SCHEMES] ?? [],
$groupOptions[RouteGroup::SCHEMES] ?? []
);
}
if (isset($previousOptions[RouteGroup::NAME], $groupOptions[RouteGroup::NAME])) {
$groupOptions[RouteGroup::NAME] = $previousOptions[RouteGroup::NAME].$groupOptions[RouteGroup::NAME];
}
if (isset($previousOptions[RouteGroup::PREFIX], $groupOptions[RouteGroup::PREFIX])) {
$groupOptions[RouteGroup::PREFIX] = $previousOptions[RouteGroup::PREFIX].$groupOptions[RouteGroup::PREFIX];
}
if (isset($previousOptions[RouteGroup::NAMESPACE], $groupOptions[RouteGroup::NAMESPACE])) {
$groupOptions[RouteGroup::NAMESPACE] = $previousOptions[RouteGroup::NAMESPACE].$groupOptions[RouteGroup::NAMESPACE];
}

return $groupOptions;
}

Expand Down

0 comments on commit 7d9af13

Please sign in to comment.