PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

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