PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/cake/tests/cases/libs/error.test.php

http://skygames.googlecode.com/
PHP | 451 lines | 189 code | 3 blank | 259 comment | 4 complexity | 7b0d06d1196cbfd4ec90cbf647110cf9 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, CC-BY-SA-3.0
  1. <?php
  2. /* SVN FILE: $Id: error.test.php 7846 2008-11-08 01:00:05Z renan.saddam $ */
  3. /**
  4. * Short description for file.
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19. * @package cake.tests
  20. * @subpackage cake.tests.cases.libs
  21. * @since CakePHP(tm) v 1.2.0.5432
  22. * @version $Revision: 7846 $
  23. * @modifiedby $LastChangedBy: renan.saddam $
  24. * @lastmodified $Date: 2008-11-07 19:00:05 -0600 (Fri, 07 Nov 2008) $
  25. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26. */
  27. if (class_exists('TestErrorHandler')) {
  28. return;
  29. }
  30. if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
  31. define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
  32. }
  33. /**
  34. * BlueberryComponent class
  35. *
  36. * @package cake.tests
  37. * @subpackage cake.tests.cases.libs
  38. */
  39. class BlueberryComponent extends Object {
  40. /**
  41. * testName property
  42. *
  43. * @access public
  44. * @return void
  45. */
  46. var $testName = null;
  47. /**
  48. * initialize method
  49. *
  50. * @access public
  51. * @return void
  52. */
  53. function initialize(&$controller) {
  54. $this->testName = 'BlueberryComponent';
  55. }
  56. }
  57. /**
  58. * BlueberryDispatcher class
  59. *
  60. * @package cake.tests
  61. * @subpackage cake.tests.cases.libs
  62. */
  63. class BlueberryDispatcher extends Dispatcher {
  64. /**
  65. * cakeError method
  66. *
  67. * @access public
  68. * @return void
  69. */
  70. function cakeError($method, $messages = array()) {
  71. $error = new TestErrorHandler($method, $messages);
  72. return $error;
  73. }
  74. }
  75. /**
  76. * Short description for class.
  77. *
  78. * @package cake.tests
  79. * @subpackage cake.tests.cases.libs
  80. */
  81. class AuthBlueberryUser extends CakeTestModel {
  82. /**
  83. * name property
  84. *
  85. * @var string 'AuthBlueberryUser'
  86. * @access public
  87. */
  88. var $name = 'AuthBlueberryUser';
  89. /**
  90. * useTable property
  91. *
  92. * @var string
  93. * @access public
  94. */
  95. var $useTable = false;
  96. }
  97. if (!class_exists('AppController')) {
  98. /**
  99. * AppController class
  100. *
  101. * @package cake.tests
  102. * @subpackage cake.tests.cases.libs
  103. */
  104. class AppController extends Controller {
  105. /**
  106. * components property
  107. *
  108. * @access public
  109. * @return void
  110. */
  111. var $components = array('Blueberry');
  112. /**
  113. * beforeRender method
  114. *
  115. * @access public
  116. * @return void
  117. */
  118. function beforeRender() {
  119. echo $this->Blueberry->testName;
  120. }
  121. /**
  122. * header method
  123. *
  124. * @access public
  125. * @return void
  126. */
  127. function header($header) {
  128. echo $header;
  129. }
  130. /**
  131. * _stop method
  132. *
  133. * @access public
  134. * @return void
  135. */
  136. function _stop($status = 0) {
  137. echo 'Stopped with status: ' . $status;
  138. }
  139. }
  140. }
  141. App::import('Core', array('Error', 'Controller'));
  142. /**
  143. * TestErrorController class
  144. *
  145. * @package cake.tests
  146. * @subpackage cake.tests.cases.libs
  147. */
  148. class TestErrorController extends AppController {
  149. /**
  150. * uses property
  151. *
  152. * @var array
  153. * @access public
  154. */
  155. var $uses = array();
  156. /**
  157. * index method
  158. *
  159. * @access public
  160. * @return void
  161. */
  162. function index() {
  163. $this->autoRender = false;
  164. return 'what up';
  165. }
  166. }
  167. /**
  168. * BlueberryController class
  169. *
  170. * @package cake.tests
  171. * @subpackage cake.tests.cases.libs
  172. */
  173. class BlueberryController extends AppController {
  174. /**
  175. * name property
  176. *
  177. * @access public
  178. * @return void
  179. */
  180. var $name = 'BlueberryController';
  181. /**
  182. * uses property
  183. *
  184. * @access public
  185. * @return void
  186. */
  187. var $uses = array('AuthBlueberryUser');
  188. /**
  189. * components property
  190. *
  191. * @access public
  192. * @return void
  193. */
  194. var $components = array('Auth');
  195. }
  196. /**
  197. * TestErrorHandler class
  198. *
  199. * @package cake.tests
  200. * @subpackage cake.tests.cases.libs
  201. */
  202. class TestErrorHandler extends ErrorHandler {
  203. /**
  204. * stop method
  205. *
  206. * @access public
  207. * @return void
  208. */
  209. function _stop() {
  210. return;
  211. }
  212. }
  213. /**
  214. * Short description for class.
  215. *
  216. * @package cake.tests
  217. * @subpackage cake.tests.cases.libs
  218. */
  219. class TestErrorHandlerTest extends CakeTestCase {
  220. /**
  221. * skip method
  222. *
  223. * @access public
  224. * @return void
  225. */
  226. function skip() {
  227. $this->skipif ((PHP_SAPI == 'cli'), 'TestErrorHandlerTest cannot be run from console');
  228. }
  229. /**
  230. * testError method
  231. *
  232. * @access public
  233. * @return void
  234. */
  235. function testError() {
  236. ob_start();
  237. $TestErrorHandler = new TestErrorHandler('error404', array('message' => 'Page not found'));
  238. ob_clean();
  239. ob_start();
  240. $TestErrorHandler->error(array(
  241. 'code' => 404,
  242. 'message' => 'Page not Found',
  243. 'name' => "Couldn't find what you were looking for"));
  244. $result = ob_get_clean();
  245. $this->assertPattern("/<h2>Couldn't find what you were looking for<\/h2>/", $result);
  246. $this->assertPattern('/Page not Found/', $result);
  247. }
  248. /**
  249. * testError404 method
  250. *
  251. * @access public
  252. * @return void
  253. */
  254. function testError404() {
  255. ob_start();
  256. $TestErrorHandler = new TestErrorHandler('error404', array('message' => 'Page not found', 'url' => '/test_error'));
  257. $result = ob_get_clean();
  258. $this->assertPattern('/<h2>Not Found<\/h2>/', $result);
  259. $this->assertPattern("/<strong>'\/test_error'<\/strong>/", $result);
  260. }
  261. /**
  262. * testMissingController method
  263. *
  264. * @access public
  265. * @return void
  266. */
  267. function testMissingController() {
  268. ob_start();
  269. $TestErrorHandler = new TestErrorHandler('missingController', array('className' => 'PostsController'));
  270. $result = ob_get_clean();
  271. $this->assertPattern('/<h2>Missing Controller<\/h2>/', $result);
  272. $this->assertPattern('/<em>PostsController<\/em>/', $result);
  273. $this->assertPattern('/BlueberryComponent/', $result);
  274. }
  275. /**
  276. * testMissingAction method
  277. *
  278. * @access public
  279. * @return void
  280. */
  281. function testMissingAction() {
  282. ob_start();
  283. $TestErrorHandler = new TestErrorHandler('missingAction', array('className' => 'PostsController', 'action' => 'index'));
  284. $result = ob_get_clean();
  285. $this->assertPattern('/<h2>Missing Method in PostsController<\/h2>/', $result);
  286. $this->assertPattern('/<em>PostsController::<\/em><em>index\(\)<\/em>/', $result);
  287. ob_start();
  288. $dispatcher = new BlueberryDispatcher('/blueberry/inexistent');
  289. $result = ob_get_clean();
  290. $this->assertPattern('/<h2>Missing Method in BlueberryController<\/h2>/', $result);
  291. $this->assertPattern('/<em>BlueberryController::<\/em><em>inexistent\(\)<\/em>/', $result);
  292. $this->assertNoPattern('/Location: (.*)\/users\/login/', $result);
  293. $this->assertNoPattern('/Stopped with status: 0/', $result);
  294. }
  295. /**
  296. * testPrivateAction method
  297. *
  298. * @access public
  299. * @return void
  300. */
  301. function testPrivateAction() {
  302. ob_start();
  303. $TestErrorHandler = new TestErrorHandler('privateAction', array('className' => 'PostsController', 'action' => '_secretSauce'));
  304. $result = ob_get_clean();
  305. $this->assertPattern('/<h2>Private Method in PostsController<\/h2>/', $result);
  306. $this->assertPattern('/<em>PostsController::<\/em><em>_secretSauce\(\)<\/em>/', $result);
  307. }
  308. /**
  309. * testMissingTable method
  310. *
  311. * @access public
  312. * @return void
  313. */
  314. function testMissingTable() {
  315. ob_start();
  316. $TestErrorHandler = new TestErrorHandler('missingTable', array('className' => 'Article', 'table' => 'articles'));
  317. $result = ob_get_clean();
  318. $this->assertPattern('/<h2>Missing Database Table<\/h2>/', $result);
  319. $this->assertPattern('/table <em>articles<\/em> for model <em>Article<\/em>/', $result);
  320. }
  321. /**
  322. * testMissingDatabase method
  323. *
  324. * @access public
  325. * @return void
  326. */
  327. function testMissingDatabase() {
  328. ob_start();
  329. $TestErrorHandler = new TestErrorHandler('missingDatabase', array());
  330. $result = ob_get_clean();
  331. $this->assertPattern('/<h2>Missing Database Connection<\/h2>/', $result);
  332. $this->assertPattern('/Confirm you have created the file/', $result);
  333. }
  334. /**
  335. * testMissingView method
  336. *
  337. * @access public
  338. * @return void
  339. */
  340. function testMissingView() {
  341. restore_error_handler();
  342. ob_start();
  343. $TestErrorHandler = new TestErrorHandler('missingView', array('className' => 'Pages', 'action' => 'display', 'file' => 'pages/about.ctp', 'base' => ''));
  344. $expected = ob_get_clean();
  345. set_error_handler('simpleTestErrorHandler');
  346. $this->assertPattern("/PagesController::/", $expected);
  347. $this->assertPattern("/pages\/about.ctp/", $expected);
  348. }
  349. /**
  350. * testMissingLayout method
  351. *
  352. * @access public
  353. * @return void
  354. */
  355. function testMissingLayout() {
  356. restore_error_handler();
  357. ob_start();
  358. $TestErrorHandler = new TestErrorHandler('missingLayout', array( 'layout' => 'my_layout', 'file' => 'layouts/my_layout.ctp', 'base' => ''));
  359. $expected = ob_get_clean();
  360. set_error_handler('simpleTestErrorHandler');
  361. $this->assertPattern("/Missing Layout/", $expected);
  362. $this->assertPattern("/layouts\/my_layout.ctp/", $expected);
  363. }
  364. /**
  365. * testMissingConnection method
  366. *
  367. * @access public
  368. * @return void
  369. */
  370. function testMissingConnection() {
  371. ob_start();
  372. $TestErrorHandler = new TestErrorHandler('missingConnection', array('className' => 'Article'));
  373. $result = ob_get_clean();
  374. $this->assertPattern('/<h2>Missing Database Connection<\/h2>/', $result);
  375. $this->assertPattern('/Article requires a database connection/', $result);
  376. }
  377. /**
  378. * testMissingHelperFile method
  379. *
  380. * @access public
  381. * @return void
  382. */
  383. function testMissingHelperFile() {
  384. ob_start();
  385. $TestErrorHandler = new TestErrorHandler('missingHelperFile', array('helper' => 'MyCustom', 'file' => 'my_custom.php'));
  386. $result = ob_get_clean();
  387. $this->assertPattern('/<h2>Missing Helper File<\/h2>/', $result);
  388. $this->assertPattern('/Create the class below in file:/', $result);
  389. $this->assertPattern('/(\/|\\\)my_custom.php/', $result);
  390. }
  391. /**
  392. * testMissingHelperClass method
  393. *
  394. * @access public
  395. * @return void
  396. */
  397. function testMissingHelperClass() {
  398. ob_start();
  399. $TestErrorHandler = new TestErrorHandler('missingHelperClass', array('helper' => 'MyCustom', 'file' => 'my_custom.php'));
  400. $result = ob_get_clean();
  401. $this->assertPattern('/<h2>Missing Helper Class<\/h2>/', $result);
  402. $this->assertPattern('/The helper class <em>MyCustomHelper<\/em> can not be found or does not exist./', $result);
  403. $this->assertPattern('/(\/|\\\)my_custom.php/', $result);
  404. }
  405. /**
  406. * testMissingComponentFile method
  407. *
  408. * @access public
  409. * @return void
  410. */
  411. function testMissingComponentFile() {
  412. ob_start();
  413. $TestErrorHandler = new TestErrorHandler('missingComponentFile', array('className' => 'PostsController', 'component' => 'Sidebox', 'file' => 'sidebox.php'));
  414. $result = ob_get_clean();
  415. $this->assertPattern('/<h2>Missing Component File<\/h2>/', $result);
  416. $this->assertPattern('/Create the class <em>SideboxComponent<\/em> in file:/', $result);
  417. $this->assertPattern('/(\/|\\\)sidebox.php/', $result);
  418. }
  419. /**
  420. * testMissingComponentClass method
  421. *
  422. * @access public
  423. * @return void
  424. */
  425. function testMissingComponentClass() {
  426. ob_start();
  427. $TestErrorHandler = new TestErrorHandler('missingComponentClass', array('className' => 'PostsController', 'component' => 'Sidebox', 'file' => 'sidebox.php'));
  428. $result = ob_get_clean();
  429. $this->assertPattern('/<h2>Missing Component Class<\/h2>/', $result);
  430. $this->assertPattern('/Create the class <em>SideboxComponent<\/em> in file:/', $result);
  431. $this->assertPattern('/(\/|\\\)sidebox.php/', $result);
  432. }
  433. /**
  434. * testMissingModel method
  435. *
  436. * @access public
  437. * @return void
  438. */
  439. function testMissingModel() {
  440. ob_start();
  441. $TestErrorHandler = new TestErrorHandler('missingModel', array('className' => 'Article', 'file' => 'article.php'));
  442. $result = ob_get_clean();
  443. $this->assertPattern('/<h2>Missing Model<\/h2>/', $result);
  444. $this->assertPattern('/<em>Article<\/em> could not be found./', $result);
  445. $this->assertPattern('/(\/|\\\)article.php/', $result);
  446. }
  447. }
  448. ?>