Example #1
0
 public function testCollection()
 {
     $routes = [['product.details', ['POST' => 'handler0', 'GET' => 'handler1']], ['product.category', ['POST' => 'handler0', 'GET' => 'handler1', 'PUT' => 'handler2']]];
     $collection = new Collection();
     foreach ($routes as $key => $data) {
         $route = new Route($data[0]);
         foreach ($data[1] as $method => $handler) {
             $route->setHandlers($method, $handler, ['*/*']);
         }
         $collection->addRoute($route);
     }
     $route0 = $collection->getRouteByName('product.details');
     $this->assertNotNull($route0);
     $route1 = $collection->getRouteByName('product.category');
     $this->assertNotNull($route1);
     $route2 = $collection->getRouteByName('product.variant');
     $this->assertNull($route2);
     // Testing serilization
     $data = serialize($collection);
     $newCollection = unserialize($data);
     $route0 = $newCollection->getRouteByName('product.details');
     $this->assertNotNull($route0);
     $route1 = $newCollection->getRouteByName('product.category');
     $this->assertNotNull($route1);
     $route2 = $newCollection->getRouteByName('product.variant');
     $this->assertNull($route2);
 }
Example #2
0
 /**
  * @dataProvider routeProvider3
  */
 public function testHeadMethodMatching($pattern, $handlers)
 {
     $collection = new Collection();
     $route = new Route($pattern);
     foreach ($handlers as $handler) {
         list($method, $function, $contentTypes) = $handler;
         $route->setHandlers($method, $function, $contentTypes);
     }
     $collection->addRoute($route);
     $dispatcher = new Dispatcher();
     $dispatcher->useCollection($collection);
     // Route found
     $result = $dispatcher->dispatch('GET', 'archive', ['application/json' => 0.9]);
     $this->assertEquals(DispatchingResult::ROUTE_FOUND, $result->getCode());
     $result = $dispatcher->dispatch('HEAD', 'archive', ['application/json' => 0.9]);
     $this->assertEquals(DispatchingResult::ROUTE_FOUND, $result->getCode());
     // Now disable matching
     $dispatcher->matchMissingHeadHandlerWithGetHandler(false);
     $result = $dispatcher->dispatch('HEAD', 'archive', ['application/json' => 0.9]);
     $this->assertEquals(DispatchingResult::HTTP_METHOD_NOT_SUPPORTED, $result->getCode());
 }