CakeTestCaseTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. /**
  3. * CakeTestCaseTest file
  4. *
  5. * Test Case for CakeTestCase class
  6. *
  7. * PHP version 5
  8. *
  9. * CakePHP : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2012, 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 2005-2012, Cake Software Foundation, Inc.
  16. * @link http://cakephp.org CakePHP Project
  17. * @package Cake.Test.Case.TestSuite
  18. * @since CakePHP v 1.2.0.4487
  19. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  20. */
  21. App::uses('Controller', 'Controller');
  22. App::uses('CakeHtmlReporter', 'TestSuite/Reporter');
  23. /**
  24. * CakeTestCaseTest
  25. *
  26. * @package Cake.Test.Case.TestSuite
  27. */
  28. class CakeTestCaseTest extends CakeTestCase {
  29. /**
  30. * fixtures property
  31. *
  32. * @var array
  33. */
  34. public $fixtures = array('core.post', 'core.author', 'core.test_plugin_comment');
  35. public static function setUpBeforeClass() {
  36. require_once CAKE . 'Test' . DS . 'Fixture' . DS . 'AssertTagsTestCase.php';
  37. require_once CAKE . 'Test' . DS . 'Fixture' . DS . 'FixturizedTestCase.php';
  38. }
  39. /**
  40. * setUp
  41. *
  42. * @return void
  43. */
  44. public function setUp() {
  45. parent::setUp();
  46. $this->Reporter = $this->getMock('CakeHtmlReporter');
  47. }
  48. /**
  49. * tearDown
  50. *
  51. * @return void
  52. */
  53. public function tearDown() {
  54. parent::tearDown();
  55. unset($this->Result);
  56. unset($this->Reporter);
  57. }
  58. /**
  59. * testAssertGoodTags
  60. *
  61. * @return void
  62. */
  63. public function testAssertTagsQuotes() {
  64. $test = new AssertTagsTestCase('testAssertTagsQuotes');
  65. $result = $test->run();
  66. $this->assertEquals(0, $result->errorCount());
  67. $this->assertTrue($result->wasSuccessful());
  68. $this->assertEquals(0, $result->failureCount());
  69. $input = '<a href="/test.html" class="active">My link</a>';
  70. $pattern = array(
  71. 'a' => array('href' => '/test.html', 'class' => 'active'),
  72. 'My link',
  73. '/a'
  74. );
  75. $this->assertTrue($test->assertTags($input, $pattern), 'Double quoted attributes %s');
  76. $input = "<a href='/test.html' class='active'>My link</a>";
  77. $pattern = array(
  78. 'a' => array('href' => '/test.html', 'class' => 'active'),
  79. 'My link',
  80. '/a'
  81. );
  82. $this->assertTrue($test->assertTags($input, $pattern), 'Single quoted attributes %s');
  83. $input = "<a href='/test.html' class='active'>My link</a>";
  84. $pattern = array(
  85. 'a' => array('href' => 'preg:/.*\.html/', 'class' => 'active'),
  86. 'My link',
  87. '/a'
  88. );
  89. $this->assertTrue($test->assertTags($input, $pattern), 'Single quoted attributes %s');
  90. $input = "<span><strong>Text</strong></span>";
  91. $pattern = array(
  92. '<span',
  93. '<strong',
  94. 'Text',
  95. '/strong',
  96. '/span'
  97. );
  98. $this->assertTrue($test->assertTags($input, $pattern), 'Tags with no attributes');
  99. $input = "<span class='active'><strong>Text</strong></span>";
  100. $pattern = array(
  101. 'span' => array('class'),
  102. '<strong',
  103. 'Text',
  104. '/strong',
  105. '/span'
  106. );
  107. $this->assertTrue($test->assertTags($input, $pattern), 'Test attribute presence');
  108. }
  109. /**
  110. * testNumericValuesInExpectationForAssertTags
  111. *
  112. * @return void
  113. */
  114. public function testNumericValuesInExpectationForAssertTags() {
  115. $test = new AssertTagsTestCase('testNumericValuesInExpectationForAssertTags');
  116. $result = $test->run();
  117. $this->assertEquals(0, $result->errorCount());
  118. $this->assertTrue($result->wasSuccessful());
  119. $this->assertEquals(0, $result->failureCount());
  120. }
  121. /**
  122. * testBadAssertTags
  123. *
  124. * @return void
  125. */
  126. public function testBadAssertTags() {
  127. $test = new AssertTagsTestCase('testBadAssertTags');
  128. $result = $test->run();
  129. $this->assertEquals(0, $result->errorCount());
  130. $this->assertFalse($result->wasSuccessful());
  131. $this->assertEquals(1, $result->failureCount());
  132. $test = new AssertTagsTestCase('testBadAssertTags2');
  133. $result = $test->run();
  134. $this->assertEquals(0, $result->errorCount());
  135. $this->assertFalse($result->wasSuccessful());
  136. $this->assertEquals(1, $result->failureCount());
  137. }
  138. /**
  139. * testLoadFixtures
  140. *
  141. * @return void
  142. */
  143. public function testLoadFixtures() {
  144. $test = new FixturizedTestCase('testFixturePresent');
  145. $manager = $this->getMock('CakeFixtureManager');
  146. $manager->fixturize($test);
  147. $test->fixtureManager = $manager;
  148. $manager->expects($this->once())->method('load');
  149. $manager->expects($this->once())->method('unload');
  150. $result = $test->run();
  151. $this->assertEquals(0, $result->errorCount());
  152. $this->assertTrue($result->wasSuccessful());
  153. $this->assertEquals(0, $result->failureCount());
  154. }
  155. /**
  156. * testLoadFixturesOnDemand
  157. *
  158. * @return void
  159. */
  160. public function testLoadFixturesOnDemand() {
  161. $test = new FixturizedTestCase('testFixtureLoadOnDemand');
  162. $test->autoFixtures = false;
  163. $manager = $this->getMock('CakeFixtureManager');
  164. $manager->fixturize($test);
  165. $test->fixtureManager = $manager;
  166. $manager->expects($this->once())->method('loadSingle');
  167. $result = $test->run();
  168. $this->assertEquals(0, $result->errorCount());
  169. }
  170. /**
  171. * testLoadFixturesOnDemand
  172. *
  173. * @return void
  174. */
  175. public function testUnoadFixturesAfterFailure() {
  176. $test = new FixturizedTestCase('testFixtureLoadOnDemand');
  177. $test->autoFixtures = false;
  178. $manager = $this->getMock('CakeFixtureManager');
  179. $manager->fixturize($test);
  180. $test->fixtureManager = $manager;
  181. $manager->expects($this->once())->method('loadSingle');
  182. $result = $test->run();
  183. $this->assertEquals(0, $result->errorCount());
  184. }
  185. /**
  186. * testThrowException
  187. *
  188. * @return void
  189. */
  190. public function testThrowException() {
  191. $test = new FixturizedTestCase('testThrowException');
  192. $test->autoFixtures = false;
  193. $manager = $this->getMock('CakeFixtureManager');
  194. $manager->fixturize($test);
  195. $test->fixtureManager = $manager;
  196. $manager->expects($this->once())->method('unload');
  197. $result = $test->run();
  198. $this->assertEquals(1, $result->errorCount());
  199. }
  200. /**
  201. * testSkipIf
  202. *
  203. * @return void
  204. */
  205. public function testSkipIf() {
  206. $test = new FixturizedTestCase('testSkipIfTrue');
  207. $result = $test->run();
  208. $this->assertEquals(1, $result->skippedCount());
  209. $test = new FixturizedTestCase('testSkipIfFalse');
  210. $result = $test->run();
  211. $this->assertEquals(0, $result->skippedCount());
  212. }
  213. /**
  214. * Test that CakeTestCase::setUp() backs up values.
  215. *
  216. * @return void
  217. */
  218. public function testSetupBackUpValues() {
  219. $this->assertArrayHasKey('debug', $this->_configure);
  220. $this->assertArrayHasKey('Plugin', $this->_pathRestore);
  221. }
  222. /**
  223. * test assertTextNotEquals()
  224. *
  225. * @return void
  226. */
  227. public function testAssertTextNotEquals() {
  228. $one = "\r\nOne\rTwooo";
  229. $two = "\nOne\nTwo";
  230. $this->assertTextNotEquals($one, $two);
  231. }
  232. /**
  233. * test assertTextEquals()
  234. *
  235. * @return void
  236. */
  237. public function testAssertTextEquals() {
  238. $one = "\r\nOne\rTwo";
  239. $two = "\nOne\nTwo";
  240. $this->assertTextEquals($one, $two);
  241. }
  242. /**
  243. * test assertTextStartsWith()
  244. *
  245. * @return void
  246. */
  247. public function testAssertTextStartsWith() {
  248. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  249. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  250. $this->assertStringStartsWith("some\nstring", $stringDirty);
  251. $this->assertStringStartsNotWith("some\r\nstring\r\nwith", $stringDirty);
  252. $this->assertStringStartsNotWith("some\nstring\nwith", $stringDirty);
  253. $this->assertTextStartsWith("some\nstring\nwith", $stringDirty);
  254. $this->assertTextStartsWith("some\r\nstring\r\nwith", $stringDirty);
  255. }
  256. /**
  257. * test assertTextStartsNotWith()
  258. *
  259. * @return void
  260. */
  261. public function testAssertTextStartsNotWith() {
  262. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  263. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  264. $this->assertTextStartsNotWith("some\nstring\nwithout", $stringDirty);
  265. }
  266. /**
  267. * test assertTextEndsWith()
  268. *
  269. * @return void
  270. */
  271. public function testAssertTextEndsWith() {
  272. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  273. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  274. $this->assertTextEndsWith("string\nwith\r\ndifferent\rline endings!", $stringDirty);
  275. $this->assertTextEndsWith("string\r\nwith\ndifferent\nline endings!", $stringDirty);
  276. }
  277. /**
  278. * test assertTextEndsNotWith()
  279. *
  280. * @return void
  281. */
  282. public function testAssertTextEndsNotWith() {
  283. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  284. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  285. $this->assertStringEndsNotWith("different\nline endings", $stringDirty);
  286. $this->assertTextEndsNotWith("different\rline endings", $stringDirty);
  287. }
  288. /**
  289. * test assertTextContains()
  290. *
  291. * @return void
  292. */
  293. public function testAssertTextContains() {
  294. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  295. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  296. $this->assertContains("different", $stringDirty);
  297. $this->assertNotContains("different\rline", $stringDirty);
  298. $this->assertTextContains("different\rline", $stringDirty);
  299. }
  300. /**
  301. * test assertTextNotContains()
  302. *
  303. * @return void
  304. */
  305. public function testAssertTextNotContains() {
  306. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  307. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  308. $this->assertTextNotContains("different\rlines", $stringDirty);
  309. }
  310. /**
  311. * test getMockForModel()
  312. *
  313. * @return void
  314. */
  315. public function testGetMockForModel() {
  316. $Post = $this->getMockForModel('Post');
  317. $this->assertInstanceOf('Post', $Post);
  318. $this->assertNull($Post->save(array()));
  319. $this->assertNull($Post->find('all'));
  320. $this->assertEquals('posts', $Post->useTable);
  321. $Post = $this->getMockForModel('Post', array('save'));
  322. $this->assertNull($Post->save(array()));
  323. $this->assertInternalType('array', $Post->find('all'));
  324. }
  325. /**
  326. * test getMockForModel() with plugin models
  327. *
  328. * @return void
  329. */
  330. public function testGetMockForModelWithPlugin() {
  331. $TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComment');
  332. $result = ClassRegistry::init('TestPlugin.TestPluginComment');
  333. $this->assertInstanceOf('TestPluginComment', $result);
  334. $TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComment', array('save'));
  335. $this->assertInstanceOf('TestPluginComment', $TestPluginComment);
  336. $TestPluginComment->expects($this->at(0))
  337. ->method('save')
  338. ->will($this->returnValue(true));
  339. $TestPluginComment->expects($this->at(1))
  340. ->method('save')
  341. ->will($this->returnValue(false));
  342. $this->assertTrue($TestPluginComment->save(array()));
  343. $this->assertFalse($TestPluginComment->save(array()));
  344. }
  345. }