TestCaseTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 1.2.0
  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. * testLoadFixturesOnDemand
  134. *
  135. * @return void
  136. */
  137. public function testLoadFixturesOnDemand() {
  138. $this->markTestIncomplete('Cannot work until fixtures are fixed');
  139. $test = new FixturizedTestCase('testFixtureLoadOnDemand');
  140. $test->autoFixtures = false;
  141. $manager = $this->getMock('Cake\TestSuite\Fixture\FixtureManager');
  142. $manager->fixturize($test);
  143. $test->fixtureManager = $manager;
  144. $manager->expects($this->once())->method('loadSingle');
  145. $result = $test->run();
  146. $this->assertEquals(0, $result->errorCount());
  147. }
  148. /**
  149. * testLoadFixturesOnDemand
  150. *
  151. * @return void
  152. */
  153. public function testUnoadFixturesAfterFailure() {
  154. $this->markTestIncomplete('Cannot work until fixtures are fixed');
  155. $test = new FixturizedTestCase('testFixtureLoadOnDemand');
  156. $test->autoFixtures = false;
  157. $manager = $this->getMock('Cake\TestSuite\Fixture\FixtureManager');
  158. $manager->fixturize($test);
  159. $test->fixtureManager = $manager;
  160. $manager->expects($this->once())->method('loadSingle');
  161. $result = $test->run();
  162. $this->assertEquals(0, $result->errorCount());
  163. }
  164. /**
  165. * testThrowException
  166. *
  167. * @return void
  168. */
  169. public function testThrowException() {
  170. $this->markTestIncomplete('Cannot work until fixtures are fixed');
  171. $test = new FixturizedTestCase('testThrowException');
  172. $test->autoFixtures = false;
  173. $manager = $this->getMock('Cake\TestSuite\Fixture\FixtureManager');
  174. $manager->fixturize($test);
  175. $test->fixtureManager = $manager;
  176. $manager->expects($this->once())->method('unload');
  177. $result = $test->run();
  178. $this->assertEquals(1, $result->errorCount());
  179. }
  180. /**
  181. * testSkipIf
  182. *
  183. * @return void
  184. */
  185. public function testSkipIf() {
  186. $this->markTestIncomplete('Cannot work until fixtures are fixed');
  187. $test = new FixturizedTestCase('testSkipIfTrue');
  188. $result = $test->run();
  189. $this->assertEquals(1, $result->skippedCount());
  190. $test = new FixturizedTestCase('testSkipIfFalse');
  191. $result = $test->run();
  192. $this->assertEquals(0, $result->skippedCount());
  193. }
  194. /**
  195. * Test that TestCase::setUp() backs up values.
  196. *
  197. * @return void
  198. */
  199. public function testSetupBackUpValues() {
  200. $this->assertArrayHasKey('debug', $this->_configure);
  201. }
  202. /**
  203. * test assertTextNotEquals()
  204. *
  205. * @return void
  206. */
  207. public function testAssertTextNotEquals() {
  208. $one = "\r\nOne\rTwooo";
  209. $two = "\nOne\nTwo";
  210. $this->assertTextNotEquals($one, $two);
  211. }
  212. /**
  213. * test assertTextEquals()
  214. *
  215. * @return void
  216. */
  217. public function testAssertTextEquals() {
  218. $one = "\r\nOne\rTwo";
  219. $two = "\nOne\nTwo";
  220. $this->assertTextEquals($one, $two);
  221. }
  222. /**
  223. * test assertTextStartsWith()
  224. *
  225. * @return void
  226. */
  227. public function testAssertTextStartsWith() {
  228. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  229. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  230. $this->assertStringStartsWith("some\nstring", $stringDirty);
  231. $this->assertStringStartsNotWith("some\r\nstring\r\nwith", $stringDirty);
  232. $this->assertStringStartsNotWith("some\nstring\nwith", $stringDirty);
  233. $this->assertTextStartsWith("some\nstring\nwith", $stringDirty);
  234. $this->assertTextStartsWith("some\r\nstring\r\nwith", $stringDirty);
  235. }
  236. /**
  237. * test assertTextStartsNotWith()
  238. *
  239. * @return void
  240. */
  241. public function testAssertTextStartsNotWith() {
  242. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  243. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  244. $this->assertTextStartsNotWith("some\nstring\nwithout", $stringDirty);
  245. }
  246. /**
  247. * test assertTextEndsWith()
  248. *
  249. * @return void
  250. */
  251. public function testAssertTextEndsWith() {
  252. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  253. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  254. $this->assertTextEndsWith("string\nwith\r\ndifferent\rline endings!", $stringDirty);
  255. $this->assertTextEndsWith("string\r\nwith\ndifferent\nline endings!", $stringDirty);
  256. }
  257. /**
  258. * test assertTextEndsNotWith()
  259. *
  260. * @return void
  261. */
  262. public function testAssertTextEndsNotWith() {
  263. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  264. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  265. $this->assertStringEndsNotWith("different\nline endings", $stringDirty);
  266. $this->assertTextEndsNotWith("different\rline endings", $stringDirty);
  267. }
  268. /**
  269. * test assertTextContains()
  270. *
  271. * @return void
  272. */
  273. public function testAssertTextContains() {
  274. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  275. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  276. $this->assertContains("different", $stringDirty);
  277. $this->assertNotContains("different\rline", $stringDirty);
  278. $this->assertTextContains("different\rline", $stringDirty);
  279. }
  280. /**
  281. * test assertTextNotContains()
  282. *
  283. * @return void
  284. */
  285. public function testAssertTextNotContains() {
  286. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  287. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  288. $this->assertTextNotContains("different\rlines", $stringDirty);
  289. }
  290. /**
  291. * test getMockForModel()
  292. *
  293. * @return void
  294. */
  295. public function testGetMockForModel() {
  296. Configure::write('App.namespace', 'TestApp');
  297. $Posts = $this->getMockForModel('Posts');
  298. $entity = new \Cake\ORM\Entity(array());
  299. $this->assertInstanceOf('TestApp\Model\Table\PostsTable', $Posts);
  300. $this->assertNull($Posts->save($entity));
  301. $this->assertNull($Posts->table());
  302. $Posts = $this->getMockForModel('Posts', array('save'));
  303. $this->assertNull($Posts->save($entity));
  304. $Posts->expects($this->at(0))
  305. ->method('save')
  306. ->will($this->returnValue(true));
  307. $Posts->expects($this->at(1))
  308. ->method('save')
  309. ->will($this->returnValue(false));
  310. $this->assertTrue($Posts->save($entity));
  311. $this->assertFalse($Posts->save($entity));
  312. }
  313. /**
  314. * test getMockForModel() with plugin models
  315. *
  316. * @return void
  317. */
  318. public function testGetMockForModelWithPlugin() {
  319. Configure::write('App.namespace', 'TestApp');
  320. Plugin::load('TestPlugin');
  321. $TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComments');
  322. $result = TableRegistry::get('TestPlugin.TestPluginComments');
  323. $this->assertInstanceOf('\TestPlugin\Model\Table\TestPluginCommentsTable', $result);
  324. $TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComments', array('save'));
  325. $this->assertInstanceOf('\TestPlugin\Model\Table\TestPluginCommentsTable', $TestPluginComment);
  326. $TestPluginComment->expects($this->at(0))
  327. ->method('save')
  328. ->will($this->returnValue(true));
  329. $TestPluginComment->expects($this->at(1))
  330. ->method('save')
  331. ->will($this->returnValue(false));
  332. $entity = new \Cake\ORM\Entity(array());
  333. $this->assertTrue($TestPluginComment->save($entity));
  334. $this->assertFalse($TestPluginComment->save($entity));
  335. }
  336. /**
  337. * testGetMockForModelTable
  338. *
  339. * @return void
  340. */
  341. public function testGetMockForModelTable() {
  342. $Mock = $this->getMockForModel(
  343. 'Table',
  344. array('save'),
  345. array('alias' => 'Comments', 'className' => '\Cake\ORM\Table')
  346. );
  347. $result = TableRegistry::get('Comments');
  348. $this->assertInstanceOf('Cake\ORM\Table', $result);
  349. $this->assertEquals('Comments', $Mock->alias());
  350. $Mock->expects($this->at(0))
  351. ->method('save')
  352. ->will($this->returnValue(true));
  353. $Mock->expects($this->at(1))
  354. ->method('save')
  355. ->will($this->returnValue(false));
  356. $entity = new \Cake\ORM\Entity(array());
  357. $this->assertTrue($Mock->save($entity));
  358. $this->assertFalse($Mock->save($entity));
  359. }
  360. }