PageRenderTime 40ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Joomla/Router/Tests/RouterTest.php

https://github.com/dianaprajescu/joomla-framework
PHP | 349 lines | 265 code | 13 blank | 71 comment | 0 complexity | 3bbc617f71426926041bec2482e4bb93 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  4. * @license GNU General Public License version 2 or later; see LICENSE
  5. */
  6. namespace Joomla\Router\Tests;
  7. use Joomla\Router\Router;
  8. use Joomla\Test\TestHelper;
  9. require_once __DIR__ . '/Stubs/Bar.php';
  10. require_once __DIR__ . '/Stubs/Baz.php';
  11. require_once __DIR__ . '/Stubs/Foo.php';
  12. require_once __DIR__ . '/Stubs/GooGet.php';
  13. /**
  14. * Tests for the Joomla\Router\Router class.
  15. *
  16. * @since 1.0
  17. */
  18. class RouterTest extends \PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * An instance of the object to be tested.
  22. *
  23. * @var Router
  24. * @since 1.0
  25. */
  26. private $instance;
  27. /**
  28. * Provides test data for the testParseRoute method.
  29. *
  30. * @return array
  31. *
  32. * @since 1.0
  33. */
  34. public static function seedTestParseRoute()
  35. {
  36. // Route, Exception, ControllerName, InputData, MapSet
  37. return array(
  38. array('', false, 'home', array(), 1),
  39. array('articles/4', true, 'home', array(), 1),
  40. array('', false, 'index', array(), 2),
  41. array('login', false, 'login', array('_rawRoute' => 'login'), 2),
  42. array('articles', false, 'articles', array('_rawRoute' => 'articles'), 2),
  43. array('articles/4', false, 'article', array('article_id' => 4, '_rawRoute' => 'articles/4'), 2),
  44. array('articles/4/crap', true, '', array(), 2),
  45. array('test', true, '', array(), 2),
  46. array('test/foo', true, '', array(), 2),
  47. array('test/foo/path', true, '', array(), 2),
  48. array('test/foo/path/bar', false, 'test', array('seg1' => 'foo', 'seg2' => 'bar', '_rawRoute' => 'test/foo/path/bar'), 2),
  49. array('content/article-1/*', false, 'content', array('_rawRoute' => 'content/article-1/*'), 2),
  50. array('content/cat-1/article-1', false,
  51. 'article', array('category' => 'cat-1', 'article' => 'article-1', '_rawRoute' => 'content/cat-1/article-1'), 2),
  52. array('content/cat-1/cat-2/article-1', false,
  53. 'article', array('category' => 'cat-1/cat-2', 'article' => 'article-1', '_rawRoute' => 'content/cat-1/cat-2/article-1'), 2),
  54. array('content/cat-1/cat-2/cat-3/article-1', false,
  55. 'article', array('category' => 'cat-1/cat-2/cat-3', 'article' => 'article-1', '_rawRoute' => 'content/cat-1/cat-2/cat-3/article-1'), 2)
  56. );
  57. }
  58. /**
  59. * Setup the router maps to option 1.
  60. *
  61. * This has no routes but has a default controller for the home page.
  62. *
  63. * @return void
  64. *
  65. * @since 1.0
  66. */
  67. protected function setMaps1()
  68. {
  69. $this->instance->addMaps(array());
  70. $this->instance->setDefaultController('home');
  71. }
  72. /**
  73. * Setup the router maps to option 2.
  74. *
  75. * @return void
  76. *
  77. * @since 1.0
  78. */
  79. protected function setMaps2()
  80. {
  81. $this->instance->addMaps(
  82. array(
  83. 'login' => 'login',
  84. 'logout' => 'logout',
  85. 'articles' => 'articles',
  86. 'articles/:article_id' => 'article',
  87. 'test/:seg1/path/:seg2' => 'test',
  88. 'content/:/\*' => 'content',
  89. 'content/*category/:article' => 'article'
  90. )
  91. );
  92. $this->instance->setDefaultController('index');
  93. }
  94. /**
  95. * Tests the Joomla\Router\Router::__construct method.
  96. *
  97. * @return void
  98. *
  99. * @covers Joomla\Router\Router::__construct
  100. * @since 1.0
  101. */
  102. public function test__construct()
  103. {
  104. $this->assertAttributeInstanceOf('Joomla\\Input\\Input', 'input', $this->instance);
  105. }
  106. /**
  107. * Tests the Joomla\Router\Router::addMap method.
  108. *
  109. * @return void
  110. *
  111. * @covers Joomla\Router\Router::addMap
  112. * @since 1.0
  113. */
  114. public function testAddMap()
  115. {
  116. $this->assertAttributeEmpty('maps', $this->instance);
  117. $this->instance->addMap('foo', 'MyApplicationFoo');
  118. $this->assertAttributeEquals(
  119. array(
  120. array(
  121. 'regex' => chr(1) . '^foo$' . chr(1),
  122. 'vars' => array(),
  123. 'controller' => 'MyApplicationFoo'
  124. )
  125. ),
  126. 'maps',
  127. $this->instance
  128. );
  129. }
  130. /**
  131. * Tests the Joomla\Router\Router::addMaps method.
  132. *
  133. * @return void
  134. *
  135. * @covers Joomla\Router\Router::addMaps
  136. * @since 1.0
  137. */
  138. public function testAddMaps()
  139. {
  140. $maps = array(
  141. 'login' => 'login',
  142. 'logout' => 'logout',
  143. 'requests' => 'requests',
  144. 'requests/:request_id' => 'request'
  145. );
  146. $rules = array(
  147. array(
  148. 'regex' => chr(1) . '^login$' . chr(1),
  149. 'vars' => array(),
  150. 'controller' => 'login'
  151. ),
  152. array(
  153. 'regex' => chr(1) . '^logout$' . chr(1),
  154. 'vars' => array(),
  155. 'controller' => 'logout'
  156. ),
  157. array(
  158. 'regex' => chr(1) . '^requests$' . chr(1),
  159. 'vars' => array(),
  160. 'controller' => 'requests'
  161. ),
  162. array(
  163. 'regex' => chr(1) . '^requests/([^/]*)$' . chr(1),
  164. 'vars' => array('request_id'),
  165. 'controller' => 'request'
  166. )
  167. );
  168. $this->assertAttributeEmpty('maps', $this->instance);
  169. $this->instance->addMaps($maps);
  170. $this->assertAttributeEquals($rules, 'maps', $this->instance);
  171. }
  172. /**
  173. * Tests the Joomla\Router\Router::getController method.
  174. *
  175. * @return void
  176. *
  177. * @covers Joomla\Router\Router::getController
  178. * @since 1.0
  179. */
  180. public function testGetController()
  181. {
  182. $this->instance->setControllerPrefix('\Joomla\Router\Tests\Stubs\\')
  183. ->addMap('articles/:article_id', 'GooGet');
  184. $controller = $this->instance->getController('articles/3');
  185. $this->assertInstanceOf('\Joomla\Router\Tests\Stubs\GooGet', $controller);
  186. $input = $controller->getInput();
  187. $this->assertEquals('3', $input->get('article_id'));
  188. }
  189. /**
  190. * Tests the Joomla\Router\Router::parseRoute method.
  191. *
  192. * @param string $r The route to parse.
  193. * @param boolean $e True if an exception is expected.
  194. * @param string $c The expected controller name.
  195. * @param array $i The expected input object data.
  196. * @param integer $m The map set to use for setting up the router.
  197. *
  198. * @return void
  199. *
  200. * @covers Joomla\Router\Router::parseRoute
  201. * @dataProvider seedTestParseRoute
  202. * @since 1.0
  203. */
  204. public function testParseRoute($r, $e, $c, $i, $m)
  205. {
  206. // Setup the router maps.
  207. $mapSetup = 'setMaps' . $m;
  208. $this->$mapSetup();
  209. // If we should expect an exception set that up.
  210. if ($e)
  211. {
  212. $this->setExpectedException('InvalidArgumentException');
  213. }
  214. // Execute the route parsing.
  215. $actual = TestHelper::invoke($this->instance, 'parseRoute', $r);
  216. // Test the assertions.
  217. $this->assertEquals($c, $actual, 'Incorrect controller name found.');
  218. }
  219. /**
  220. * Tests the Joomla\Router\Router::setControllerPrefix method.
  221. *
  222. * @return void
  223. *
  224. * @covers Joomla\Router\Router::setControllerPrefix
  225. * @since 1.0
  226. */
  227. public function testSetControllerPrefix()
  228. {
  229. $this->instance->setControllerPrefix('MyApplication');
  230. $this->assertAttributeEquals('MyApplication', 'controllerPrefix', $this->instance);
  231. }
  232. /**
  233. * Tests the Joomla\Router\Router::setDefaultController method.
  234. *
  235. * @return void
  236. *
  237. * @covers Joomla\Router\Router::setDefaultController
  238. * @since 1.0
  239. */
  240. public function testSetDefaultController()
  241. {
  242. $this->instance->setDefaultController('foobar');
  243. $this->assertAttributeEquals('foobar', 'default', $this->instance);
  244. }
  245. /**
  246. * Tests the Joomla\Router\Router::fetchController method if the controller class is missing.
  247. *
  248. * @return void
  249. *
  250. * @covers Joomla\Router\Router::fetchController
  251. * @since 1.0
  252. */
  253. public function testFetchControllerWithMissingClass()
  254. {
  255. $this->setExpectedException('RuntimeException');
  256. $controller = TestHelper::invoke($this->instance, 'fetchController', 'goober');
  257. }
  258. /**
  259. * Tests the Joomla\Router\Router::fetchController method if the class not a controller.
  260. *
  261. * @return void
  262. *
  263. * @covers Joomla\Router\Router::fetchController
  264. * @since 1.0
  265. */
  266. public function testFetchControllerWithNonController()
  267. {
  268. $this->setExpectedException('RuntimeException');
  269. $controller = TestHelper::invoke($this->instance, 'fetchController', 'MyTestControllerBaz');
  270. }
  271. /**
  272. * Tests the Joomla\Router\Router::fetchController method with a prefix set.
  273. *
  274. * @return void
  275. *
  276. * @covers Joomla\Router\Router::fetchController
  277. * @since 1.0
  278. */
  279. public function testFetchControllerWithPrefixSet()
  280. {
  281. $this->instance->setControllerPrefix('MyTestController');
  282. $controller = TestHelper::invoke($this->instance, 'fetchController', 'Foo');
  283. }
  284. /**
  285. * Tests the Joomla\Router\Router::fetchController method without a prefix set even though it is necessary.
  286. *
  287. * @return void
  288. *
  289. * @covers Joomla\Router\Router::fetchController
  290. * @since 1.0
  291. */
  292. public function testFetchControllerWithoutPrefixSetThoughNecessary()
  293. {
  294. $this->setExpectedException('RuntimeException');
  295. $controller = TestHelper::invoke($this->instance, 'fetchController', 'foo');
  296. }
  297. /**
  298. * Tests the Joomla\Router\Router::fetchController method without a prefix set.
  299. *
  300. * @return void
  301. *
  302. * @covers Joomla\Router\Router::fetchController
  303. * @since 1.0
  304. */
  305. public function testFetchControllerWithoutPrefixSet()
  306. {
  307. $controller = TestHelper::invoke($this->instance, 'fetchController', 'TControllerBar');
  308. }
  309. /**
  310. * Prepares the environment before running a test.
  311. *
  312. * @return void
  313. *
  314. * @since 1.0
  315. */
  316. protected function setUp()
  317. {
  318. parent::setUp();
  319. $this->instance = new Router;
  320. }
  321. }