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

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

http://skygames.googlecode.com/
PHP | 375 lines | 227 code | 38 blank | 110 comment | 0 complexity | 073c966f3ad6026fd3325c34ee9bfbb1 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, CC-BY-SA-3.0
  1. <?php
  2. /* SVN FILE: $Id: cake_test_case.test.php 7805 2008-10-30 17:30:26Z AD7six $ */
  3. /**
  4. * CakeTestCase TestCase
  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: 7805 $
  23. * @modifiedby $LastChangedBy: AD7six $
  24. * @lastmodified $Date: 2008-10-30 12:30:26 -0500 (Thu, 30 Oct 2008) $
  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.tests
  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. function testDummy() {
  52. }
  53. }
  54. /**
  55. * CakeTestCaseTest
  56. *
  57. * @package cake.tests
  58. * @subpackage cake.tests.cases.libs
  59. **/
  60. class CakeTestCaseTest extends CakeTestCase {
  61. /**
  62. * setUp
  63. *
  64. * @access public
  65. * @return void
  66. */
  67. function setUp() {
  68. $this->Case =& new SubjectCakeTestCase();
  69. $reporter =& new MockCakeHtmlReporter();
  70. $this->Case->setReporter($reporter);
  71. $this->Reporter = $reporter;
  72. }
  73. /**
  74. * testAssertGoodTags
  75. *
  76. * @access public
  77. * @return void
  78. */
  79. function testAssertGoodTags() {
  80. $this->Reporter->expectAtLeastOnce('paintPass');
  81. $this->Reporter->expectNever('paintFail');
  82. $input = '<p>Text</p>';
  83. $pattern = array(
  84. '<p',
  85. 'Text',
  86. '/p',
  87. );
  88. $this->assertTrue($this->Case->assertTags($input, $pattern));
  89. $input = '<a href="/test.html" class="active">My link</a>';
  90. $pattern = array(
  91. 'a' => array('href' => '/test.html', 'class' => 'active'),
  92. 'My link',
  93. '/a'
  94. );
  95. $this->assertTrue($this->Case->assertTags($input, $pattern));
  96. $pattern = array(
  97. 'a' => array('class' => 'active', 'href' => '/test.html'),
  98. 'My link',
  99. '/a'
  100. );
  101. $this->assertTrue($this->Case->assertTags($input, $pattern));
  102. $input = "<a href=\"/test.html\"\t\n\tclass=\"active\"\tid=\"primary\">\t<span>My link</span></a>";
  103. $pattern = array(
  104. 'a' => array('id' => 'primary', 'href' => '/test.html', 'class' => 'active'),
  105. '<span',
  106. 'My link',
  107. '/span',
  108. '/a'
  109. );
  110. $this->assertTrue($this->Case->assertTags($input, $pattern));
  111. $input = '<p class="info"><a href="/test.html" class="active"><strong onClick="alert(\'hey\');">My link</strong></a></p>';
  112. $pattern = array(
  113. 'p' => array('class' => 'info'),
  114. 'a' => array('class' => 'active', 'href' => '/test.html' ),
  115. 'strong' => array('onClick' => 'alert(\'hey\');'),
  116. 'My link',
  117. '/strong',
  118. '/a',
  119. '/p'
  120. );
  121. $this->assertTrue($this->Case->assertTags($input, $pattern));
  122. }
  123. /**
  124. * testBadAssertTags
  125. *
  126. * @access public
  127. * @return void
  128. */
  129. function testBadAssertTags() {
  130. $this->Reporter->expectAtLeastOnce('paintFail');
  131. $this->Reporter->expectNever('paintPass');
  132. $input = '<a href="/test.html" class="active">My link</a>';
  133. $pattern = array(
  134. 'a' => array('hRef' => '/test.html', 'clAss' => 'active'),
  135. 'My link',
  136. '/a'
  137. );
  138. $this->assertFalse($this->Case->assertTags($input, $pattern));
  139. $input = '<a href="/test.html" class="active">My link</a>';
  140. $pattern = array(
  141. '<a' => array('href' => '/test.html', 'class' => 'active'),
  142. 'My link',
  143. '/a'
  144. );
  145. $this->assertFalse($this->Case->assertTags($input, $pattern));
  146. }
  147. /**
  148. * testBefore
  149. *
  150. * @access public
  151. * @return void
  152. */
  153. function testBefore() {
  154. $this->Case->before('testDummy');
  155. $this->assertFalse(isset($this->Case->db));
  156. $this->Case->fixtures = array('core.post');
  157. $this->Case->before('start');
  158. $this->assertTrue(isset($this->Case->db));
  159. $this->assertTrue(isset($this->Case->_fixtures['core.post']));
  160. $this->assertTrue(is_a($this->Case->_fixtures['core.post'], 'CakeTestFixture'));
  161. $this->assertEqual($this->Case->_fixtureClassMap['Post'], 'core.post');
  162. }
  163. /**
  164. * testAfter
  165. *
  166. * @access public
  167. * @return void
  168. */
  169. function testAfter() {
  170. $this->Case->after('testDummy');
  171. $this->assertFalse($this->Case->__truncated);
  172. $this->Case->fixtures = array('core.post');
  173. $this->Case->before('start');
  174. $this->Case->start();
  175. $this->Case->after('testDummy');
  176. $this->assertTrue($this->Case->__truncated);
  177. }
  178. /**
  179. * testLoadFixtures
  180. *
  181. * @access public
  182. * @return void
  183. */
  184. function testLoadFixtures() {
  185. $this->Case->fixtures = array('core.post');
  186. $this->Case->autoFixtures = false;
  187. $this->Case->before('start');
  188. $this->expectError();
  189. $this->Case->loadFixtures('Wrong!');
  190. }
  191. /**
  192. * testGetTests Method
  193. *
  194. * @return void
  195. * @access public
  196. */
  197. function testGetTests() {
  198. $result = $this->Case->getTests();
  199. $this->assertEqual(array_slice($result, 0, 2), array('start', 'startCase'));
  200. $this->assertEqual(array_slice($result, -2), array('endCase', 'end'));
  201. }
  202. /**
  203. * TestTestAction
  204. *
  205. * @access public
  206. * @return void
  207. **/
  208. function testTestAction() {
  209. $_back = array(
  210. 'controller' => Configure::read('controllerPaths'),
  211. 'view' => Configure::read('viewPaths'),
  212. 'model' => Configure::read('modelPaths'),
  213. 'plugin' => Configure::read('pluginPaths')
  214. );
  215. Configure::write('controllerPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'controllers' . DS));
  216. Configure::write('viewPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS));
  217. Configure::write('modelPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'models' . DS));
  218. Configure::write('pluginPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS));
  219. $result = $this->Case->testAction('/tests_apps/index', array('return' => 'view'));
  220. $this->assertPattern('/This is the TestsAppsController index view/', $result);
  221. $result = $this->Case->testAction('/tests_apps/index', array('return' => 'contents'));
  222. $this->assertPattern('/This is the TestsAppsController index view/', $result);
  223. $this->assertPattern('/<html/', $result);
  224. $this->assertPattern('/<\/html>/', $result);
  225. $result = $this->Case->testAction('/tests_apps/some_method', array('return' => 'result'));
  226. $this->assertEqual($result, 5);
  227. $result = $this->Case->testAction('/tests_apps/set_action', array('return' => 'vars'));
  228. $this->assertEqual($result, array('var' => 'string'));
  229. $result = $this->Case->testAction('/tests_apps_posts/add', array('return' => 'vars'));
  230. $this->assertTrue(array_key_exists('posts', $result));
  231. $this->assertEqual(count($result['posts']), 1);
  232. $result = $this->Case->testAction('/tests_apps_posts/url_var/var1:value1/var2:val2', array(
  233. 'return' => 'vars',
  234. 'method' => 'get',
  235. ));
  236. $this->assertTrue(isset($result['params']['url']['url']));
  237. $this->assertTrue(isset($result['params']['url']['output']));
  238. $this->assertEqual(array_keys($result['params']['named']), array('var1', 'var2'));
  239. $result = $this->Case->testAction('/tests_apps_posts/url_var/gogo/val2', array(
  240. 'return' => 'vars',
  241. 'method' => 'get',
  242. ));
  243. $this->assertEqual($result['params']['pass'], array('gogo', 'val2'));
  244. $result = $this->Case->testAction('/tests_apps_posts/url_var', array(
  245. 'return' => 'vars',
  246. 'method' => 'get',
  247. 'data' => array(
  248. 'red' => 'health',
  249. 'blue' => 'mana'
  250. )
  251. ));
  252. $this->assertTrue(isset($result['params']['url']['output']));
  253. $this->assertTrue(isset($result['params']['url']['red']));
  254. $this->assertTrue(isset($result['params']['url']['blue']));
  255. $this->assertTrue(isset($result['params']['url']['url']));
  256. $result = $this->Case->testAction('/tests_apps_posts/post_var', array(
  257. 'return' => 'vars',
  258. 'method' => 'post',
  259. 'data' => array(
  260. 'name' => 'is jonas',
  261. 'pork' => 'and beans',
  262. )
  263. ));
  264. $this->assertEqual(array_keys($result['data']), array('name', 'pork'));
  265. $db =& ConnectionManager::getDataSource('test_suite');
  266. $_backPrefix = $db->config['prefix'];
  267. $db->config['prefix'] = 'cake_testaction_test_suite_';
  268. $config = $db->config;
  269. $config['prefix'] = 'cake_testcase_test_';
  270. ConnectionManager::create('cake_test_case', $config);
  271. $db =& ConnectionManager::getDataSource('cake_test_case');
  272. $fixture =& new PostFixture($db);
  273. $fixture->create($db);
  274. $fixture->insert($db);
  275. $result = $this->Case->testAction('/tests_apps_posts/fixtured', array(
  276. 'return' => 'vars',
  277. 'fixturize' => true,
  278. 'connection' => 'cake_test_case',
  279. ));
  280. $this->assertTrue(isset($result['posts']));
  281. $this->assertEqual(count($result['posts']), 3);
  282. $fixture->drop($db);
  283. $db =& ConnectionManager::getDataSource('test_suite');
  284. $db->config['prefix'] = $_backPrefix;
  285. $fixture->drop($db);
  286. Configure::write('modelPaths', $_back['model']);
  287. Configure::write('controllerPaths', $_back['controller']);
  288. Configure::write('viewPaths', $_back['view']);
  289. Configure::write('pluginPaths', $_back['plugin']);
  290. }
  291. /**
  292. * testSkipIf
  293. *
  294. * @return void
  295. **/
  296. function testSkipIf() {
  297. $this->assertTrue($this->Case->skipIf(true));
  298. $this->assertFalse($this->Case->skipIf(false));
  299. }
  300. /**
  301. * testTestDispatcher
  302. *
  303. * @access public
  304. * @return void
  305. */
  306. function testTestDispatcher() {
  307. $_back = array(
  308. 'controller' => Configure::read('controllerPaths'),
  309. 'view' => Configure::read('viewPaths'),
  310. 'plugin' => Configure::read('pluginPaths')
  311. );
  312. Configure::write('controllerPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'controllers' . DS));
  313. Configure::write('viewPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS));
  314. Configure::write('pluginPaths', array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS));
  315. $Dispatcher =& new CakeTestDispatcher();
  316. $Case =& new CakeDispatcherMockTestCase();
  317. $Case->expectOnce('startController');
  318. $Case->expectOnce('endController');
  319. $Dispatcher->testCase($Case);
  320. $this->assertTrue(isset($Dispatcher->testCase));
  321. $return = $Dispatcher->dispatch('/tests_apps/index', array('autoRender' => 0, 'return' => 1, 'requested' => 1));
  322. Configure::write('controllerPaths', $_back['controller']);
  323. Configure::write('viewPaths', $_back['view']);
  324. Configure::write('pluginPaths', $_back['plugin']);
  325. }
  326. /**
  327. * tearDown
  328. *
  329. * @access public
  330. * @return void
  331. */
  332. function tearDown() {
  333. unset($this->Case);
  334. unset($this->Reporter);
  335. }
  336. }
  337. ?>