You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
7.4 KiB

  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-11-1
  8. *
  9. * Unit tests for FrontController class
  10. */
  11. require_once dirname(__FILE__) . '/../../session/Session.php';
  12. require_once dirname(__FILE__) . '/../../classes/Env.class.php';
  13. require_once dirname(__FILE__) . '/../../Registry.php';
  14. require_once dirname(__FILE__) . '/../../Config.php';
  15. require_once dirname(__FILE__) . '/../../util/FirePHPCore-0.3.2/lib/FirePHPCore/fb.php';
  16. require_once dirname(__FILE__) . '/../../util/profiler/Profiler.php';
  17. require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
  18. require_once dirname(__FILE__) . '/../../exception/Error404Exception.php';
  19. require_once dirname(__FILE__) . '/../../exception/ErrorHandler.php';
  20. require_once dirname(__FILE__) . '/../../app/router/Route.php';
  21. require_once dirname(__FILE__) . '/../../app/router/Router.php';
  22. require_once dirname(__FILE__) . '/../../app/FrontController.php';
  23. require_once dirname(__FILE__) . '/../../app/Action.php';
  24. require_once dirname(__FILE__) . '/../../app/AjaxAction.php';
  25. class FrontControllerTest extends PHPUnit_Framework_TestCase
  26. {
  27. public function run(PHPUnit_Framework_TestResult $result = NULL)
  28. {
  29. $this->setPreserveGlobalState(false);
  30. return parent::run($result);
  31. }
  32. public function setUp()
  33. {
  34. if (!class_exists('PHPViewMock')) {
  35. $this->getMock('PHPView', array('fetch', 'append', 'prepend', 'assign', 'getTemplate'), array(), 'PHPViewMock', false);
  36. }
  37. if (!class_exists('View')) {
  38. $this->getMock('View');
  39. }
  40. if (!class_exists('ErrorLayout')) {
  41. $this->getMock('ErrorLayout', array('fetch'), array(), 'ErrorLayoutMock');
  42. }
  43. if (!class_exists('ErrorActionMock')) {
  44. $this->getMock('ErrorAction', array(), array(), 'ErrorActionMock', false);
  45. }
  46. set_new_overload(array($this, 'newCallback'));
  47. }
  48. /**
  49. * @runInSeparateProcess
  50. */
  51. public function testGetInstanceNoProfiler()
  52. {
  53. $this->setConstants(false);
  54. $controller = FrontController::getInstance();
  55. $this->assertAttributeEquals($controller, 'instance', 'FrontController');
  56. }
  57. /**
  58. * @runInSeparateProcess
  59. */
  60. public function testGetInstanceWithProfiler()
  61. {
  62. $this->setConstants(true);
  63. $controller = FrontController::getInstance();
  64. $this->assertAttributeEquals($controller, 'instance', 'FrontController');
  65. }
  66. /**
  67. * @runInSeparateProcess
  68. */
  69. public function testSetView()
  70. {
  71. $this->setConstants(false);
  72. $controller = FrontController::getInstance();
  73. $this->assertEquals($controller, $controller->setView('View'));
  74. }
  75. /**
  76. * @runInSeparateProcess
  77. */
  78. public function testGetDefaultView()
  79. {
  80. $this->setConstants(false);
  81. $controller = FrontController::getInstance();
  82. $this->assertNotInstanceOf('View', $controller->getView());
  83. $this->assertInstanceOf('PHPView', $controller->getView());
  84. }
  85. /**
  86. * @runInSeparateProcess
  87. */
  88. public function testGetCustomView()
  89. {
  90. $this->setConstants(false);
  91. $controller = FrontController::getInstance();
  92. $this->assertInstanceOf('View', $controller->getView('View'));
  93. }
  94. /**
  95. * @runInSeparateProcess
  96. */
  97. public function testSetGetBaseUrl()
  98. {
  99. $this->setConstants(false);
  100. $controller = FrontController::getInstance();
  101. $this->assertEquals('', $controller->getBaseUrl());
  102. $controller->setBaseUrl('/index/');
  103. $this->assertEquals('/index', $controller->getBaseUrl());
  104. }
  105. /**
  106. * @runInSeparateProcess
  107. */
  108. public function testGetRouter()
  109. {
  110. $this->setConstants(false);
  111. $controller = FrontController::getInstance();
  112. $this->assertInstanceOf('Router', $controller->getRouter());
  113. }
  114. /**
  115. * @runInSeparateProcess
  116. */
  117. public function testExecuteNoRoute()
  118. {
  119. $this->setConstants(false);
  120. $controller = FrontController::getInstance();
  121. $this->assertNull($controller->execute());
  122. }
  123. /**
  124. * @runInSeparateProcess
  125. */
  126. public function testExecuteNoRouteDebug()
  127. {
  128. $this->setConstants(true);
  129. $controller = FrontController::getInstance();
  130. $result = $controller->execute();
  131. $this->assertNotEmpty($result);
  132. $this->assertContains('Route for "" not found', $result);
  133. $this->assertContains('Error404Exception', $result);
  134. }
  135. /**
  136. * @runInSeparateProcess
  137. */
  138. public function testExecuteNoAction()
  139. {
  140. $_SERVER['REQUEST_URI'] = '/user/account/213';
  141. $this->setConstants(true);
  142. $controller = FrontController::getInstance();
  143. $router = $controller->getRouter();
  144. $router->add('user', 'user/account/:id', 'user');
  145. $result = $controller->execute();
  146. $this->assertContains('Action class "userAction" not found.', $result);
  147. }
  148. /**
  149. * @runInSeparateProcess
  150. */
  151. public function testExecuteNoLayout()
  152. {
  153. $this->getMock('userAction');
  154. $_SERVER['REQUEST_URI'] = '/user/account/213';
  155. $this->setConstants(true);
  156. $controller = FrontController::getInstance();
  157. $router = $controller->getRouter();
  158. $router->add('user', 'user/account/:id', 'user');
  159. $result = $controller->execute();
  160. $this->assertContains('Layout class "DefaultLayout" not found.', $result);
  161. }
  162. /**
  163. * @runInSeparateProcess
  164. */
  165. public function testExecuteWithLayout()
  166. {
  167. $this->getMock('userAction');
  168. $this->getMock('DefaultLayout', array('fetch'), array(), 'DefaultLayoutMock');
  169. $_SERVER['REQUEST_URI'] = '/user/account/213';
  170. $this->setConstants(true);
  171. $controller = FrontController::getInstance();
  172. $router = $controller->getRouter();
  173. $router->add('user', 'user/account/:id', 'user');
  174. $result = $controller->execute();
  175. $this->assertEmpty($result);
  176. }
  177. /**
  178. * @runInSeparateProcess
  179. */
  180. public function testExecuteWithAjaxAction()
  181. {
  182. $this->getMock('userAction');
  183. $this->getMock('DefaultLayout', array('fetch'), array(), 'DefaultLayoutMock');
  184. $_SERVER['REQUEST_URI'] = '/user/account/213';
  185. $this->setConstants(true);
  186. $controller = FrontController::getInstance();
  187. $router = $controller->getRouter();
  188. $router->add('user', 'user/account/:id', 'NewAjax');
  189. $result = $controller->execute();
  190. $this->assertEmpty($result);
  191. }
  192. private function setConstants($val = false)
  193. {
  194. if (!defined('DEBUG')) {
  195. define('DEBUG', $val);
  196. }
  197. }
  198. public function tearDown()
  199. {
  200. unset_new_overload();
  201. }
  202. protected function newCallback($className)
  203. {
  204. switch ($className) {
  205. case 'PHPView':
  206. return 'PHPViewMock';
  207. case 'DefaultLayout':
  208. return 'DefaultLayoutMock';
  209. case 'userAction':
  210. return 'userAction';
  211. case 'ErrorAction':
  212. return 'ErrorActionMock';
  213. case 'ErrorLayout':
  214. return 'ErrorLayoutMock';
  215. default:
  216. return $className;
  217. }
  218. }
  219. }
  220. class NewAjaxAction extends AjaxAction{
  221. protected function execute() {}
  222. }