TestCaseTest.php 12 KB

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