Exemplo n.º 1
0
 public function testLoad()
 {
     $container = $this->getMock('Symfony\\Component\\DependencyInjection\\ContainerInterface');
     $pool = $this->getMock('Sonata\\AdminBundle\\Admin\\Pool', array(), array($container, 'title', 'logoTitle'));
     $adminPoolLoader = new AdminPoolLoader($pool, array('foo_admin', 'bar_admin'), $container);
     $routeCollection1 = new RouteCollection('base.Code.Route.foo', 'baseRouteNameFoo', 'baseRoutePatternFoo', 'baseControllerNameFoo');
     $routeCollection2 = new RouteCollection('base.Code.Route.bar', 'baseRouteNameBar', 'baseRoutePatternBar', 'baseControllerNameBar');
     $routeCollection1->add('foo');
     $routeCollection2->add('bar');
     $routeCollection2->add('baz');
     $admin1 = $this->getMock('Sonata\\AdminBundle\\Admin\\AdminInterface');
     $admin1->expects($this->once())->method('getRoutes')->will($this->returnValue($routeCollection1));
     $admin2 = $this->getMock('Sonata\\AdminBundle\\Admin\\AdminInterface');
     $admin2->expects($this->once())->method('getRoutes')->will($this->returnValue($routeCollection2));
     $pool->expects($this->any())->method('getInstance')->will($this->returnCallback(function ($id) use($admin1, $admin2) {
         switch ($id) {
             case 'foo_admin':
                 return $admin1;
             case 'bar_admin':
                 return $admin2;
         }
         return;
     }));
     $collection = $adminPoolLoader->load('foo', 'sonata_admin');
     $this->assertInstanceOf('Symfony\\Component\\Routing\\RouteCollection', $collection);
     $this->assertInstanceOf('Symfony\\Component\\Routing\\Route', $collection->get('baseRouteNameFoo_foo'));
     $this->assertInstanceOf('Symfony\\Component\\Routing\\Route', $collection->get('baseRouteNameBar_bar'));
     $this->assertInstanceOf('Symfony\\Component\\Routing\\Route', $collection->get('baseRouteNameBar_bar'));
 }
Exemplo n.º 2
0
 /**
  * Добавляет новые маршруты в роутер.
  *
  * @param array $routes маршруты.
  * @return Router $this current object.
  * @throws \InvalidArgumentException
  */
 public function addRoutes(array $routes)
 {
     foreach ($routes as $name => $route) {
         if (array_diff(['pattern', 'controller'], array_keys($route))) {
             throw new \InvalidArgumentException('Invalid route');
         }
         $route['params'] = isset($route['params']) ? $route['params'] : [];
         $this->routes->add($name, new Route($route['pattern'], $route['controller'], $route['params']));
     }
     return $this;
 }
Exemplo n.º 3
0
 public function testAddAndCurrent()
 {
     $routes = [new Route('#/0#i', function () {
     }), new Route('#/1#i', function () {
     }), new Route('#/2#i', function () {
     })];
     $collection = new RouteCollection();
     foreach ($routes as $route) {
         $collection->add($route);
     }
     $this->assertSame($routes[0], $collection->current());
     $collection->next();
     $this->assertSame($routes[1], $collection->current());
     $collection->next();
     $this->assertSame($routes[2], $collection->current());
 }
Exemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function getRouteCollection()
 {
     if (!$this->routes) {
         $this->routes = $this->controllers->getRoutes();
         foreach ($this->aliases as $source => $alias) {
             $name = $source;
             $params = [];
             if ($query = substr(strstr($source, '?'), 1)) {
                 $name = strstr($source, '?', true);
                 parse_str($query, $params);
             }
             if ($route = $this->routes->get($name)) {
                 $this->routes->add($source, new Route($alias[0], array_merge($route->getDefaults(), $params, ['_variables' => $route->compile()->getPathVariables()])));
             }
         }
     }
     return $this->routes;
 }
Exemplo n.º 5
0
 public function testMethods()
 {
     $router = new RouteCollection();
     $this->assertFalse($router->has("home"));
     $this->assertNull($router->get("home"));
     $this->assertSame(0, $router->count());
     // add one
     $this->assertInstanceOf("SugiPHP\\Routing\\RouteCollection", $router->add("home", new Route("/")));
     $this->assertSame(1, $router->count());
     $this->assertTrue($router->has("home"));
     $this->assertInstanceOf("SugiPHP\\Routing\\Route", $router->get("home"));
     // change it
     $this->assertInstanceOf("SugiPHP\\Routing\\RouteCollection", $router->set("home", new Route("/foo")));
     $this->assertSame(1, $router->count());
     $this->assertTrue($router->has("home"));
     $this->assertInstanceOf("SugiPHP\\Routing\\Route", $router->get("home"));
     // remove it
     $this->assertInstanceOf("SugiPHP\\Routing\\RouteCollection", $router->delete("home"));
     $this->assertFalse($router->has("home"));
     $this->assertNull($router->get("home"));
     $this->assertSame(0, $router->count());
 }
 public function testNamedRoutesFillInParams()
 {
     $routes = new RouteCollection();
     $routes->add('path/(:any)/to/(:num)', 'myController::goto/$1/$2', ['as' => 'namedRoute']);
     $match = $routes->reverseRoute('namedRoute', 'string', 13);
     $this->assertEquals('path/string/to/13', $match);
 }
Exemplo n.º 7
0
 /**
  * @param Route $route
  * @return $this
  */
 public function addRoute(Route $route)
 {
     $this->routeCollection->add($route);
     return $this;
 }