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.

280 lines
8.7 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/ErrorHTTPException.php';
  19. require_once dirname(__FILE__) . '/../../exception/Error404Exception.php';
  20. require_once dirname(__FILE__) . '/../../exception/ErrorHandler.php';
  21. require_once dirname(__FILE__) . '/../../app/router/Route.php';
  22. require_once dirname(__FILE__) . '/../../app/router/Router.php';
  23. require_once dirname(__FILE__) . '/../../app/FrontController.php';
  24. require_once dirname(__FILE__) . '/../../app/Action.php';
  25. require_once dirname(__FILE__) . '/../../app/AjaxAction.php';
  26. class FrontControllerTest extends PHPUnit_Framework_TestCase
  27. {
  28. public function run(PHPUnit_Framework_TestResult $result = NULL)
  29. {
  30. $this->setPreserveGlobalState(false);
  31. return parent::run($result);
  32. }
  33. public function setUp()
  34. {
  35. if (!class_exists('PHPViewMock')) {
  36. $this->getMock('PHPView', array('fetch', 'append', 'prepend', 'assign', 'getTemplate'), array(), 'PHPViewMock', false);
  37. }
  38. if (!class_exists('View')) {
  39. $this->getMock('View');
  40. }
  41. if (!class_exists('ErrorLayout')) {
  42. $this->getMock('ErrorLayout', array('fetch', 'setException'), array(), 'ErrorLayoutMock');
  43. }
  44. if (!class_exists('ErrorActionMock')) {
  45. $this->getMock('ErrorAction', array(), array(), 'ErrorActionMock', false);
  46. }
  47. set_new_overload(array($this, 'newCallback'));
  48. }
  49. /**
  50. * @runInSeparateProcess
  51. */
  52. public function testGetInstanceNoProfiler()
  53. {
  54. $this->setConstants(false);
  55. $controller = FrontController::getInstance();
  56. $this->assertAttributeEquals($controller, 'instance', 'FrontController');
  57. }
  58. /**
  59. * @runInSeparateProcess
  60. */
  61. public function testGetInstanceWithProfiler()
  62. {
  63. $this->setConstants(true);
  64. $controller = FrontController::getInstance();
  65. $this->assertAttributeEquals($controller, 'instance', 'FrontController');
  66. }
  67. /**
  68. * @runInSeparateProcess
  69. */
  70. public function testSetView()
  71. {
  72. $this->setConstants(false);
  73. $controller = FrontController::getInstance();
  74. $this->assertSame($controller, $controller->setView('View'));
  75. }
  76. /**
  77. * @runInSeparateProcess
  78. */
  79. public function testGetDefaultView()
  80. {
  81. $this->setConstants(false);
  82. $controller = FrontController::getInstance();
  83. $this->assertNotInstanceOf('View', $controller->getView());
  84. $this->assertInstanceOf('PHPView', $controller->getView());
  85. }
  86. /**
  87. * @runInSeparateProcess
  88. */
  89. public function testGetCustomView()
  90. {
  91. $this->setConstants(false);
  92. $controller = FrontController::getInstance();
  93. $this->assertInstanceOf('View', $controller->getView('View'));
  94. }
  95. /**
  96. * @runInSeparateProcess
  97. */
  98. public function testSetGetBaseUrl()
  99. {
  100. $this->setConstants(false);
  101. $controller = FrontController::getInstance();
  102. $this->assertSame('', $controller->getBaseUrl());
  103. $controller->setBaseUrl('/index/');
  104. $this->assertSame('/index', $controller->getBaseUrl());
  105. }
  106. /**
  107. * @runInSeparateProcess
  108. */
  109. public function testGetRouter()
  110. {
  111. $this->setConstants(false);
  112. $controller = FrontController::getInstance();
  113. $this->assertInstanceOf('Router', $controller->getRouter());
  114. }
  115. /**
  116. * @runInSeparateProcess
  117. */
  118. public function testExecuteNoRoute()
  119. {
  120. $this->setConstants(false);
  121. $controller = FrontController::getInstance();
  122. $result = $controller->execute();
  123. $controller = FrontController::getInstance();
  124. $this->assertNull($controller->execute());
  125. }
  126. /**
  127. * @runInSeparateProcess
  128. */
  129. public function testExecuteNoRouteDebug()
  130. {
  131. $this->setConstants(true);
  132. $controller = FrontController::getInstance();
  133. $result = $controller->execute();
  134. $this->assertNotEmpty($result);
  135. $this->assertContains('Route for "" not found', $result);
  136. $this->assertContains('Error404Exception', $result);
  137. }
  138. /**
  139. * @runInSeparateProcess
  140. */
  141. public function testExecuteNoAction()
  142. {
  143. $_SERVER['REQUEST_URI'] = '/user/account/213';
  144. $this->setConstants(true);
  145. $controller = FrontController::getInstance();
  146. $router = $controller->getRouter();
  147. $router->add('user', 'user/account/:id', 'user');
  148. $result = $controller->execute();
  149. $this->assertContains('Action class "userAction" not found.', $result);
  150. }
  151. /**
  152. * @runInSeparateProcess
  153. */
  154. public function testExecuteNoLayout()
  155. {
  156. $this->getMock('userAction');
  157. $_SERVER['REQUEST_URI'] = '/user/account/213';
  158. $this->setConstants(true);
  159. $controller = FrontController::getInstance();
  160. $router = $controller->getRouter();
  161. $router->add('user', 'user/account/:id', 'user');
  162. $result = $controller->execute();
  163. $this->assertContains('Layout class "DefaultLayout" not found.', $result);
  164. }
  165. /**
  166. * @runInSeparateProcess
  167. */
  168. public function testExecuteWithLayout()
  169. {
  170. $this->getMock('userAction');
  171. $this->getMock('DefaultLayout', array('fetch'), array(), 'DefaultLayoutMock');
  172. $_SERVER['REQUEST_URI'] = '/user/account/213';
  173. $this->setConstants(true);
  174. $controller = FrontController::getInstance();
  175. $router = $controller->getRouter();
  176. $router->add('user', 'user/account/:id', 'user');
  177. $result = $controller->execute();
  178. $this->assertNull($result);
  179. }
  180. /**
  181. * @runInSeparateProcess
  182. */
  183. public function testExecuteWithLayoutProfiler()
  184. {
  185. Config::set('PROFILER', true);
  186. $this->getMock('userAction');
  187. $this->getMock('DefaultLayout', array('fetch'), array(), 'DefaultLayoutMock');
  188. $_SERVER['REQUEST_URI'] = '/user/account/213';
  189. $this->setConstants(true);
  190. $controller = FrontController::getInstance();
  191. $router = $controller->getRouter();
  192. $router->add('user', 'user/account/:id', 'user');
  193. $result = $controller->execute();
  194. $this->assertNull($result);
  195. }
  196. /**
  197. * @runInSeparateProcess
  198. */
  199. public function testExecuteWithAjaxAction()
  200. {
  201. $this->getMock('userAction');
  202. $this->getMock('DefaultLayout', array('fetch'), array(), 'DefaultLayoutMock');
  203. $_SERVER['REQUEST_URI'] = '/user/account/213';
  204. $this->setConstants(true);
  205. $controller = FrontController::getInstance();
  206. $router = $controller->getRouter();
  207. $router->add('user', 'user/account/:id', 'NewAjax');
  208. $result = $controller->execute();
  209. $this->assertNull($result);
  210. }
  211. /**
  212. * @runInSeparateProcess
  213. */
  214. public function testExecuteWithAjaxActionProfiler()
  215. {
  216. Config::set('PROFILER', true);
  217. $this->getMock('userAction');
  218. $this->getMock('DefaultLayout', array('fetch'), array(), 'DefaultLayoutMock');
  219. $_SERVER['REQUEST_URI'] = '/user/account/213';
  220. $this->setConstants(true);
  221. $controller = FrontController::getInstance();
  222. $router = $controller->getRouter();
  223. $router->add('user', 'user/account/:id', 'NewAjax');
  224. $result = $controller->execute();
  225. $this->assertNull($result);
  226. }
  227. private function setConstants($val = false)
  228. {
  229. Config::set('DEBUG', $val);
  230. }
  231. public function tearDown()
  232. {
  233. unset_new_overload();
  234. }
  235. protected function newCallback($className)
  236. {
  237. switch ($className) {
  238. case 'PHPView':
  239. return 'PHPViewMock';
  240. case 'DefaultLayout':
  241. return 'DefaultLayoutMock';
  242. case 'ErrorAction':
  243. return 'ErrorActionMock';
  244. case 'ErrorLayout':
  245. return 'ErrorLayoutMock';
  246. default:
  247. return $className;
  248. }
  249. }
  250. }
  251. /**
  252. * Used in testExecuteWithAjaxAction
  253. */
  254. class NewAjaxAction extends AjaxAction{
  255. protected function execute() {}
  256. }