public function testAddPrefix()
 {
     $collection = new RouteCollection();
     $collection->addRoute('foo', $foo = new Route('/foo'));
     $collection->addRoute('bar', $bar = new Route('/bar'));
     $collection->addPrefix('/admin');
     $this->assertEquals('/admin/foo', $collection->getRoute('foo')->getPattern(), '->addPrefix() adds a prefix to all routes');
     $this->assertEquals('/admin/bar', $collection->getRoute('bar')->getPattern(), '->addPrefix() adds a prefix to all routes');
 }
Exemple #2
0
 protected function parseRoute(RouteCollection $collection, $definition, $file)
 {
     $defaults = array();
     $requirements = array();
     $options = array();
     foreach ($definition->childNodes as $node) {
         if (!$node instanceof \DOMElement) {
             continue;
         }
         switch ($node->tagName) {
             case 'default':
                 $defaults[(string) $node->getAttribute('key')] = trim((string) $node->nodeValue);
                 break;
             case 'option':
                 $options[(string) $node->getAttribute('key')] = trim((string) $node->nodeValue);
                 break;
             case 'requirement':
                 $requirements[(string) $node->getAttribute('key')] = trim((string) $node->nodeValue);
                 break;
             default:
                 throw new \InvalidArgumentException(sprintf('Unable to parse tag "%s"', $node->tagName));
         }
     }
     $route = new Route((string) $definition->getAttribute('pattern'), $defaults, $requirements, $options);
     $collection->addRoute((string) $definition->getAttribute('id'), $route);
 }
Exemple #3
0
 public function testNormalizeUrl()
 {
     $collection = new RouteCollection();
     $collection->addRoute('foo', new Route('/:foo'));
     $matcher = new UrlMatcherForTests($collection, array(), array());
     $this->assertEquals('/', $matcher->normalizeUrl(''), '->normalizeUrl() adds a / at the beginning of the URL if needed');
     $this->assertEquals('/foo', $matcher->normalizeUrl('foo'), '->normalizeUrl() adds a / at the beginning of the URL if needed');
     $this->assertEquals('/foo', $matcher->normalizeUrl('/foo?foo=bar'), '->normalizeUrl() removes the query string');
     $this->assertEquals('/foo/bar', $matcher->normalizeUrl('/foo//bar'), '->normalizeUrl() removes duplicated /');
 }
 /**
  * @throws \InvalidArgumentException When config pattern is not defined for the given route
  */
 protected function parseRoute(RouteCollection $collection, $name, $config, $file)
 {
     $defaults = isset($config['defaults']) ? $config['defaults'] : array();
     $requirements = isset($config['requirements']) ? $config['requirements'] : array();
     $options = isset($config['options']) ? $config['options'] : array();
     if (!isset($config['pattern'])) {
         throw new \InvalidArgumentException(sprintf('You must define a "pattern" for the "%s" route.', $name));
     }
     $route = new Route($config['pattern'], $defaults, $requirements, $options);
     $collection->addRoute($name, $route);
 }
 /**
  * @covers Symfony\Components\Routing\Loader\ClosureLoader::load
  */
 public function testLoad()
 {
     $loader = new ClosureLoader();
     $route = new Route('/');
     $routes = $loader->load(function () use($route) {
         $routes = new RouteCollection();
         $routes->addRoute('foo', $route);
         return $routes;
     });
     $this->assertEquals($route, $routes->getRoute('foo'), '->load() loads a \\Closure resource');
 }
 /**
  * @covers Symfony\Components\Routing\Loader\DelegatingLoader::load
  */
 public function testLoad()
 {
     $resolver = new LoaderResolver(array(new ClosureLoader()));
     $loader = new DelegatingLoader($resolver);
     $route = new Route('/');
     $routes = $loader->load(function () use($route) {
         $routes = new RouteCollection();
         $routes->addRoute('foo', $route);
         return $routes;
     });
     $this->assertSame($route, $routes->getRoute('foo'), '->load() loads a resource using the loaders from the resolver');
     try {
         $loader->load('foo.foo');
         $this->fail('->load() throws an \\InvalidArgumentException if the resource cannot be loaded');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\InvalidArgumentException', $e, '->load() throws an \\InvalidArgumentException if the resource cannot be loaded');
     }
 }
<?php

/*
 * This file is part of the symfony package.
 * (c) Fabien Potencier <*****@*****.**>
 * 
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
require_once __DIR__ . '/../../../../bootstrap.php';
use Symfony\Components\Routing\Matcher\UrlMatcher;
use Symfony\Components\Routing\RouteCollection;
use Symfony\Components\Routing\Route;
$t = new LimeTest(1);
$collection = new RouteCollection();
$collection->addRoute('foo', new Route('/:foo'));
class UrlMatcherTest extends UrlMatcher
{
    public function normalizeUrl($url)
    {
        return parent::normalizeUrl($url);
    }
}
$matcher = new UrlMatcherTest($collection, array(), array());
// ->normalizeUrl()
$t->diag('->normalizeUrl()');
$t->is($matcher->normalizeUrl(''), '/', '->normalizeUrl() adds a / at the beginning of the URL if needed');
$t->is($matcher->normalizeUrl('foo'), '/foo', '->normalizeUrl() adds a / at the beginning of the URL if needed');
$t->is($matcher->normalizeUrl('/foo?foo=bar'), '/foo', '->normalizeUrl() removes the query string');
$t->is($matcher->normalizeUrl('/foo//bar'), '/foo/bar', '->normalizeUrl() removes duplicated /');
// ->match()
Exemple #8
0
<?php

use Symfony\Components\Routing\RouteCollection;
use Symfony\Components\Routing\Route;
$collection = new RouteCollection();
$collection->addRoute('homepage', new Route('/', array('_controller' => 'FrameworkBundle:Default:index')));
return $collection;
$collection->addRoute('foo', $foo = new Route('/foo'));
$collection1 = new RouteCollection();
$collection1->addRoute('foo', $foo1 = new Route('/foo1'));
$collection1->addRoute('bar', $bar1 = new Route('/bar1'));
$collection->addCollection($collection1);
$t->is($collection->getRoutes(), array('foo' => $foo1, 'bar' => $bar1), '->addCollection() adds routes from another collection');
$collection = new RouteCollection();
$collection->addRoute('foo', $foo = new Route('/foo'));
$collection1 = new RouteCollection();
$collection1->addRoute('foo', $foo1 = new Route('/foo1'));
$collection->addCollection($collection1, '/foo');
$t->is($collection->getRoute('foo')->getPattern(), '/foo/foo1', '->addCollection() can add a prefix to all merged routes');
$collection = new RouteCollection();
$collection->addResource($foo = new FileResource('foo'));
$collection1 = new RouteCollection();
$collection1->addResource($foo1 = new FileResource('foo1'));
$collection->addCollection($collection1);
$t->is($collection->getResources(), array($foo, $foo1), '->addCollection() merges resources');
// ->addPrefix()
$t->diag('->addPrefix()');
$collection = new RouteCollection();
$collection->addRoute('foo', $foo = new Route('/foo'));
$collection->addRoute('bar', $bar = new Route('/bar'));
$collection->addPrefix('/admin');
$t->is($collection->getRoute('foo')->getPattern(), '/admin/foo', '->addPrefix() adds a prefix to all routes');
$t->is($collection->getRoute('bar')->getPattern(), '/admin/bar', '->addPrefix() adds a prefix to all routes');
// ->getResources() ->addResource()
$t->diag('->getResources() ->addResource()');
$collection = new RouteCollection();
$collection->addResource($foo = new FileResource('foo'));
$t->is($collection->getResources(), array($foo), '->addResources() adds a resource');