PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/standard/tags/release-0.6.0/tests/Zend/Controller/FrontTest.php

https://github.com/bhaumik25/zend-framework
PHP | 354 lines | 248 code | 56 blank | 50 comment | 2 complexity | 1127a1e5252d0f0af9c6afb009a59e12 MD5 | raw file
  1. <?php
  2. require_once 'Zend/Controller/Front.php';
  3. require_once 'PHPUnit/Framework/TestCase.php';
  4. require_once 'Zend/Controller/Request/Http.php';
  5. require_once 'Zend/Controller/Response/Cli.php';
  6. require_once 'Zend/Controller/Dispatcher.php';
  7. require_once 'Zend/Controller/Router.php';
  8. class Zend_Controller_FrontTest extends PHPUnit_Framework_TestCase
  9. {
  10. protected $_controller = null;
  11. public function setUp()
  12. {
  13. $this->_controller = Zend_Controller_Front::getInstance();
  14. $this->_controller->resetInstance();
  15. $this->_controller->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  16. $this->_controller->returnResponse(true);
  17. $this->_controller->throwExceptions(false);
  18. }
  19. public function tearDown()
  20. {
  21. unset($this->_controller);
  22. }
  23. public function testResetInstance()
  24. {
  25. $this->_controller->setParam('foo', 'bar');
  26. $this->assertEquals('bar', $this->_controller->getParam('foo'));
  27. $this->_controller->resetInstance();
  28. $this->assertNull($this->_controller->getParam('bar'));
  29. $this->assertSame(array(), $this->_controller->getParams());
  30. $this->assertSame(array(), $this->_controller->getDispatcher()->getControllerDirectory());
  31. }
  32. public function testSetGetRequest()
  33. {
  34. $request = new Zend_Controller_Request_Http();
  35. $this->_controller->setRequest($request);
  36. $this->assertTrue($request === $this->_controller->getRequest());
  37. }
  38. public function testSetGetResponse()
  39. {
  40. $response = new Zend_Controller_Response_Cli();
  41. $this->_controller->setResponse($response);
  42. $this->assertTrue($response === $this->_controller->getResponse());
  43. }
  44. public function testSetGetRouter()
  45. {
  46. $router = new Zend_Controller_Router();
  47. $this->_controller->setRouter($router);
  48. $this->assertTrue($router === $this->_controller->getRouter());
  49. }
  50. public function testSetGetDispatcher()
  51. {
  52. $dispatcher = new Zend_Controller_Dispatcher();
  53. $this->_controller->setDispatcher($dispatcher);
  54. $this->assertTrue($dispatcher === $this->_controller->getDispatcher());
  55. }
  56. public function testSetGetControllerDirectory()
  57. {
  58. $test = $this->_controller->getControllerDirectory();
  59. $expected = array(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  60. $this->assertSame($expected, $test);
  61. }
  62. public function testGetSetParam()
  63. {
  64. $this->_controller->setParam('foo', 'bar');
  65. $this->assertEquals('bar', $this->_controller->getParam('foo'));
  66. $this->_controller->setParam('bar', 'baz');
  67. $this->assertEquals('baz', $this->_controller->getParam('bar'));
  68. }
  69. public function testGetSetParams()
  70. {
  71. $this->_controller->setParams(array('foo' => 'bar'));
  72. $this->assertSame(array('foo' => 'bar'), $this->_controller->getParams());
  73. $this->_controller->setParam('baz', 'bat');
  74. $this->assertSame(array('foo' => 'bar', 'baz' => 'bat'), $this->_controller->getParams());
  75. $this->_controller->setParams(array('foo' => 'bug'));
  76. $this->assertSame(array('foo' => 'bug', 'baz' => 'bat'), $this->_controller->getParams());
  77. }
  78. public function testClearParams()
  79. {
  80. $this->_controller->setParams(array('foo' => 'bar', 'baz' => 'bat'));
  81. $this->assertSame(array('foo' => 'bar', 'baz' => 'bat'), $this->_controller->getParams());
  82. $this->_controller->clearParams('foo');
  83. $this->assertSame(array('baz' => 'bat'), $this->_controller->getParams());
  84. $this->_controller->clearParams();
  85. $this->assertSame(array(), $this->_controller->getParams());
  86. $this->_controller->setParams(array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'bat'));
  87. $this->assertSame(array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'bat'), $this->_controller->getParams());
  88. $this->_controller->clearParams(array('foo', 'baz'));
  89. $this->assertSame(array('bar' => 'baz'), $this->_controller->getParams());
  90. }
  91. public function testSetGetDefaultController()
  92. {
  93. $this->assertEquals('index', $this->_controller->getDefaultController());
  94. $this->_controller->setDefaultController('foo');
  95. $this->assertEquals('foo', $this->_controller->getDefaultController());
  96. }
  97. public function testSetGetDefaultAction()
  98. {
  99. $this->assertEquals('index', $this->_controller->getDefaultAction());
  100. $this->_controller->setDefaultAction('bar');
  101. $this->assertEquals('bar', $this->_controller->getDefaultAction());
  102. }
  103. /**
  104. * Test default action on valid controller
  105. */
  106. public function testDispatch()
  107. {
  108. $request = new Zend_Controller_Request_Http();
  109. $request->setControllerName('index');
  110. $this->_controller->setResponse(new Zend_Controller_Response_Cli());
  111. $response = $this->_controller->dispatch($request);
  112. $this->assertContains('Index action called', $response->getBody());
  113. }
  114. /**
  115. * Test valid action on valid controller
  116. */
  117. public function testDispatch1()
  118. {
  119. $request = new Zend_Controller_Request_Http();
  120. $request->setControllerName('index');
  121. $request->setActionName('index');
  122. $this->_controller->setResponse(new Zend_Controller_Response_Cli());
  123. $response = $this->_controller->dispatch($request);
  124. $this->assertContains('Index action called', $response->getBody());
  125. }
  126. /**
  127. * Test invalid action on valid controller
  128. */
  129. public function testDispatch2()
  130. {
  131. $request = new Zend_Controller_Request_Http();
  132. $request->setControllerName('index');
  133. $request->setActionName('foo');
  134. try {
  135. $this->_controller->dispatch($request);
  136. $this->fail('Exception should be raised by __call');
  137. } catch (Exception $e) {
  138. // success
  139. }
  140. }
  141. /**
  142. * Test invalid controller
  143. */
  144. public function testDispatch3()
  145. {
  146. $request = new Zend_Controller_Request_Http();
  147. $request->setControllerName('baz');
  148. try {
  149. $this->_controller->dispatch($request);
  150. $this->fail('Exception should be raised; no such controller');
  151. } catch (Exception $e) {
  152. // success
  153. }
  154. }
  155. /**
  156. * Test valid action on valid controller; test pre/postDispatch
  157. */
  158. public function testDispatch4()
  159. {
  160. $request = new Zend_Controller_Request_Http('http://example.com/foo/bar');
  161. $this->_controller->setResponse(new Zend_Controller_Response_Cli());
  162. $response = $this->_controller->dispatch($request);
  163. $body = $response->getBody();
  164. $this->assertContains('Bar action called', $body, $body);
  165. $this->assertContains('preDispatch called', $body, $body);
  166. $this->assertContains('postDispatch called', $body, $body);
  167. }
  168. /**
  169. * Test that extra arguments get passed
  170. */
  171. public function testDispatch5()
  172. {
  173. $request = new Zend_Controller_Request_Http();
  174. $request->setControllerName('index');
  175. $request->setActionName('args');
  176. $this->_controller->setResponse(new Zend_Controller_Response_Cli());
  177. $this->_controller->setParam('foo', 'bar');
  178. $this->_controller->setParam('baz', 'bat');
  179. $response = $this->_controller->dispatch($request);
  180. $body = $response->getBody();
  181. $this->assertContains('foo: bar', $body);
  182. $this->assertContains('baz: bat', $body);
  183. }
  184. /**
  185. * Test using router
  186. */
  187. public function testDispatch6()
  188. {
  189. $request = new Zend_Controller_Request_Http('http://framework.zend.com/foo/bar/var1/baz');
  190. $this->_controller->setResponse(new Zend_Controller_Response_Cli());
  191. $this->_controller->setRouter(new Zend_Controller_Router());
  192. $response = $this->_controller->dispatch($request);
  193. $body = $response->getBody();
  194. $this->assertContains('Bar action called', $body);
  195. $params = $request->getParams();
  196. $this->assertTrue(isset($params['var1']));
  197. $this->assertEquals('baz', $params['var1']);
  198. }
  199. /**
  200. * Test without router, using GET params
  201. */
  202. public function testDispatch7()
  203. {
  204. if ('cli' == strtolower(php_sapi_name())) {
  205. $this->markTestSkipped('Issues with $_GET in CLI interface prevents test from passing');
  206. }
  207. $request = new Zend_Controller_Request_Http('http://framework.zend.com/index.php?controller=foo&action=bar');
  208. $response = new Zend_Controller_Response_Cli();
  209. $response = $this->_controller->dispatch($request, $response);
  210. $body = $response->getBody();
  211. $this->assertContains('Bar action called', $body);
  212. }
  213. /**
  214. * Test that run() throws exception when called from object instance
  215. */
  216. public function _testRunThrowsException()
  217. {
  218. try {
  219. $this->_controller->run(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
  220. $this->fail('Should not be able to call run() from object instance');
  221. } catch (Exception $e) {
  222. // success
  223. }
  224. }
  225. /**
  226. * Test that set/getBaseUrl() functionality works
  227. */
  228. public function testSetGetBaseUrl()
  229. {
  230. $this->assertNull($this->_controller->getBaseUrl());
  231. $this->_controller->setBaseUrl('/index.php');
  232. $this->assertEquals('/index.php', $this->_controller->getBaseUrl());
  233. }
  234. /**
  235. * Test that a set base URL is pushed to the request during the dispatch
  236. * process
  237. */
  238. public function testBaseUrlPushedToRequest()
  239. {
  240. $this->_controller->setBaseUrl('/index.php');
  241. $request = new Zend_Controller_Request_Http('http://example.com/index');
  242. $response = new Zend_Controller_Response_Cli();
  243. $response = $this->_controller->dispatch($request, $response);
  244. $this->assertContains('index.php', $request->getBaseUrl());
  245. }
  246. /**
  247. * Test that throwExceptions() sets and returns value properly
  248. */
  249. public function testThrowExceptions()
  250. {
  251. $this->_controller->throwExceptions(true);
  252. $this->assertTrue($this->_controller->throwExceptions());
  253. $this->_controller->throwExceptions(false);
  254. $this->assertFalse($this->_controller->throwExceptions());
  255. }
  256. /**
  257. * Test that with throwExceptions() set, an exception is thrown
  258. */
  259. public function testThrowExceptionsThrows()
  260. {
  261. $this->_controller->throwExceptions(true);
  262. $this->_controller->setControllerDirectory(dirname(__FILE__));
  263. $request = new Zend_Controller_Request_Http('http://framework.zend.com/bogus/baz');
  264. $this->_controller->setResponse(new Zend_Controller_Response_Cli());
  265. $this->_controller->setRouter(new Zend_Controller_Router());
  266. try {
  267. $response = $this->_controller->dispatch($request);
  268. $this->fail('Invalid controller should throw exception');
  269. } catch (Exception $e) {
  270. // success
  271. }
  272. }
  273. /**
  274. * Test that returnResponse() sets and returns value properly
  275. */
  276. public function testReturnResponse()
  277. {
  278. $this->_controller->returnResponse(true);
  279. $this->assertTrue($this->_controller->returnResponse());
  280. $this->_controller->returnResponse(false);
  281. $this->assertFalse($this->_controller->returnResponse());
  282. }
  283. /**
  284. * Test that with returnResponse set to false, output is echoed and equals that in the response
  285. */
  286. public function testReturnResponseReturnsResponse()
  287. {
  288. $request = new Zend_Controller_Request_Http('http://framework.zend.com/foo/bar/var1/baz');
  289. $this->_controller->setResponse(new Zend_Controller_Response_Cli());
  290. $this->_controller->setRouter(new Zend_Controller_Router());
  291. $this->_controller->returnResponse(false);
  292. ob_start();
  293. $this->_controller->dispatch($request);
  294. $body = ob_get_clean();
  295. $actual = $this->_controller->getResponse()->getBody();
  296. $this->assertEquals($actual, $body);
  297. }
  298. }