| [ Index ] |
PHP Cross Reference of Limb3 |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * Limb PHP Framework 4 * 5 * @link http://limb-project.com 6 * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com) 7 * @license LGPL http://www.gnu.org/copyleft/lesser.html 8 */ 9 lmb_require('limb/web_app/src/request/lmbRoutes.class.php'); 10 lmb_require('limb/net/src/lmbUri.class.php'); 11 12 class lmbRoutesDispatchTest extends UnitTestCase 13 { 14 function setUp() 15 { 16 $toolkit = lmbToolkit :: save(); 17 } 18 19 function tearDown() 20 { 21 lmbToolkit :: restore(); 22 } 23 24 function testControllerAndDefaultAction() 25 { 26 $config = array(array('path' => '/blog', 27 'defaults' => array('controller' => 'Blog', 28 'action' => 'display')), 29 array('path' => '/news', 30 'defaults' => array('controller' => 'Newsline', 31 'action' => 'display'))); 32 33 $routes = new lmbRoutes($config); 34 $result = $routes->dispatch('/blog'); 35 36 $this->assertEqual($result['controller'], 'Blog'); 37 $this->assertEqual($result['action'], 'display'); 38 39 $result = $routes->dispatch('/news'); 40 41 $this->assertEqual($result['controller'], 'Newsline'); 42 $this->assertEqual($result['action'], 'display'); 43 44 $this->assertEqual($routes->dispatch('/no_such_url'), array()); 45 } 46 47 function testAnyController() 48 { 49 $config = array(array('path' => '/:controller', 50 'defaults' => array('action' => 'display'))); 51 52 $routes = new lmbRoutes($config); 53 $result = $routes->dispatch('/blog'); 54 55 $this->assertEqual($result['controller'], 'blog'); 56 $this->assertEqual($result['action'], 'display'); 57 58 $result = $routes->dispatch('/news'); 59 60 $this->assertEqual($result['controller'], 'news'); 61 $this->assertEqual($result['action'], 'display'); 62 } 63 64 function testAnyControllerAndAction() 65 { 66 $config = array(array('path' => '/:controller/:action', 67 'defaults' => array('action' => 'display'))); 68 69 $routes = new lmbRoutes($config); 70 $result = $routes->dispatch('/blog/index'); 71 72 $this->assertEqual($result['controller'], 'blog'); 73 $this->assertEqual($result['action'], 'index'); 74 75 $result = $routes->dispatch('/blog'); 76 77 $this->assertEqual($result['controller'], 'blog'); 78 $this->assertEqual($result['action'], 'display'); 79 80 $result = $routes->dispatch('/news/last_news'); 81 82 $this->assertEqual($result['controller'], 'news'); 83 $this->assertEqual($result['action'], 'last_news'); 84 } 85 86 function testConcreteControllerAndAnyAction() 87 { 88 $config = array(array('path' => '/blog/:action', 89 'defaults' => array('controller' => 'Blog', 90 'action' => 'display')), 91 array('path' => '/news/:action', 92 'defaults' => array('controller' => 'News', 93 'action' => 'display'))); 94 95 $routes = new lmbRoutes($config); 96 $result = $routes->dispatch('/blog/index'); 97 98 $this->assertEqual($result['controller'], 'Blog'); 99 $this->assertEqual($result['action'], 'index'); 100 101 $result = $routes->dispatch('/blog'); 102 103 $this->assertEqual($result['controller'], 'Blog'); 104 $this->assertEqual($result['action'], 'display'); 105 106 $result = $routes->dispatch('/news/last_news'); 107 108 $this->assertEqual($result['controller'], 'News'); 109 $this->assertEqual($result['action'], 'last_news'); 110 } 111 112 function testUrlToMatchAll() 113 { 114 $config = array(array('path' => '*', 115 'defaults' => array('controller' => '404', 116 'action' => 'display'))); 117 118 $routes = new lmbRoutes($config); 119 $result = $routes->dispatch('/blog/index'); 120 121 $this->assertEqual($result['controller'], '404'); 122 $this->assertEqual($result['action'], 'display'); 123 124 $result = $routes->dispatch('/path/to/heaven'); 125 126 $this->assertEqual($result['controller'], '404'); 127 $this->assertEqual($result['action'], 'display'); 128 } 129 130 function testExtraParamAfterOthers() 131 { 132 $config = array(array('path' => '/:controller/:action/*additional', 133 'defaults' => array('controller' => '404', 134 'action' => 'display'))); 135 136 $routes = new lmbRoutes($config); 137 $result = $routes->dispatch('/blog/index/and/many/params'); 138 139 $this->assertEqual($result['controller'], 'blog'); 140 $this->assertEqual($result['action'], 'index'); 141 $this->assertEqual($result['additional'], 'and/many/params'); 142 } 143 144 function testExtraParamDefaultName() 145 { 146 $config = array(array('path' => '/:controller/:action/*', 147 'defaults' => array('controller' => '404', 148 'action' => 'display'))); 149 150 $routes = new lmbRoutes($config); 151 $result = $routes->dispatch('/blog/index/and/many/params'); 152 153 $this->assertEqual($result['controller'], 'blog'); 154 $this->assertEqual($result['action'], 'index'); 155 $this->assertEqual($result['extra'], 'and/many/params'); 156 } 157 158 function testWithRequirements() 159 { 160 $config = array(array('path' => 'blog/', 161 'defaults' => array('controller' => 'Blog', 162 'action' => 'display')), 163 array('path' => 'blog/:year/:month/:day', 164 'defaults' => array('controller' => 'Blog', 165 'action' => 'archive', 166 'year' => date('Y'), 167 'month' => $default_month = date('m'), 168 'day' => $default_day = date('d')), 169 'requirements' => array('year' => '/(19|20)\d\d/', 170 'month' => '/[01]?\d/', 171 'day' => '/[0-3]?\d/')), 172 array('path' => 'blog/:action', 173 'defaults' => array('controller' => 'Blog', 174 'action' => 'display'))); 175 176 $routes = new lmbRoutes($config); 177 $result = $routes->dispatch('/blog/2004/12'); 178 179 $this->assertEqual($result['controller'], 'Blog'); 180 $this->assertEqual($result['action'], 'archive'); 181 $this->assertEqual($result['year'], '2004'); 182 $this->assertEqual($result['month'], '12'); 183 $this->assertEqual($result['day'], $default_day); 184 185 $result = $routes->dispatch('/blog/2004'); 186 187 $this->assertEqual($result['controller'], 'Blog'); 188 $this->assertEqual($result['action'], 'archive'); 189 $this->assertEqual($result['year'], '2004'); 190 $this->assertEqual($result['month'], $default_month); 191 $this->assertEqual($result['day'], $default_day); 192 193 $result = $routes->dispatch('/blog/1865'); 194 195 $this->assertEqual($result['controller'], 'Blog'); 196 $this->assertEqual($result['action'], '1865'); 197 $this->assertFalse(isset($result['year'])); 198 199 $result = $routes->dispatch('/blog/last_articles'); 200 201 $this->assertEqual($result['controller'], 'Blog'); 202 $this->assertEqual($result['action'], 'last_articles'); 203 $this->assertFalse(isset($result['year'])); 204 } 205 206 function testApplyDispatchFilter() 207 { 208 $config = array(array('path' => '/:controller/:action', 209 'defaults' => array('action' => 'display'), 210 'dispatch_filter' => array($this, '_processDispatchResult'))); 211 212 $routes = new lmbRoutes($config); 213 $result = $routes->dispatch('/blog/display'); 214 215 $this->assertEqual($result['controller'], 'Blog'); 216 $this->assertEqual($result['action'], 'display'); 217 } 218 219 function _processDispatchResult(&$dispatched) 220 { 221 if(isset($dispatched['controller'])) 222 $dispatched['controller'] = lmb_camel_case($dispatched['controller']); 223 } 224 } 225 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sat Aug 30 04:38:32 2008 | Cross-referenced by PHPXref 0.7 |