Ejemplo n.º 1
0
$url = '/customer/create';
$params = array('module' => 'customer', 'action' => 'create');
$t->is($r->parse($url), $params, '->parse() /:module/:action route');
$url = '/customer/param1/action';
$params = array('module' => 'default', 'action' => 'action', 'param1' => 'param1');
$t->is($r->parse($url), $params, '->parse() /customer/:param1/:action/* route');
$r->clearRoutes();
$r->connect('test', new sfRoute('/customer/:id/:id_name', array('module' => 'default')));
$t->is($r->generate('', array('id' => 2, 'id_name' => 'fabien')), '/customer/2/fabien', '->generate() first replaces the longest variable names');
$r->clearRoutes();
$r->connect('default', new sfAlwaysAbsoluteRoute('/:module/:action'));
$t->is($r->generate('', array('module' => 'foo', 'action' => 'bar')), 'http://localhost/foo/bar', '->generate() allows route to generate absolute urls');
$t->is($r->generate('', array('module' => 'foo', 'action' => 'bar'), true), 'http://localhost/foo/bar', '->generate() does not double-absolutize urls');
$t->diag('suffix handling with generate_shortest_url option');
$r = new sfPatternRoutingTest(new sfEventDispatcher(), null, array('generate_shortest_url' => true, 'extra_parameters_as_query_string' => false, 'suffix' => '.html'));
$r->connect('test2', new sfRoute('/users/:username/:sort/:start/', array('module' => 'user', 'action' => 'show', 'sort' => 'all', 'start' => '0'), array('requirements' => array('username' => '\\w+', 'start' => '\\d+'))));
$t->is($r->generate('', array('username' => 'test1', 'module' => 'user', 'action' => 'show')), '/users/test1/', '->generate() creates URL when using suffix and generate_shortest_url');
$t->is($r->generate('', array('username' => 'test1', 'module' => 'user', 'action' => 'show', 'sort' => 'all', 'start' => '1')), '/users/test1/all/1/', '->generate() creates URL when using suffix and generate_shortest_url');
$t->is($r->parse('/users/test1/'), array('module' => 'user', 'action' => 'show', 'sort' => 'all', 'start' => '0', 'username' => 'test1'), '->parse() returns all default parameters when provided suffix and generate_shortest_url enabled with / suffix');
$r = new sfPatternRoutingTest(new sfEventDispatcher(), null, array('generate_shortest_url' => true, 'extra_parameters_as_query_string' => false, 'suffix' => '.html'));
$r->connect('test1', new sfRoute('/users/:username/:sort/:start', array('module' => 'user', 'action' => 'show', 'sort' => 'all', 'start' => '0'), array('requirements' => array('username' => '\\w+', 'start' => '\\d+'))));
$t->is($r->generate('', array('username' => 'test1', 'module' => 'user', 'action' => 'show')), '/users/test1.html', '->generate() creates URL when using suffix and generate_shortest_url');
$t->is($r->generate('', array('username' => 'test1', 'module' => 'user', 'action' => 'show', 'sort' => 'all', 'start' => '0')), '/users/test1.html', '->generate() creates URL when using suffix and generate_shortest_url');
$t->is($r->generate('', array('username' => 'test1', 'module' => 'user', 'action' => 'show', 'sort' => 'all', 'start' => '1')), '/users/test1/all/1.html', '->generate() creates URL when using suffix and generate_shortest_url');
$t->is($r->parse('/users/test1.html'), array('module' => 'user', 'action' => 'show', 'sort' => 'all', 'start' => '0', 'username' => 'test1'), '->parse() returns all default parameters when provided suffix and generate_shortest_url enabled with .html suffix');
$r = new sfPatternRoutingTest(new sfEventDispatcher(), null, array('generate_shortest_url' => true, 'extra_parameters_as_query_string' => false, 'suffix' => '.html'));
$r->connect('posts', new sfRoute('/posts', array('module' => 'posts', 'action' => 'index', 'page' => '1')));
$r->connect('posts_pages', new sfRoute('/posts/:page', array('module' => 'posts', 'action' => 'index', 'page' => '1')));
$t->is($r->generate('', array('module' => 'posts', 'action' => 'index')), '/posts.html', '->generate() creates URL when using suffix and generate_shortest_url');
$t->is($r->generate('', array('module' => 'posts', 'action' => 'index', 'page' => '1')), '/posts.html', '->generate() creates URL when using suffix and generate_shortest_url');
$t->is($r->generate('', array('module' => 'posts', 'action' => 'index', 'page' => '2')), '/posts/2.html', '->generate() creates URL when using suffix and generate_shortest_url');
Ejemplo n.º 2
0
// Lazy routes config cache
$t->diag('Lazy Routes Config Cache');
$dispatcher = new sfEventDispatcher();
$dispatcher->connect('routing.load_configuration', 'configureRouting');
function configureRouting($event)
{
    $event->getSubject()->connect('first', new sfRoute('/first'));
    $event->getSubject()->connect('second', new sfRoute('/', array()));
}
// these tests are against r7363
$t->is($r->getCurrentInternalUri(false), 'foo/bar?id=2', '->getCurrentInternalUri() returns the internal URI for last parsed URL');
$t->is($r->getCurrentInternalUri(true), '@test2?id=2', '->getCurrentInternalUri() returns the internal URI for last parsed URL');
$t->is($r->getCurrentInternalUri(false), 'foo/bar?id=2', '->getCurrentInternalUri() returns the internal URI for last parsed URL');
// regression for ticket #3423  occuring when cache is used. (for the test its enough to have it non null)
$rCached = new sfPatternRoutingTest(new sfEventDispatcher(), new sfNoCache(), $options);
$rCached->connect('test', new sfRoute('/:module', array('action' => 'index')));
$rCached->connect('test2', new sfRoute('/', array()));
$rCached->parse('/');
$t->is($rCached->getCurrentInternalUri(), 'default/index', '->getCurrentInternalUri() returns the internal URI for last parsed URL using cache');
$rCached->parse('/test');
$t->is($rCached->getCurrentInternalUri(), 'test/index', '->getCurrentInternalUri() returns the internal URI for last parsed URL using cache');
$rCached->parse('/');
$t->is($rCached->getCurrentInternalUri(), 'default/index', '->getCurrentInternalUri() returns the internal URI for last parsed URL using cache');
// findRoute was added to be the side effectless version to check an uri
$parameters = $rCached->findRoute('/test');
$t->is($parameters, array('name' => 'test', 'pattern' => '/:module', 'parameters' => array('action' => 'index', 'module' => 'test')), '->findRoute() returns information about matching route');
$t->is($rCached->getCurrentInternalUri(), 'default/index', '->findRoute() does not change the internal URI of sfPatternRouting');
$t->is($rCached->findRoute('/no/match/found'), null, '->findRoute() returns null on non-matching route');
// current internal uri is reset after negative match
$r->clearRoutes();
$r->connect('test', new sfRoute('/test', array('bar' => 'foo')));