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

Route options not processed in ApplicationFactory #118

Merged
merged 3 commits into from
Sep 3, 2015
Merged
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
6 changes: 5 additions & 1 deletion src/Container/ApplicationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Zend\Expressive\Emitter\EmitterStack;
use Zend\Expressive\Exception;
use Zend\Expressive\Router\Aura as AuraRouter;
use Zend\Expressive\Router\Route;
Copy link
Contributor

Choose a reason for hiding this comment

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

sort the use statement, place Zend\Expressive\Router\Route before Zend\Expressive\Router\RouterInterface

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

use Zend\Expressive\Router\RouterInterface;

/**
Expand Down Expand Up @@ -167,11 +168,14 @@ private function injectRoutes(Application $app, ContainerInterface $container)
? $spec['allowed_methods']
: null;
$name = isset($spec['name']) ? $spec['name'] : null;
$route = $app->route($spec['path'], $spec['middleware'], $methods, $name);
$methods = (null === $methods) ? Route::HTTP_METHOD_ANY : $methods;
$route = new Route($spec['path'], $spec['middleware'], $methods, $name);

if (isset($spec['options']) && is_array($spec['options'])) {
$route->setOptions($spec['options']);
}

$app->route($route);
}
}

Expand Down
71 changes: 71 additions & 0 deletions test/Container/ApplicationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -997,4 +997,75 @@ public function testCanSpecifyRouteNamesViaConfiguration()
$this->assertInstanceOf(Route::class, $route);
$this->assertEquals('home', $route->getName());
}

public function testCanSpecifyRouteOptionsViaConfiguration()
{
$router = $this->prophesize('Zend\Expressive\Router\RouterInterface');
$emitter = $this->prophesize('Zend\Diactoros\Response\EmitterInterface');
$finalHandler = function ($req, $res, $err = null) {
};

$expected = [
'values' => [
'foo' => 'bar'
],
'tokens' => [
'bar' => 'foo'
]
];
$config = [
'routes' => [
[
'path' => '/',
'middleware' => 'HelloWorld',
'name' => 'home',
'allowed_methods' => ['GET'],
'options' => $expected
],
],
];

$this->container
->has('Zend\Expressive\Router\RouterInterface')
->willReturn(true);
$this->container
->get('Zend\Expressive\Router\RouterInterface')
->will(function () use ($router) {
return $router->reveal();
});

$this->container
->has('Zend\Diactoros\Response\EmitterInterface')
->willReturn(true);
$this->container
->get('Zend\Diactoros\Response\EmitterInterface')
->will(function () use ($emitter) {
return $emitter->reveal();
});

$this->container
->has('Zend\Expressive\FinalHandler')
->willReturn(true);
$this->container
->get('Zend\Expressive\FinalHandler')
->willReturn($finalHandler);

$this->container
->has('config')
->willReturn(true);

$this->container
->get('config')
->willReturn($config);

$app = $this->factory->__invoke($this->container->reveal());

$r = new ReflectionProperty($app, 'routes');
$r->setAccessible(true);
$routes = $r->getValue($app);
$route = array_shift($routes);

$this->assertInstanceOf(Route::class, $route);
$this->assertEquals($expected, $route->getOptions());
}
}