RouteTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\console\command;
  9. use lithium\console\command\Route;
  10. use lithium\console\Request;
  11. use lithium\net\http\Router;
  12. use lithium\core\Libraries;
  13. /**
  14. * The RouteTest class tests the "li3 route" command.
  15. */
  16. class RouteTest extends \lithium\test\Unit {
  17. /**
  18. * Holds config params.
  19. *
  20. * @var array
  21. */
  22. protected $_config = array('routes' => null);
  23. /**
  24. * Holds the temporary test path.
  25. *
  26. * @var string
  27. */
  28. protected $_testPath = null;
  29. /**
  30. * Set the testPath and check if it is writable (skip if not).
  31. */
  32. public function skip() {
  33. $path = Libraries::get(true, 'resources');
  34. if (is_writable($path) && !is_dir("{$path}/tmp/tests")) {
  35. mkdir("{$path}/tmp/tests", 0777, true);
  36. }
  37. $this->_testPath = "{$path}/tmp/tests";
  38. $this->skipIf(!is_writable($this->_testPath), "Path `{$this->_testPath}` is not writable.");
  39. }
  40. /**
  41. * Create a temporary routes.php file for testing and reset the router.
  42. */
  43. public function setUp() {
  44. $this->_config['routes'] = "{$this->_testPath}/routes.php";
  45. $testParams = 'array("controller" => "lithium\test\Controller")';
  46. $content = array(
  47. '<?php',
  48. 'use lithium\net\http\Router;',
  49. 'use lithium\core\Environment;',
  50. 'Router::connect("/", "Pages::view");',
  51. 'Router::connect("/pages/{:args}", "Pages::view");',
  52. 'if (!Environment::is("production")) {',
  53. 'Router::connect("/test/{:args}", ' . $testParams . ');',
  54. 'Router::connect("/test", ' . $testParams . ');',
  55. '}',
  56. '?>'
  57. );
  58. file_put_contents($this->_config['routes'], join("\n", $content));
  59. Router::reset();
  60. }
  61. /**
  62. * Delete the temporary routes.php file.
  63. */
  64. public function tearDown() {
  65. if (file_exists($this->_config['routes'])) {
  66. unlink($this->_config['routes']);
  67. }
  68. }
  69. /**
  70. * Tests if the default environment is loaded correctly
  71. * and if overriding works as expected.
  72. */
  73. public function testEnvironment() {
  74. $command = new Route(array('routes' => $this->_config['routes']));
  75. $expected = 'development';
  76. $this->assertEqual($expected, $command->env);
  77. $request = new Request();
  78. $request->params['env'] = 'production';
  79. $command = new Route(compact('request') + array('routes' => $this->_config['routes']));
  80. $this->assertEqual('production', $command->env);
  81. }
  82. /**
  83. * Test if the routes.php file is loaded correctly and the
  84. * routes are connected to the router.
  85. */
  86. public function testRouteLoading() {
  87. $this->assertFalse(Router::get());
  88. $command = new Route(array('routes' => $this->_config['routes']));
  89. $this->assertEqual(4, count(Router::get()));
  90. Router::reset();
  91. $request = new Request();
  92. $request->params['env'] = 'production';
  93. $command = new Route(compact('request') + array('routes' => $this->_config['routes']));
  94. $this->assertEqual(2, count(Router::get()));
  95. }
  96. /**
  97. * Tests the "all" command without an env param.
  98. *
  99. * Don't be confused if the expected output doesn't make sense here. We are
  100. * stripping the whitespace away so that this source code is easier to read.
  101. * Built-In methods are used for output formatting and are tested elsewhere.
  102. */
  103. public function testAllWithoutEnvironment() {
  104. $command = new Route(array(
  105. 'routes' => $this->_config['routes'],
  106. 'classes' => array('response' => 'lithium\tests\mocks\console\MockResponse'),
  107. 'request' => new Request()
  108. ));
  109. $command->all();
  110. $expected = 'TemplateParams--------------
  111. /{"controller":"Pages","action":"view"}
  112. /pages/{:args}{"controller":"Pages","action":"view"}
  113. /test/{:args}{"controller":"lithium\\test\\\\Controller","action":"index"}
  114. /test{"controller":"lithium\\test\\\\Controller","action":"index"}';
  115. $this->assertEqual($this->_strip($expected),$this->_strip($command->response->output));
  116. }
  117. /**
  118. * Tests the "all" command with an env (production) param.
  119. *
  120. * Don't be confused if the expected output doesn't make sense here. We are
  121. * stripping the whitespace away so that this source code is easier to read.
  122. * Built-In methods are used for output formatting and are tested elsewhere.
  123. */
  124. public function testAllWithEnvironment() {
  125. $request = new Request();
  126. $request->params = array(
  127. 'env' => 'production'
  128. );
  129. $command = new Route(compact('request') + array(
  130. 'routes' => $this->_config['routes'],
  131. 'classes' => array('response' => 'lithium\tests\mocks\console\MockResponse')
  132. ));
  133. $command->all();
  134. $expected = 'TemplateParams--------------
  135. /{"controller":"Pages","action":"view"}
  136. /pages/{:args}{"controller":"Pages","action":"view"}';
  137. $this->assertEqual($this->_strip($expected),$this->_strip($command->response->output));
  138. }
  139. /**
  140. * Test the alias method for "all".
  141. */
  142. public function testRun() {
  143. $command = new Route(array(
  144. 'routes' => $this->_config['routes'],
  145. 'classes' => array('response' => 'lithium\tests\mocks\console\MockResponse'),
  146. 'request' => new Request()
  147. ));
  148. $command->run();
  149. $expected = 'TemplateParams--------------
  150. /{"controller":"Pages","action":"view"}
  151. /pages/{:args}{"controller":"Pages","action":"view"}
  152. /test/{:args}{"controller":"lithium\\test\\\\Controller","action":"index"}
  153. /test{"controller":"lithium\\test\\\\Controller","action":"index"}';
  154. $this->assertEqual($this->_strip($expected),$this->_strip($command->response->output));
  155. }
  156. /**
  157. * Test the show command with no route.
  158. */
  159. public function testShowWithNoRoute() {
  160. $command = new Route(array(
  161. 'routes' => $this->_config['routes'],
  162. 'classes' => array('response' => 'lithium\tests\mocks\console\MockResponse'),
  163. 'request' => new Request()
  164. ));
  165. $command->show();
  166. $expected = "Please provide a valid URL\n";
  167. $this->assertEqual($expected, $command->response->error);
  168. }
  169. /**
  170. * Test the show command with an invalid route.
  171. */
  172. public function testShowWithInvalidRoute() {
  173. $request = new Request();
  174. $request->params = array(
  175. 'args' => array('/foobar')
  176. );
  177. $command = new Route(compact('request') + array(
  178. 'routes' => $this->_config['routes'],
  179. 'classes' => array('response' => 'lithium\tests\mocks\console\MockResponse')
  180. ));
  181. $command->show();
  182. $expected = "No route found.\n";
  183. $this->assertEqual($expected, $command->response->output);
  184. }
  185. /**
  186. * Test the show command with a valid route.
  187. */
  188. public function testShowWithValidRoute() {
  189. $request = new Request();
  190. $request->params = array('args' => array('/'));
  191. $command = new Route(compact('request') + array(
  192. 'routes' => $this->_config['routes'],
  193. 'classes' => array('response' => 'lithium\tests\mocks\console\MockResponse')
  194. ));
  195. $command->show();
  196. $expected = "{\"controller\":\"Pages\",\"action\":\"view\"}\n";
  197. $this->assertEqual($expected, $command->response->output);
  198. }
  199. /**
  200. * Test the show command with a env param.
  201. */
  202. public function testShowWithEnvironment() {
  203. $request = new Request();
  204. $request->params = array(
  205. 'env' => 'production',
  206. 'args' => array('/test')
  207. );
  208. $command = new Route(compact('request') + array(
  209. 'routes' => $this->_config['routes'],
  210. 'classes' => array('response' => 'lithium\tests\mocks\console\MockResponse')
  211. ));
  212. $command->show();
  213. $expected = "No route found.\n";
  214. $this->assertEqual($expected, $command->response->output);
  215. }
  216. /**
  217. * Test the show command with http method.
  218. *
  219. * This tests a call similar to "li3 route GET /".
  220. */
  221. public function testShowWithHttpMethod() {
  222. $request = new Request();
  223. $request->params = array(
  224. 'args' => array('post', '/')
  225. );
  226. $command = new Route(compact('request') + array(
  227. 'routes' => $this->_config['routes'],
  228. 'classes' => array('response' => 'lithium\tests\mocks\console\MockResponse')
  229. ));
  230. $command->show();
  231. $expected = "{\"controller\":\"Pages\",\"action\":\"view\"}\n";
  232. $this->assertEqual($expected, $command->response->output);
  233. }
  234. /**
  235. * Remove formatting whitespace, tabs and newlines for better sourcecode
  236. * readability.
  237. *
  238. * @param string $str A string from which to strip spaces
  239. * @return string Returns the value of `$str` with all whitespace removed.
  240. */
  241. protected function _strip($str) {
  242. return preg_replace('/\s/', '', $str);
  243. }
  244. }
  245. ?>