PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/hardsshah/bookmarks
PHP | 411 lines | 251 code | 43 blank | 117 comment | 0 complexity | 15e4d0b2126f636dd32aaa052128dc51 MD5 | raw file
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * CakeTestCaseTest file
  5. *
  6. * Test Case for CakeTestCase class
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP : Rapid Development Framework (http://www.cakephp.org)
  11. * Copyright 2006-2008, Cake Software Foundation, Inc.
  12. *
  13. * Licensed under The MIT License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2006-2008, Cake Software Foundation, Inc.
  18. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project
  19. * @package cake
  20. * @subpackage cake.cake.libs.
  21. * @since CakePHP v 1.2.0.4487
  22. * @version $Revision$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  25. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  26. */
  27. App::import('Core', 'CakeTestCase');
  28. App::import('Core', 'AppController');
  29. Mock::generate('CakeHtmlReporter');
  30. Mock::generate('CakeTestCase', 'CakeDispatcherMockTestCase');
  31. SimpleTest::ignore('SubjectCakeTestCase');
  32. SimpleTest::ignore('CakeDispatcherMockTestCase');
  33. /**
  34. * SubjectCakeTestCase
  35. *
  36. * @package cake
  37. * @subpackage cake.tests.cases.libs
  38. */
  39. class SubjectCakeTestCase extends CakeTestCase {
  40. /**
  41. * Feed a Mocked Reporter to the subject case
  42. * prevents its pass/fails from affecting the real test
  43. *
  44. * @param string $reporter
  45. * @access public
  46. * @return void
  47. */
  48. function setReporter(&$reporter) {
  49. $this->_reporter = &$reporter;
  50. }
  51. /**
  52. * testDummy method
  53. *
  54. * @return void
  55. * @access public
  56. */
  57. function testDummy() {
  58. }
  59. }
  60. /**
  61. * CakeTestCaseTest
  62. *
  63. * @package cake
  64. * @subpackage cake.tests.cases.libs
  65. */
  66. class CakeTestCaseTest extends CakeTestCase {
  67. /**
  68. * setUp
  69. *
  70. * @access public
  71. * @return void
  72. */
  73. function setUp() {
  74. $this->Case =& new SubjectCakeTestCase();
  75. $reporter =& new MockCakeHtmlReporter();
  76. $this->Case->setReporter($reporter);
  77. $this->Reporter = $reporter;
  78. }
  79. /**
  80. * tearDown
  81. *
  82. * @access public
  83. * @return void
  84. */
  85. function tearDown() {
  86. unset($this->Case);
  87. unset($this->Reporter);
  88. }
  89. /**
  90. * testAssertGoodTags
  91. *
  92. * @access public
  93. * @return void
  94. */
  95. function testAssertGoodTags() {
  96. $this->Reporter->expectAtLeastOnce('paintPass');
  97. $this->Reporter->expectNever('paintFail');
  98. $input = '<p>Text</p>';
  99. $pattern = array(
  100. '<p',
  101. 'Text',
  102. '/p',
  103. );
  104. $this->assertTrue($this->Case->assertTags($input, $pattern));
  105. $input = '<a href="/test.html" class="active">My link</a>';
  106. $pattern = array(
  107. 'a' => array('href' => '/test.html', 'class' => 'active'),
  108. 'My link',
  109. '/a'
  110. );
  111. $this->assertTrue($this->Case->assertTags($input, $pattern));
  112. $pattern = array(
  113. 'a' => array('class' => 'active', 'href' => '/test.html'),
  114. 'My link',
  115. '/a'
  116. );
  117. $this->assertTrue($this->Case->assertTags($input, $pattern));
  118. $input = "<a href=\"/test.html\"\t\n\tclass=\"active\"\tid=\"primary\">\t<span>My link</span></a>";
  119. $pattern = array(
  120. 'a' => array('id' => 'primary', 'href' => '/test.html', 'class' => 'active'),
  121. '<span',
  122. 'My link',
  123. '/span',
  124. '/a'
  125. );
  126. $this->assertTrue($this->Case->assertTags($input, $pattern));
  127. $input = '<p class="info"><a href="/test.html" class="active"><strong onClick="alert(\'hey\');">My link</strong></a></p>';
  128. $pattern = array(
  129. 'p' => array('class' => 'info'),
  130. 'a' => array('class' => 'active', 'href' => '/test.html' ),
  131. 'strong' => array('onClick' => 'alert(\'hey\');'),
  132. 'My link',
  133. '/strong',
  134. '/a',
  135. '/p'
  136. );
  137. $this->assertTrue($this->Case->assertTags($input, $pattern));
  138. }
  139. /**
  140. * testBadAssertTags
  141. *
  142. * @access public
  143. * @return void
  144. */
  145. function testBadAssertTags() {
  146. $this->Reporter->expectAtLeastOnce('paintFail');
  147. $this->Reporter->expectNever('paintPass');
  148. $input = '<a href="/test.html" class="active">My link</a>';
  149. $pattern = array(
  150. 'a' => array('hRef' => '/test.html', 'clAss' => 'active'),
  151. 'My link',
  152. '/a'
  153. );
  154. $this->assertFalse($this->Case->assertTags($input, $pattern));
  155. $input = '<a href="/test.html" class="active">My link</a>';
  156. $pattern = array(
  157. '<a' => array('href' => '/test.html', 'class' => 'active'),
  158. 'My link',
  159. '/a'
  160. );
  161. $this->assertFalse($this->Case->assertTags($input, $pattern));
  162. }
  163. /**
  164. * testBefore
  165. *
  166. * @access public
  167. * @return void
  168. */
  169. function testBefore() {
  170. $this->Case->before('testDummy');
  171. $this->assertFalse(isset($this->Case->db));
  172. $this->Case->fixtures = array('core.post');
  173. $this->Case->before('start');
  174. $this->assertTrue(isset($this->Case->db));
  175. $this->assertTrue(isset($this->Case->_fixtures['core.post']));
  176. $this->assertTrue(is_a($this->Case->_fixtures['core.post'], 'CakeTestFixture'));
  177. $this->assertEqual($this->Case->_fixtureClassMap['Post'], 'core.post');
  178. }
  179. /**
  180. * testAfter
  181. *
  182. * @access public
  183. * @return void
  184. */
  185. function testAfter() {
  186. $this->Case->after('testDummy');
  187. $this->assertFalse($this->Case->__truncated);
  188. $this->Case->fixtures = array('core.post');
  189. $this->Case->before('start');
  190. $this->Case->start();
  191. $this->Case->after('testDummy');
  192. $this->assertTrue($this->Case->__truncated);
  193. }
  194. /**
  195. * testLoadFixtures
  196. *
  197. * @access public
  198. * @return void
  199. */
  200. function testLoadFixtures() {
  201. $this->Case->fixtures = array('core.post');
  202. $this->Case->autoFixtures = false;
  203. $this->Case->before('start');
  204. $this->expectError();
  205. $this->Case->loadFixtures('Wrong!');
  206. }
  207. /**
  208. * testGetTests Method
  209. *
  210. * @return void
  211. * @access public
  212. */
  213. function testGetTests() {
  214. $result = $this->Case->getTests();
  215. $this->assertEqual(array_slice($result, 0, 2), array('start', 'startCase'));
  216. $this->assertEqual(array_slice($result, -2), array('endCase', 'end'));
  217. }
  218. /**
  219. * TestTestAction
  220. *
  221. * @access public
  222. * @return void
  223. **/
  224. function testTestAction() {
  225. $_back = array(
  226. 'controller' => Configure::read('controllerPaths'),
  227. 'view' => Configure::read('viewPaths'),
  228. 'model' => Configure::read('modelPaths'),
  229. 'plugin' => Configure::read('pluginPaths')
  230. );
  231. Configure::write('controllerPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'controllers' . DS));
  232. Configure::write('viewPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS));
  233. Configure::write('modelPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'models' . DS));
  234. Configure::write('pluginPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS));
  235. $result = $this->Case->testAction('/tests_apps/index', array('return' => 'view'));
  236. $this->assertPattern('/This is the TestsAppsController index view/', $result);
  237. $result = $this->Case->testAction('/tests_apps/index', array('return' => 'contents'));
  238. $this->assertPattern('/This is the TestsAppsController index view/', $result);
  239. $this->assertPattern('/<html/', $result);
  240. $this->assertPattern('/<\/html>/', $result);
  241. $result = $this->Case->testAction('/tests_apps/some_method', array('return' => 'result'));
  242. $this->assertEqual($result, 5);
  243. $result = $this->Case->testAction('/tests_apps/set_action', array('return' => 'vars'));
  244. $this->assertEqual($result, array('var' => 'string'));
  245. $result = $this->Case->testAction('/tests_apps_posts/add', array('return' => 'vars'));
  246. $this->assertTrue(array_key_exists('posts', $result));
  247. $this->assertEqual(count($result['posts']), 1);
  248. $result = $this->Case->testAction('/tests_apps_posts/url_var/var1:value1/var2:val2', array(
  249. 'return' => 'vars',
  250. 'method' => 'get',
  251. ));
  252. $this->assertTrue(isset($result['params']['url']['url']));
  253. $this->assertTrue(isset($result['params']['url']['output']));
  254. $this->assertEqual(array_keys($result['params']['named']), array('var1', 'var2'));
  255. $result = $this->Case->testAction('/tests_apps_posts/url_var/gogo/val2', array(
  256. 'return' => 'vars',
  257. 'method' => 'get',
  258. ));
  259. $this->assertEqual($result['params']['pass'], array('gogo', 'val2'));
  260. $result = $this->Case->testAction('/tests_apps_posts/url_var', array(
  261. 'return' => 'vars',
  262. 'method' => 'get',
  263. 'data' => array(
  264. 'red' => 'health',
  265. 'blue' => 'mana'
  266. )
  267. ));
  268. $this->assertTrue(isset($result['params']['url']['output']));
  269. $this->assertTrue(isset($result['params']['url']['red']));
  270. $this->assertTrue(isset($result['params']['url']['blue']));
  271. $this->assertTrue(isset($result['params']['url']['url']));
  272. $result = $this->Case->testAction('/tests_apps_posts/post_var', array(
  273. 'return' => 'vars',
  274. 'method' => 'post',
  275. 'data' => array(
  276. 'name' => 'is jonas',
  277. 'pork' => 'and beans',
  278. )
  279. ));
  280. $this->assertEqual(array_keys($result['data']), array('name', 'pork'));
  281. $db =& ConnectionManager::getDataSource('test_suite');
  282. $_backPrefix = $db->config['prefix'];
  283. $db->config['prefix'] = 'cake_testaction_test_suite_';
  284. $config = $db->config;
  285. $config['prefix'] = 'cake_testcase_test_';
  286. ConnectionManager::create('cake_test_case', $config);
  287. $db =& ConnectionManager::getDataSource('cake_test_case');
  288. $fixture =& new PostFixture($db);
  289. $fixture->create($db);
  290. $fixture->insert($db);
  291. $result = $this->Case->testAction('/tests_apps_posts/fixtured', array(
  292. 'return' => 'vars',
  293. 'fixturize' => true,
  294. 'connection' => 'cake_test_case',
  295. ));
  296. $this->assertTrue(isset($result['posts']));
  297. $this->assertEqual(count($result['posts']), 3);
  298. $tables = $db->listSources(true);
  299. $this->assertFalse(in_array('cake_testaction_test_suite_posts', $tables));
  300. $fixture->drop($db);
  301. $db =& ConnectionManager::getDataSource('test_suite');
  302. $db->config['prefix'] = $_backPrefix;
  303. $fixture->drop($db);
  304. //test that drop tables behaves as exepected with testAction
  305. $db =& ConnectionManager::getDataSource('test_suite');
  306. $_backPrefix = $db->config['prefix'];
  307. $db->config['prefix'] = 'cake_testaction_test_suite_';
  308. $config = $db->config;
  309. $config['prefix'] = 'cake_testcase_test_';
  310. ConnectionManager::create('cake_test_case', $config);
  311. $db =& ConnectionManager::getDataSource('cake_test_case');
  312. $fixture =& new PostFixture($db);
  313. $fixture->create($db);
  314. $fixture->insert($db);
  315. $this->Case->dropTables = false;
  316. $result = $this->Case->testAction('/tests_apps_posts/fixtured', array(
  317. 'return' => 'vars',
  318. 'fixturize' => true,
  319. 'connection' => 'cake_test_case',
  320. ));
  321. $tables = $db->listSources();
  322. $this->assertTrue(in_array('cake_testaction_test_suite_posts', $tables));
  323. $fixture->drop($db);
  324. $db =& ConnectionManager::getDataSource('test_suite');
  325. $db->config['prefix'] = $_backPrefix;
  326. $fixture->drop($db);
  327. Configure::write('modelPaths', $_back['model']);
  328. Configure::write('controllerPaths', $_back['controller']);
  329. Configure::write('viewPaths', $_back['view']);
  330. Configure::write('pluginPaths', $_back['plugin']);
  331. }
  332. /**
  333. * testSkipIf
  334. *
  335. * @return void
  336. **/
  337. function testSkipIf() {
  338. $this->assertTrue($this->Case->skipIf(true));
  339. $this->assertFalse($this->Case->skipIf(false));
  340. }
  341. /**
  342. * testTestDispatcher
  343. *
  344. * @access public
  345. * @return void
  346. */
  347. function testTestDispatcher() {
  348. $_back = array(
  349. 'controller' => Configure::read('controllerPaths'),
  350. 'view' => Configure::read('viewPaths'),
  351. 'plugin' => Configure::read('pluginPaths')
  352. );
  353. Configure::write('controllerPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'controllers' . DS));
  354. Configure::write('viewPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS));
  355. Configure::write('pluginPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS));
  356. $Dispatcher =& new CakeTestDispatcher();
  357. $Case =& new CakeDispatcherMockTestCase();
  358. $Case->expectOnce('startController');
  359. $Case->expectOnce('endController');
  360. $Dispatcher->testCase($Case);
  361. $this->assertTrue(isset($Dispatcher->testCase));
  362. $return = $Dispatcher->dispatch('/tests_apps/index', array('autoRender' => 0, 'return' => 1, 'requested' => 1));
  363. Configure::write('controllerPaths', $_back['controller']);
  364. Configure::write('viewPaths', $_back['view']);
  365. Configure::write('pluginPaths', $_back['plugin']);
  366. }
  367. }
  368. ?>