-
Notifications
You must be signed in to change notification settings - Fork 25
allow route options/defaults in uri generation #7
Conversation
$this->assertEquals($uri14, '/foo/123/456'); | ||
//---------------------------------------------------------------------- | ||
|
||
// 2. Route with required and optional parameters, only required is set |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create separate test cases. That way if the first fails, we're still testing the second. If the second depends on setup from the first, have a method for doing the setup and/or use the @depends
annotation, and return $router
from the first (and accept it as an argument in the second).
@weierophinney |
maybe it's better to make the nested 2 tests separate AND independent so we can reuse once more the data provider used for the test without optional route parameters these are passing 55, 56 and 70 in my local setup, if ok i will commit tomorrow. public function uriGeneratorDataProvider()
{
return [
// both param1 and params2 are missing => use route defaults
['/foo/abc/def', []],
// param1 is passed to the uri generator => use it
// param2 is missing => use route default
['/foo/123/def', ['param1' => '123']],
// param1 is missing => use route default
// param2 is passed to the uri generator => use it
['/foo/abc/456', ['param2' => '456']],
// both param1 and param2 are passed to the uri generator
['/foo/123/456', ['param1' => '123', 'param2' => '456']],
];
}
/**
* @dataProvider uriGeneratorDataProvider
*/
public function testUriGenerationSubstitutionsWithDefaultOptions($expectedUri, $params)
{
$router = new FastRouteRouter();
$route = new Route('/foo/{param1}/{param2}', 'foo', ['GET'], 'foo');
$route->setOptions([
'defaults' => [
'param1' => 'abc',
'param2' => 'def',
]
]);
$router->addRoute($route);
$this->assertEquals($expectedUri, $router->generateUri('foo', $params));
}
public function uriGeneratorWithPartialDefaultsDataProvider()
{
return [
// required param1 is missing => use route default
// optional param2 is missing and no route default => skip it
['/foo/abc', []],
// required param1 is passed to the uri generator => use it
// optional param2 is missing and no route default => skip it
['/foo/123', ['param1' => '123']],
// required param1 is missing => use default
// optional param2 is passed to the uri generator => use it
['/foo/abc/456', ['param2' => '456']],
// both param1 and param2 are passed to the uri generator
['/foo/123/456', ['param1' => '123', 'param2' => '456']],
];
}
/**
* @dataProvider uriGeneratorDataProvider
*/
public function testUriGenerationSubstitutionsWithDefaultsAndOptionalParameters($expectedUri, $params)
{
$router = new FastRouteRouter();
$route = new Route('/foo/{param1}/{param2}', 'foo', ['GET'], 'foo');
$route->setOptions([
'defaults' => [
'param1' => 'abc',
'param2' => 'def',
]
]);
$router->addRoute($route);
$this->assertEquals($expectedUri, $router->generateUri('foo', $params));
}
/**
* @dataProvider uriGeneratorWithPartialDefaultsDataProvider
*/
public function testUriGenerationSubstitutionsWithPartialDefaultsAndOptionalParameters($expectedUri, $params)
{
$router = new FastRouteRouter();
$route = new Route('/foo/{param1}[/{param2}]', 'foo', ['GET'], 'foo');
$route->setOptions([
'defaults' => [
'param1' => 'abc',
]
]);
$router->addRoute($route);
$this->assertEquals($expectedUri, $router->generateUri('foo', $params));
} kind regards PS //...
public function uriGeneratorDataProvider()
{
$defaults = [
'param1' => '123',
'param2' => '456',
];
return [
// both param1 and params2 are missing => use route defaults
['/foo/abc', [], $defaults],
//...
//...
|
@weierophinney |
@pine3ree The approaches you suggest for both the tests, and omission of the DefaultParamsTrait, both sound sane; go ahead! |
@weierophinney p.s.
This lead me to another - more general - question: should we use a "zend-expressive" configuration and have the bridge packages translate it to the used implementation or should we stick to the implementation configuration or maybe support both (this last option would mean for example that if i use 'defaults' with aura/router v.2 bridge it will be used as 'values', this is just a |
The idea is that factories will do translation. While we may be able to use the general |
I think we need more test cases. Given the following route definition:
The other routers, IIRC, raise an exception in such cases, as they are unable to fully assemble the URI. |
@weierophinney |
allow route options/defaults in uri generation
this replaces #5 and adds test cases