ErrorHandlerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Controller
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_Controller_Plugin_ErrorHandlerTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD"))
  24. {
  25. define("PHPUnit_MAIN_METHOD", "Zend_Controller_Plugin_ErrorHandlerTest::main");
  26. $basePath = realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..');
  27. set_include_path(
  28. $basePath . DIRECTORY_SEPARATOR . 'tests'
  29. . PATH_SEPARATOR . $basePath . DIRECTORY_SEPARATOR . 'library'
  30. . PATH_SEPARATOR . get_include_path()
  31. );
  32. }
  33. require_once 'Zend/Controller/Plugin/ErrorHandler.php';
  34. require_once 'Zend/Controller/Request/Http.php';
  35. require_once 'Zend/Controller/Response/Http.php';
  36. require_once 'Zend/Controller/Dispatcher/Exception.php';
  37. require_once 'Zend/Controller/Action/Exception.php';
  38. require_once 'Zend/Controller/Front.php';
  39. /**
  40. * Test class for Zend_Controller_Plugin_ErrorHandler.
  41. * Generated by PHPUnit_Util_Skeleton on 2007-05-15 at 09:50:21.
  42. *
  43. * @category Zend
  44. * @package Zend_Controller
  45. * @subpackage UnitTests
  46. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  47. * @license http://framework.zend.com/license/new-bsd New BSD License
  48. * @group Zend_Controller
  49. * @group Zend_Controller_Plugin
  50. */
  51. class Zend_Controller_Plugin_ErrorHandlerTest extends PHPUnit_Framework_TestCase
  52. {
  53. /**
  54. * Request object
  55. * @var Zend_Controller_Request_Http
  56. */
  57. public $request;
  58. /**
  59. * Response object
  60. * @var Zend_Controller_Response_Http
  61. */
  62. public $response;
  63. /**
  64. * Error handler plugin
  65. * @var Zend_Controller_Plugin_ErrorHandler
  66. */
  67. public $plugin;
  68. /**
  69. * Runs the test methods of this class.
  70. *
  71. * @access public
  72. * @static
  73. */
  74. public static function main()
  75. {
  76. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Plugin_ErrorHandlerTest");
  77. $result = PHPUnit_TextUI_TestRunner::run($suite);
  78. }
  79. /**
  80. * Sets up the fixture, for example, open a network connection.
  81. * This method is called before a test is executed.
  82. *
  83. * @access protected
  84. */
  85. protected function setUp()
  86. {
  87. Zend_Controller_Front::getInstance()->resetInstance();
  88. $this->request = new Zend_Controller_Request_Http();
  89. $this->response = new Zend_Controller_Response_Http();
  90. $this->plugin = new Zend_Controller_Plugin_ErrorHandler();
  91. $this->plugin->setRequest($this->request);
  92. $this->plugin->setResponse($this->response);
  93. }
  94. /**
  95. * Tears down the fixture, for example, close a network connection.
  96. * This method is called after a test is executed.
  97. *
  98. * @access protected
  99. */
  100. protected function tearDown()
  101. {
  102. }
  103. public function testSetErrorHandler()
  104. {
  105. $this->plugin->setErrorHandler(array(
  106. 'module' => 'myfoo',
  107. 'controller' => 'bar',
  108. 'action' => 'boobaz',
  109. ));
  110. $this->assertEquals('myfoo', $this->plugin->getErrorHandlerModule());
  111. $this->assertEquals('bar', $this->plugin->getErrorHandlerController());
  112. $this->assertEquals('boobaz', $this->plugin->getErrorHandlerAction());
  113. }
  114. public function testSetErrorHandlerModule()
  115. {
  116. $this->plugin->setErrorHandlerModule('boobah');
  117. $this->assertEquals('boobah', $this->plugin->getErrorHandlerModule());
  118. }
  119. public function testSetErrorHandlerController()
  120. {
  121. $this->plugin->setErrorHandlerController('boobah');
  122. $this->assertEquals('boobah', $this->plugin->getErrorHandlerController());
  123. }
  124. public function testSetErrorHandlerAction()
  125. {
  126. $this->plugin->setErrorHandlerAction('boobah');
  127. $this->assertEquals('boobah', $this->plugin->getErrorHandlerAction());
  128. }
  129. public function testPostDispatchNoControllerException()
  130. {
  131. $this->response->setException(new Zend_Controller_Dispatcher_Exception('Testing controller exception'));
  132. $this->request->setModuleName('foo')
  133. ->setControllerName('bar')
  134. ->setActionName('baz');
  135. $this->plugin->postDispatch($this->request);
  136. $this->assertNotNull($this->request->getParam('error_handler'));
  137. $errorHandler = $this->request->getParam('error_handler');
  138. $this->assertTrue($errorHandler instanceof ArrayObject);
  139. $this->assertEquals(Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER, $errorHandler->type);
  140. $this->assertEquals('error', $this->request->getActionName());
  141. $this->assertEquals('error', $this->request->getControllerName());
  142. $this->assertEquals('default', $this->request->getModuleName());
  143. }
  144. public function testPostDispatchNoActionException()
  145. {
  146. $this->response->setException(new Zend_Controller_Action_Exception('Testing action exception', 404));
  147. $this->request->setModuleName('foo')
  148. ->setControllerName('bar')
  149. ->setActionName('baz');
  150. $this->plugin->postDispatch($this->request);
  151. $this->assertNotNull($this->request->getParam('error_handler'));
  152. $errorHandler = $this->request->getParam('error_handler');
  153. $this->assertTrue($errorHandler instanceof ArrayObject);
  154. $this->assertEquals(Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION, $errorHandler->type);
  155. $this->assertEquals('error', $this->request->getActionName());
  156. $this->assertEquals('error', $this->request->getControllerName());
  157. $this->assertEquals('default', $this->request->getModuleName());
  158. }
  159. public function testPostDispatchOtherException()
  160. {
  161. $this->response->setException(new Exception('Testing other exception'));
  162. $this->request->setModuleName('foo')
  163. ->setControllerName('bar')
  164. ->setActionName('baz');
  165. $this->plugin->postDispatch($this->request);
  166. $this->assertNotNull($this->request->getParam('error_handler'));
  167. $errorHandler = $this->request->getParam('error_handler');
  168. $this->assertTrue($errorHandler instanceof ArrayObject);
  169. $this->assertEquals(Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER, $errorHandler->type);
  170. $this->assertEquals('error', $this->request->getActionName());
  171. $this->assertEquals('error', $this->request->getControllerName());
  172. $this->assertEquals('default', $this->request->getModuleName());
  173. }
  174. public function testPostDispatchThrowsWhenCalledRepeatedly()
  175. {
  176. $this->response->setException(new Exception('Testing other exception'));
  177. $this->request->setModuleName('foo')
  178. ->setControllerName('bar')
  179. ->setActionName('baz');
  180. $this->plugin->postDispatch($this->request);
  181. $this->response->setException(new Zend_Controller_Dispatcher_Exception('Another exception'));
  182. try {
  183. $this->plugin->postDispatch($this->request);
  184. $this->fail('Repeated calls with new exceptions should throw exceptions');
  185. } catch (Exception $e) {
  186. $type = get_class($e);
  187. $this->assertEquals('Zend_Controller_Dispatcher_Exception', $type);
  188. $this->assertEquals('Another exception', $e->getMessage());
  189. }
  190. }
  191. public function testPostDispatchDoesNothingWhenCalledRepeatedlyWithoutNewExceptions()
  192. {
  193. $this->response->setException(new Exception('Testing other exception'));
  194. $this->request->setModuleName('foo')
  195. ->setControllerName('bar')
  196. ->setActionName('baz');
  197. $this->plugin->postDispatch($this->request);
  198. try {
  199. $this->plugin->postDispatch($this->request);
  200. } catch (Exception $e) {
  201. $this->fail('Repeated calls with no new exceptions should not throw exceptions');
  202. }
  203. }
  204. public function testPostDispatchWithoutException()
  205. {
  206. $this->request->setModuleName('foo')
  207. ->setControllerName('bar')
  208. ->setActionName('baz');
  209. $this->plugin->postDispatch($this->request);
  210. $this->assertEquals('baz', $this->request->getActionName());
  211. $this->assertEquals('bar', $this->request->getControllerName());
  212. $this->assertEquals('foo', $this->request->getModuleName());
  213. }
  214. public function testPostDispatchErrorRequestIsClone()
  215. {
  216. $this->response->setException(new Zend_Controller_Dispatcher_Exception('Testing controller exception'));
  217. $this->request->setModuleName('foo')
  218. ->setControllerName('bar')
  219. ->setActionName('baz');
  220. $this->plugin->postDispatch($this->request);
  221. $this->assertNotNull($this->request->getParam('error_handler'));
  222. $errorHandler = $this->request->getParam('error_handler');
  223. $this->assertTrue($errorHandler instanceof ArrayObject);
  224. $this->assertTrue($errorHandler->request instanceof Zend_Controller_Request_Http);
  225. $this->assertNotSame($this->request, $errorHandler->request);
  226. }
  227. public function testPostDispatchQuitsWithFalseUserErrorHandlerParam()
  228. {
  229. $front = Zend_Controller_Front::getInstance();
  230. $front->resetInstance();
  231. $front->setParam('noErrorHandler', true);
  232. $this->response->setException(new Zend_Controller_Dispatcher_Exception('Testing controller exception'));
  233. $this->request->setModuleName('foo')
  234. ->setControllerName('bar')
  235. ->setActionName('baz');
  236. $this->plugin->postDispatch($this->request);
  237. $this->assertNull($this->request->getParam('error_handler'));
  238. }
  239. }
  240. // Call Zend_Controller_Plugin_ErrorHandlerTest::main() if this source file is executed directly.
  241. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Plugin_ErrorHandlerTest::main")
  242. {
  243. Zend_Controller_Plugin_ErrorHandlerTest::main();
  244. }