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.

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