Example #1
0
 public function testIterator()
 {
     $identifier = 'myRoute';
     $collection = new RouteCollection();
     $collection->add(new Route('/user/{id:\\d}/form/valid', array($this, 'routeCallback'), $identifier));
     $this->assertEquals($identifier, $collection->key());
     // Test route collection looping
     foreach ($collection as $route) {
         $this->assertEquals($identifier, $route->identifier);
     }
     // Testing working with route collection as array
     $route = new Route('/user/{id:\\d}/form/valid', array($this, 'routeCallback'));
     $collection['TestRoute'] = $route;
     $this->assertArrayHasKey('TestRoute', $collection);
     // Testing overloading route identifier
     $routeWithoutId = new Route('/{id:\\d}/form/valid', array($this, 'routeCallback'), 'OriginalID');
     $collection['ChangedOriginalID'] = $routeWithoutId;
     $this->assertArrayHasKey('ChangedOriginalID', $collection);
     $this->assertArrayNotHasKey('OriginalID', $collection);
     // Test passed route object
     $this->assertEquals($route, $collection['TestRoute']);
     // Test removing routes
     unset($collection[$identifier]);
     $this->assertEquals(false, isset($collection[$identifier]));
 }
Example #2
0
 public function testGeneration()
 {
     // Create routes descriptions with identifiers
     $routeArray = array('main-page' => array('GET', '/', '/'), 'inner-page' => array('GET', '/{page}', '/text-page', array('page' => 'text-page')), 'test-two-similar-fixed' => array('GET', '/userlist', '/userlist'), 'test-two-similar-fixed2' => array('GET', '/userlist/friends', '/userlist/friends'), 'test-two-params-at-end' => array('GET', '/userlist/{group}/{action}', '/userlist/123/kill', array('group' => '123', 'action' => 'kill')), 'user-winners-slash' => array('GET', '/user/winners/', '/user/winners/'), 'user-by-id' => array('GET', '/user/{id}', '/user/123'), 'user-home-without-slash' => array('GET', '/user', '/user'), 'user-by-gender-age' => array('GET', '/user/{gender:male|female}/{age}', '/user/male/19d', array('gender' => 'male', 'age' => '19d')), 'user-by-gender-age-filtered' => array('GET', '/user/{gender:male|female}/{age:[0-9]+}', '/user/female/8', array('gender' => 'female', 'age' => '8')), 'user-by-id-form' => array('GET', '/user/{id}/form', '/user/123/form', array('id' => '123')), 'user-by-id-friends' => array('GET', '/user/{id}/friends', '/user/123/friends', array('id' => '123')), 'user-by-id-friends-with-id' => array('GET', '/user/{id}/friends/{groupid}', '/user/123/friends/1', array('id' => '123', 'groupid' => 1)), 'entity-by-id-form' => array('GET', '/{entity}/{id}/form', '/friend/123/form', array('entity' => 'friend', 'id' => '123')), 'entity-by-id-form-test' => array('GET', '/{id}/test/{page:\\d+}', '/123/test/1', array('id' => '123', 'page' => '1')), 'two-params' => array('GET', '/{num}/{page:\\d+}', '/123/23123', array('num' => '123', 'page' => '23123')), 'user-by-id-node' => array('GET', '/user/{id}/n"$ode', '/user/123/n"$ode', array('id' => '123')), 'user-by-id-node-with-id' => array('GET', '/user/{id}/n"$ode/{param}', '/user/123/n"$ode/321', array('id' => '123', 'param' => '321')), 'user-post-by-id' => array('POST', '/user/{id}/save', '/user/123/save', array('id' => '123')), 'user-post-by-id-param' => array('POST', '/user/{id}/save/{name}', '/user/123/save/vitaly', array('id' => '123', 'name' => 'vitaly')), 'user-post-by-id-param2' => array('POST', '/user/{id}/save/{name}/{group}', '/user/123/save/vitaly/students', array('id' => '123', 'name' => 'vitaly', 'group' => 'students')), 'user-post-by-id-param3' => array('POST', '/cms/gift/form/{id}', '/cms/gift/form/123', array('id' => '123')), 'user-post-by-id-param4' => array('POST', '/cms/gift/{id}/{search}', '/cms/gift/123/321', array('id' => '123', 'search' => '321')));
     // Create routes collection
     $routes = new RouteCollection();
     foreach ($routeArray as $identifier => $routeData) {
         $routes->add(new Route($routeData[1], array($this, 'baseCallback'), $identifier, $routeData[0]));
     }
     $generator = new Structure($routes, new \samsonphp\generator\Generator());
     $this->routerLogicFunction = '__router' . rand(0, 9999);
     $routerLogic = $generator->generate($this->routerLogicFunction);
     // Create real file for debugging
     file_put_contents(__DIR__ . '/testLogic.php', '<?php ' . "\n" . $routerLogic);
     require __DIR__ . '/testLogic.php';
     foreach ($routeArray as $identifier => $routeData) {
         if ($identifier === 'test-two-params-at-end') {
             $a = 1;
         }
         $result = $this->routerLogic($routeData[2], $routeData[0]);
         $this->assertEquals($identifier, $result[0]);
         if (isset($routeData[3])) {
             foreach ($routeData[3] as $key => $value) {
                 $this->assertArrayHasKey($key, $result[1]);
                 $this->assertEquals($value, $result[1][$key]);
             }
         }
     }
 }
Example #3
0
 public function testCreation()
 {
     require 'DummyRouterLogic.php';
     $identifier = 'MyRoute';
     $collection = new RouteCollection();
     $collection->add(new Route('/user/{id:\\d}/form/valid', array($this, 'routeCallback'), $identifier));
     $core = new Core($collection);
     /** @var Route $route */
     $route = null;
     $dispatchingResult = $core->dispatch('/user/123/form/valid', Route::METHOD_GET, $route);
     $this->assertEquals($identifier, $dispatchingResult[0]);
     $this->assertArrayHasKey('id', $dispatchingResult[1]);
 }
 /**
  * Generate old-fashioned routes collection.
  *
  * @param Object $module
  * @return array
  */
 protected function createGenericRoutes(&$module)
 {
     /** @var RouteCollection $routes */
     $routes = new RouteCollection();
     /** @var callable $universalCallback */
     $universalCallback = null;
     $universalRoutes = new RouteCollection();
     /** @var Route $baseRoute */
     $baseRoute = null;
     $prefix = '/' . $module->id;
     Event::fire('samsonphp.router.create.module.routes', array($module, &$prefix));
     //trace('!!!!!!!!!!!!!!!! - '.$prefix);
     // Iterate class methods
     foreach (get_class_methods($module) as $method) {
         // Try to find standard controllers
         switch (strtolower($method)) {
             case self::CTR_UNI:
                 // Add generic controller action
                 $universalCallback = $module->id . '#' . $method;
                 $universalRoutes->merge($this->getParametrizedRoutes($module, $prefix, $method));
                 break;
             case self::CTR_BASE:
                 // Add base controller action
                 $baseRoute = new Route($prefix . '/', $module->id . '#' . $method, $module->id . self::CTR_BASE);
                 break;
             case self::CTR_POST:
                 // not implemented
             // not implemented
             case self::CTR_PUT:
                 // not implemented
             // not implemented
             case self::CTR_DELETE:
                 // not implemented
                 break;
                 // Ignore magic methods
             // Ignore magic methods
             case '__call':
             case '__wakeup':
             case '__sleep':
             case '__construct':
             case '__destruct':
             case '__set':
             case '__get':
                 break;
                 // This is not special controller action
             // This is not special controller action
             default:
                 // Match controller action OOP pattern
                 if (preg_match('/^' . self::OBJ_PREFIX . '(?<async_>async_)?(?<cache_>cache_)?(?<action>.+)/i', $method, $matches)) {
                     $routes->merge($this->getParametrizedRoutes($module, $prefix, $method, $matches['action'], $matches[self::ASYNC_PREFIX]));
                 }
         }
     }
     // Add universal route
     $routes->merge($universalRoutes);
     // Add base controller action
     if (isset($baseRoute)) {
         $routes->add($baseRoute);
         // If we have not found base controller action but we have universal action
     } elseif (isset($universalCallback)) {
         // Bind its pattern to universal controller callback
         $routes->add(new Route($prefix . '/', $universalCallback, $module->id . self::CTR_BASE));
     }
     return $routes;
 }