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.

296 lines
9.1 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('setAjaxError'), 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. $this->assertNull($controller->execute());
  123. }
  124. /**
  125. * @runInSeparateProcess
  126. */
  127. public function testExecuteNoRouteDebug()
  128. {
  129. $this->setConstants(true);
  130. $controller = FrontController::getInstance();
  131. $result = $controller->execute();
  132. $this->assertNotEmpty($result);
  133. $this->assertContains('Route for "" not found', $result);
  134. $this->assertContains('Error404Exception', $result);
  135. }
  136. /**
  137. * @runInSeparateProcess
  138. */
  139. public function testExecuteNoAction()
  140. {
  141. $_SERVER['REQUEST_URI'] = '/user/account/213';
  142. $this->setConstants(true);
  143. $controller = FrontController::getInstance();
  144. $router = $controller->getRouter();
  145. $router->add('user', 'user/account/:id', 'user');
  146. $result = $controller->execute();
  147. $this->assertContains('Action class "userAction" not found.', $result);
  148. }
  149. /**
  150. * @runInSeparateProcess
  151. */
  152. public function testExecuteNoLayout()
  153. {
  154. $this->getMock('userAction');
  155. $_SERVER['REQUEST_URI'] = '/user/account/213';
  156. $this->setConstants(true);
  157. $controller = FrontController::getInstance();
  158. $router = $controller->getRouter();
  159. $router->add('user', 'user/account/:id', 'user');
  160. $result = $controller->execute();
  161. $this->assertContains('Layout class "DefaultLayout" not found.', $result);
  162. }
  163. /**
  164. * @runInSeparateProcess
  165. */
  166. public function testExecuteWithLayout()
  167. {
  168. $this->getMock('userAction');
  169. $this->getMock('DefaultLayout', array('fetch'), array(), 'DefaultLayoutMock');
  170. $_SERVER['REQUEST_URI'] = '/user/account/213';
  171. $this->setConstants(true);
  172. $controller = FrontController::getInstance();
  173. $router = $controller->getRouter();
  174. $router->add('user', 'user/account/:id', 'user');
  175. $result = $controller->execute();
  176. $this->assertNull($result);
  177. }
  178. /**
  179. * @runInSeparateProcess
  180. */
  181. public function testExecuteWithLayoutProfiler()
  182. {
  183. Config::set('PROFILER', true);
  184. $this->getMock('userAction');
  185. $this->getMock('DefaultLayout', array('fetch'), array(), 'DefaultLayoutMock');
  186. $_SERVER['REQUEST_URI'] = '/user/account/213';
  187. $this->setConstants(true);
  188. $controller = FrontController::getInstance();
  189. $router = $controller->getRouter();
  190. $router->add('user', 'user/account/:id', 'user');
  191. $result = $controller->execute();
  192. $this->assertNull($result);
  193. }
  194. /**
  195. * @runInSeparateProcess
  196. */
  197. public function testExecuteWithAjaxAction()
  198. {
  199. $this->getMock('userAction');
  200. $this->getMock('DefaultLayout', array('fetch'), array(), 'DefaultLayoutMock');
  201. $_SERVER['REQUEST_URI'] = '/user/account/213';
  202. $this->setConstants(true);
  203. $controller = FrontController::getInstance();
  204. $router = $controller->getRouter();
  205. $router->add('user', 'user/account/:id', 'NewAjax');
  206. $result = $controller->execute();
  207. $this->assertNull($result);
  208. }
  209. /**
  210. * @runInSeparateProcess
  211. */
  212. public function testExecuteWithAjaxActionError()
  213. {
  214. $this->getMock('userAction');
  215. $_SERVER['REQUEST_URI'] = '/user/account/213';
  216. $this->setConstants(false);
  217. $controller = FrontController::getInstance();
  218. $router = $controller->getRouter();
  219. $router->add('user', 'user/account/:id', 'NewAjax');
  220. $result = $controller->execute();
  221. $this->assertNull($result);
  222. }
  223. /**
  224. * @runInSeparateProcess
  225. */
  226. public function testExecuteWithAjaxActionProfiler()
  227. {
  228. Config::set('PROFILER', true);
  229. $this->getMock('userAction');
  230. $this->getMock('DefaultLayout', array('fetch'), array(), 'DefaultLayoutMock');
  231. $_SERVER['REQUEST_URI'] = '/user/account/213';
  232. $this->setConstants(true);
  233. $controller = FrontController::getInstance();
  234. $router = $controller->getRouter();
  235. $router->add('user', 'user/account/:id', 'NewAjax');
  236. $result = $controller->execute();
  237. $this->assertNull($result);
  238. }
  239. private function setConstants($val = false)
  240. {
  241. Config::set('DEBUG', $val);
  242. }
  243. public function tearDown()
  244. {
  245. unset_new_overload();
  246. }
  247. protected function newCallback($className)
  248. {
  249. switch ($className) {
  250. case 'PHPView':
  251. return 'PHPViewMock';
  252. case 'DefaultLayout':
  253. return 'DefaultLayoutMock';
  254. case 'ErrorAction':
  255. return 'ErrorActionMock';
  256. case 'ErrorLayout':
  257. return 'ErrorLayoutMock';
  258. default:
  259. return $className;
  260. }
  261. }
  262. }
  263. /**
  264. * Used in testExecuteWithAjaxAction
  265. */
  266. class NewAjaxAction extends AjaxAction
  267. {
  268. protected function execute()
  269. {
  270. }
  271. }