public function testAddingRouteToRouter() { $router = new \Drest\Router(); $routeMetaData = new \Drest\Mapping\RouteMetaData(); $routeName = 'MyRoute'; $routeMetaData->setName($routeName); $router->registerRoute($routeMetaData); $this->assertTrue($router->hasRoute($routeName)); }
public function testRemovingFromRegistryByRouteAndHavingActionUsedForOtherRoute() { $registry = new Registry(); $className = 'DrestTests\\Entities\\Typical\\User'; $className2 = 'DrestTests\\Entities\\Typical\\User'; $routeName = 'route_name'; $routeName2 = 'route_name2'; $serviceAction = new \DrestTests\Action\Custom(); $registry->register($serviceAction, [$className . '::' . $routeName, $className2 . '::' . $routeName2]); $routeMetaData = new \Drest\Mapping\RouteMetaData(); $routeMetaData->setClassMetaData(new \Drest\Mapping\ClassMetaData(new \ReflectionClass($className))); $routeMetaData->setName($routeName); $routeMetaData2 = new \Drest\Mapping\RouteMetaData(); $routeMetaData2->setClassMetaData(new \Drest\Mapping\ClassMetaData(new \ReflectionClass($className2))); $routeMetaData2->setName($routeName2); $registry->unregisterByRoute($routeMetaData); $this->assertFalse($registry->hasServiceAction($routeMetaData)); $this->assertTrue($registry->hasServiceAction($routeMetaData2)); $this->assertSame($registry->getServiceAction($routeMetaData2), $serviceAction); $registry->unregisterByRoute($routeMetaData2); $this->assertFalse($registry->hasServiceAction($routeMetaData)); $this->assertFalse($registry->hasServiceAction($routeMetaData2)); }
/** * Process all routes defined * @param array $routes * @param Mapping\ClassMetaData $metadata * @throws DrestException */ protected function processRoutes(array $routes, \Drest\Mapping\ClassMetaData $metadata) { $originFound = false; foreach ($routes as $route) { $routeMetaData = new \Drest\Mapping\RouteMetaData(); // Set name $route['name'] = preg_replace("/[^a-zA-Z0-9_\\s]/", "", $route['name']); if ($route['name'] == '') { throw DrestException::routeNameIsEmpty(); } if ($metadata->getRouteMetaData($route['name']) !== false) { throw DrestException::routeAlreadyDefinedWithName($metadata->getClassName(), $route['name']); } $routeMetaData->setName($route['name']); // Set verbs (will throw if invalid) if (isset($route['verbs'])) { $routeMetaData->setVerbs($route['verbs']); } if (isset($route['collection'])) { $routeMetaData->setCollection($route['collection']); } // Add the route pattern $routeMetaData->setRoutePattern($route['routePattern']); if (isset($route['routeConditions']) && is_array($route['routeConditions'])) { $routeMetaData->setRouteConditions($route['routeConditions']); } // Set the exposure array if (isset($route['expose']) && is_array($route['expose'])) { $routeMetaData->setExpose($route['expose']); } // Set the allow options value if (isset($route['allowOptions'])) { $routeMetaData->setAllowedOptionRequest($route['allowOptions']); } // Add action class if (isset($route['action'])) { $routeMetaData->setActionClass($route['action']); } // If the origin flag is set, set the name on the class meta data if (isset($route['origin']) && !is_null($route['origin'])) { if ($originFound) { throw DrestException::resourceCanOnlyHaveOneRouteSetAsOrigin(); } $metadata->originRouteName = $route['name']; $originFound = true; } $metadata->addRouteMetaData($routeMetaData); } }