PageRenderTime 85ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

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