PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/DevApp/library/ServerLibraries/ZendFramework/1.7/tests/Zend/Controller/Router/Route/ModuleTest.php

http://firephp.googlecode.com/
PHP | 447 lines | 340 code | 86 blank | 21 comment | 3 complexity | 41851a9dae8b227cd545ad90d47f726d MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT, Apache-2.0
  1. <?php
  2. /** Test helper */
  3. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  4. require_once "PHPUnit/Framework/TestCase.php";
  5. require_once "PHPUnit/Framework/TestSuite.php";
  6. /**
  7. * @category Zend
  8. * @package Zend_Controller
  9. * @subpackage UnitTests
  10. */
  11. /** Zend_Controller_Router_Route_Module */
  12. require_once 'Zend/Controller/Router/Route/Module.php';
  13. /** Zend_Controller_Front */
  14. require_once 'Zend/Controller/Front.php';
  15. // Call Zend_Controller_Router_Route_ModuleTest::main() if this source file is executed directly.
  16. if (!defined("PHPUnit_MAIN_METHOD")) {
  17. define("PHPUnit_MAIN_METHOD", "Zend_Controller_Router_Route_ModuleTest::main");
  18. }
  19. /**
  20. * @category Zend
  21. * @package Zend_Controller
  22. * @subpackage UnitTests
  23. */
  24. class Zend_Controller_Router_Route_ModuleTest extends PHPUnit_Framework_TestCase
  25. {
  26. protected $_request;
  27. protected $_dispatcher;
  28. protected $route;
  29. /**
  30. * Runs the test methods of this class.
  31. *
  32. * @access public
  33. * @static
  34. */
  35. public static function main()
  36. {
  37. require_once "PHPUnit/TextUI/TestRunner.php";
  38. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Router_Route_ModuleTest");
  39. $result = PHPUnit_TextUI_TestRunner::run($suite);
  40. }
  41. public function setUp()
  42. {
  43. $front = Zend_Controller_Front::getInstance();
  44. $front->resetInstance();
  45. $front->setParam('noErrorHandler', true)
  46. ->setParam('noViewRenderer', true);
  47. $this->_dispatcher = $front->getDispatcher();
  48. $this->_dispatcher->setControllerDirectory(array(
  49. 'default' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files',
  50. 'mod' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'Admin',
  51. ));
  52. $defaults = array(
  53. 'controller' => 'defctrl',
  54. 'action' => 'defact',
  55. 'module' => 'default'
  56. );
  57. require_once 'Zend/Controller/Request/Http.php';
  58. $this->_request = new Zend_Controller_Request_Http();
  59. $front->setRequest($this->_request);
  60. $this->route = new Zend_Controller_Router_Route_Module($defaults, $this->_dispatcher, $this->_request);
  61. }
  62. public function testModuleMatch()
  63. {
  64. $values = $this->route->match('mod');
  65. $this->assertType('array', $values);
  66. $this->assertTrue(isset($values['module']));
  67. $this->assertEquals('mod', $values['module']);
  68. }
  69. public function testModuleAndControllerMatch()
  70. {
  71. $values = $this->route->match('mod/con');
  72. $this->assertType('array', $values);
  73. $this->assertTrue(isset($values['module']));
  74. $this->assertEquals('mod', $values['module']);
  75. $this->assertTrue(isset($values['controller']));
  76. $this->assertEquals('con', $values['controller']);
  77. }
  78. public function testModuleControllerAndActionMatch()
  79. {
  80. $values = $this->route->match('mod/con/act');
  81. $this->assertType('array', $values);
  82. $this->assertTrue(isset($values['module']));
  83. $this->assertEquals('mod', $values['module']);
  84. $this->assertTrue(isset($values['controller']));
  85. $this->assertEquals('con', $values['controller']);
  86. $this->assertTrue(isset($values['action']));
  87. $this->assertEquals('act', $values['action']);
  88. }
  89. public function testModuleControllerActionAndParamsMatch()
  90. {
  91. $values = $this->route->match('mod/con/act/var/val/foo');
  92. $this->assertType('array', $values);
  93. $this->assertTrue(isset($values['module']));
  94. $this->assertEquals('mod', $values['module']);
  95. $this->assertTrue(isset($values['controller']));
  96. $this->assertEquals('con', $values['controller']);
  97. $this->assertTrue(isset($values['action']));
  98. $this->assertEquals('act', $values['action']);
  99. $this->assertTrue(isset($values['var']));
  100. $this->assertEquals('val', $values['var']);
  101. $this->assertTrue(array_key_exists('foo', $values), var_export($values, 1));
  102. $this->assertTrue(empty($values['foo']));
  103. }
  104. public function testControllerOnlyMatch()
  105. {
  106. $values = $this->route->match('con');
  107. $this->assertType('array', $values);
  108. $this->assertTrue(isset($values['controller']));
  109. $this->assertEquals('con', $values['controller']);
  110. }
  111. public function testControllerOnlyAndActionMatch()
  112. {
  113. $values = $this->route->match('con/act');
  114. $this->assertType('array', $values);
  115. $this->assertTrue(isset($values['controller']));
  116. $this->assertEquals('con', $values['controller']);
  117. $this->assertTrue(isset($values['action']));
  118. $this->assertEquals('act', $values['action']);
  119. }
  120. public function testControllerOnlyActionAndParamsMatch()
  121. {
  122. $values = $this->route->match('con/act/var/val/foo');
  123. $this->assertType('array', $values);
  124. $this->assertTrue(isset($values['controller']));
  125. $this->assertEquals('con', $values['controller']);
  126. $this->assertTrue(isset($values['action']));
  127. $this->assertEquals('act', $values['action']);
  128. $this->assertTrue(isset($values['var']));
  129. $this->assertEquals('val', $values['var']);
  130. $this->assertTrue(array_key_exists('foo', $values), var_export($values, 1));
  131. $this->assertTrue(empty($values['foo']));
  132. }
  133. public function testModuleMatchWithControlKeysChange()
  134. {
  135. $this->_request->setModuleKey('m');
  136. $this->_request->setControllerKey('c');
  137. $this->_request->setActionKey('a');
  138. $this->route = new Zend_Controller_Router_Route_Module(array(), $this->_dispatcher, $this->_request);
  139. $values = $this->route->match('mod/ctrl');
  140. $this->assertType('array', $values);
  141. $this->assertSame('mod', $values['m']);
  142. $this->assertSame('ctrl', $values['c']);
  143. $this->assertSame('index', $values['a']);
  144. }
  145. public function testModuleMatchWithLateControlKeysChange()
  146. {
  147. $this->_request->setModuleKey('m');
  148. $this->_request->setControllerKey('c');
  149. $this->_request->setActionKey('a');
  150. $values = $this->route->match('mod/ctrl');
  151. $this->assertType('array', $values);
  152. $this->assertSame('mod', $values['m'], var_export(array_keys($values), 1));
  153. $this->assertSame('ctrl', $values['c'], var_export(array_keys($values), 1));
  154. $this->assertSame('index', $values['a'], var_export(array_keys($values), 1));
  155. }
  156. public function testAssembleNoModuleOrController()
  157. {
  158. $params = array(
  159. 'action' => 'act',
  160. 'foo' => 'bar'
  161. );
  162. $url = $this->route->assemble($params);
  163. $this->assertEquals('defctrl/act/foo/bar', $url);
  164. }
  165. public function testAssembleControllerOnly()
  166. {
  167. $params = array(
  168. 'foo' => 'bar',
  169. 'action' => 'act',
  170. 'controller' => 'con'
  171. );
  172. $url = $this->route->assemble($params);
  173. $this->assertEquals('con/act/foo/bar', $url);
  174. }
  175. public function testAssembleModuleAndController()
  176. {
  177. $params = array(
  178. 'foo' => 'bar',
  179. 'action' => 'act',
  180. 'controller' => 'con',
  181. 'module' => 'mod'
  182. );
  183. $url = $this->route->assemble($params);
  184. $this->assertEquals('mod/con/act/foo/bar', $url);
  185. }
  186. public function testAssembleNoController()
  187. {
  188. $params = array(
  189. 'foo' => 'bar',
  190. 'action' => 'act',
  191. 'module' => 'mod'
  192. );
  193. $url = $this->route->assemble($params);
  194. $this->assertEquals('mod/defctrl/act/foo/bar', $url);
  195. }
  196. public function testAssembleNoAction()
  197. {
  198. $params = array(
  199. 'module' => 'mod',
  200. 'controller' => 'ctrl'
  201. );
  202. $url = $this->route->assemble($params);
  203. $this->assertEquals('mod/ctrl', $url);
  204. }
  205. public function testAssembleNoActionWithParams()
  206. {
  207. $params = array(
  208. 'foo' => 'bar',
  209. 'module' => 'mod',
  210. 'controller' => 'ctrl'
  211. );
  212. $url = $this->route->assemble($params);
  213. $this->assertEquals('mod/ctrl/defact/foo/bar', $url);
  214. }
  215. public function testAssembleNoModuleOrControllerMatched()
  216. {
  217. $this->route->match('');
  218. $params = array(
  219. 'action' => 'act',
  220. 'foo' => 'bar'
  221. );
  222. $url = $this->route->assemble($params);
  223. $this->assertEquals('defctrl/act/foo/bar', $url);
  224. }
  225. public function testAssembleControllerOnlyMatched()
  226. {
  227. $this->route->match('ctrl');
  228. $params = array(
  229. 'foo' => 'bar',
  230. 'action' => 'act',
  231. 'controller' => 'con'
  232. );
  233. $url = $this->route->assemble($params);
  234. $this->assertEquals('con/act/foo/bar', $url);
  235. }
  236. public function testAssembleModuleAndControllerMatched()
  237. {
  238. $this->route->match('mod/ctrl');
  239. $params = array(
  240. 'foo' => 'bar',
  241. 'action' => 'act',
  242. 'module' => 'm'
  243. );
  244. $url = $this->route->assemble($params);
  245. $this->assertEquals('m/ctrl/act/foo/bar', $url);
  246. }
  247. public function testAssembleNoControllerMatched()
  248. {
  249. $this->route->match('mod');
  250. $params = array(
  251. 'foo' => 'bar',
  252. 'action' => 'act',
  253. 'module' => 'mod'
  254. );
  255. $url = $this->route->assemble($params);
  256. $this->assertEquals('mod/defctrl/act/foo/bar', $url);
  257. }
  258. public function testAssembleNoActionMatched()
  259. {
  260. $this->route->match('mod/ctrl');
  261. $params = array(
  262. 'module' => 'def',
  263. 'controller' => 'con'
  264. );
  265. $url = $this->route->assemble($params);
  266. $this->assertEquals('def/con', $url);
  267. }
  268. public function testAssembleWithReset()
  269. {
  270. $values = $this->route->match('mod/con/act/sort/name');
  271. $url = $this->route->assemble(array('action' => 'new'), true);
  272. $this->assertSame('defctrl/new', $url);
  273. }
  274. public function testAssembleWithReset2()
  275. {
  276. $values = $this->route->match('mod/con/act/sort/name');
  277. $url = $this->route->assemble(array('controller' => 'new'), true);
  278. $this->assertSame('new', $url);
  279. }
  280. public function testAssembleWithReset3()
  281. {
  282. $values = $this->route->match('mod/con/act/sort/name');
  283. $url = $this->route->assemble(array('controller' => 'new', 'action' => 'test'), true);
  284. $this->assertSame('new/test', $url);
  285. }
  286. public function testAssembleResetOneVariable()
  287. {
  288. $values = $this->route->match('mod/con/act');
  289. $url = $this->route->assemble(array('action' => null), false);
  290. $this->assertSame('mod/con', $url);
  291. }
  292. public function testAssembleResetOneVariable2()
  293. {
  294. $values = $this->route->match('mod/con/act');
  295. $url = $this->route->assemble(array('controller' => null), false);
  296. $this->assertSame('mod/defctrl/act', $url);
  297. }
  298. public function testAssembleResetOneVariable3()
  299. {
  300. $values = $this->route->match('mod/con/act');
  301. $url = $this->route->assemble(array('module' => null), false);
  302. $this->assertSame('con/act', $url);
  303. }
  304. public function testAssembleDefaultModuleResetZF1415()
  305. {
  306. $values = $this->route->match('con/act');
  307. $url = $this->route->assemble(array('controller' => 'foo', 'action' => 'bar'), true);
  308. $this->assertSame('foo/bar', $url);
  309. }
  310. public function testAssembleDefaultModuleZF1415()
  311. {
  312. $values = $this->route->match('con/act');
  313. $url = $this->route->assemble(array('controller' => 'foo', 'action' => 'bar'), false);
  314. $this->assertSame('foo/bar', $url);
  315. }
  316. public function testAssembleDefaultModuleZF1415_2()
  317. {
  318. $values = $this->route->match('default/defctrl/defact');
  319. $url = $this->route->assemble();
  320. $this->assertSame('', $url);
  321. $values = $this->route->match('mod/defctrl/defact');
  322. $url = $this->route->assemble();
  323. $this->assertSame('mod', $url);
  324. }
  325. public function testGetInstance()
  326. {
  327. require_once 'Zend/Config.php';
  328. $routeConf = array(
  329. 'defaults' => array(
  330. 'controller' => 'ctrl'
  331. )
  332. );
  333. $config = new Zend_Config($routeConf);
  334. $route = Zend_Controller_Router_Route_Module::getInstance($config);
  335. $this->assertType('Zend_Controller_Router_Route_Module', $route);
  336. }
  337. public function testEncode()
  338. {
  339. $url = $this->route->assemble(array('controller' => 'My Controller'), false, true);
  340. $this->assertEquals('My+Controller', $url);
  341. $url = $this->route->assemble(array('controller' => 'My Controller'), false, false);
  342. $this->assertEquals('My Controller', $url);
  343. $token = $this->route->match('en/foo/id/My Value');
  344. $url = $this->route->assemble(array(), false, true);
  345. $this->assertEquals('en/foo/id/My+Value', $url);
  346. $url = $this->route->assemble(array('id' => 'My Other Value'), false, true);
  347. $this->assertEquals('en/foo/id/My+Other+Value', $url);
  348. }
  349. public function testArrayValues()
  350. {
  351. $url = $this->route->assemble(array('foo' => array('bar', 'baz')));
  352. $this->assertEquals('defctrl/defact/foo/bar/foo/baz', $url);
  353. $token = $this->route->match('defctrl/defact/foo/bar/foo/baz');
  354. $this->assertEquals('bar', $token['foo'][0]);
  355. $this->assertEquals('baz', $token['foo'][1]);
  356. }
  357. }
  358. // Call Zend_Controller_Router_Route_ModuleTest::main() if this source file is executed directly.
  359. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Router_Route_ModuleTest::main") {
  360. Zend_Controller_Router_Route_ModuleTest::main();
  361. }