PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

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