/standard/tags/release-1.0.1/tests/Zend/Controller/Plugin/ErrorHandlerTest.php

https://github.com/bhaumik25/zend-framework · PHP · 257 lines · 184 code · 37 blank · 36 comment · 3 complexity · c4a731f4f1438ade390b239de55a67ef MD5 · raw file

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