TestCaseTest.php 10 KB

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