2
0

ErrorHandlerTest.php 11 KB

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