TestCaseTest.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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\AssertHtmlTestCase;
  23. use Cake\Test\Fixture\FixturizedTestCase;
  24. /**
  25. * TestCaseTest
  26. *
  27. */
  28. class TestCaseTest extends TestCase {
  29. /**
  30. * testAssertHtml
  31. *
  32. * @return void
  33. */
  34. public function testAssertHtmlBasic() {
  35. $test = new AssertHtmlTestCase('testAssertHtmlQuotes');
  36. $result = $test->run();
  37. $this->assertEquals(0, $result->errorCount());
  38. $this->assertTrue($result->wasSuccessful());
  39. $this->assertEquals(0, $result->failureCount());
  40. }
  41. /**
  42. * test assertHtml works with single and double quotes
  43. *
  44. * @return void
  45. */
  46. public function testAssertHtmlQuoting() {
  47. $input = '<a href="/test.html" class="active">My link</a>';
  48. $pattern = array(
  49. 'a' => array('href' => '/test.html', 'class' => 'active'),
  50. 'My link',
  51. '/a'
  52. );
  53. $this->assertHtml($pattern, $input);
  54. $input = "<a href='/test.html' class='active'>My link</a>";
  55. $pattern = array(
  56. 'a' => array('href' => '/test.html', 'class' => 'active'),
  57. 'My link',
  58. '/a'
  59. );
  60. $this->assertHtml($pattern, $input);
  61. $input = "<a href='/test.html' class='active'>My link</a>";
  62. $pattern = array(
  63. 'a' => array('href' => 'preg:/.*\.html/', 'class' => 'active'),
  64. 'My link',
  65. '/a'
  66. );
  67. $this->assertHtml($pattern, $input);
  68. $input = "<span><strong>Text</strong></span>";
  69. $pattern = array(
  70. '<span',
  71. '<strong',
  72. 'Text',
  73. '/strong',
  74. '/span'
  75. );
  76. $this->assertHtml($pattern, $input);
  77. $input = "<span class='active'><strong>Text</strong></span>";
  78. $pattern = array(
  79. 'span' => array('class'),
  80. '<strong',
  81. 'Text',
  82. '/strong',
  83. '/span'
  84. );
  85. $this->assertHtml($pattern, $input);
  86. }
  87. /**
  88. * Test that assertHtml runs quickly.
  89. *
  90. * @return void
  91. */
  92. public function testAssertHtmlRuntimeComplexity() {
  93. $pattern = array(
  94. 'div' => array(
  95. 'attr1' => 'val1',
  96. 'attr2' => 'val2',
  97. 'attr3' => 'val3',
  98. 'attr4' => 'val4',
  99. 'attr5' => 'val5',
  100. 'attr6' => 'val6',
  101. 'attr7' => 'val7',
  102. 'attr8' => 'val8',
  103. ),
  104. 'My div',
  105. '/div'
  106. );
  107. $input = '<div attr8="val8" attr6="val6" attr4="val4" attr2="val2"' .
  108. ' attr1="val1" attr3="val3" attr5="val5" attr7="val7" />' .
  109. 'My div' .
  110. '</div>';
  111. $this->assertHtml($pattern, $input);
  112. }
  113. /**
  114. * testNumericValuesInExpectationForAssertHtml
  115. *
  116. * @return void
  117. */
  118. public function testNumericValuesInExpectationForAssertHtml() {
  119. $test = new AssertHtmlTestCase('testNumericValuesInExpectationForAssertHtml');
  120. $result = $test->run();
  121. $this->assertEquals(0, $result->errorCount());
  122. $this->assertTrue($result->wasSuccessful());
  123. $this->assertEquals(0, $result->failureCount());
  124. }
  125. /**
  126. * testBadAssertHtml
  127. *
  128. * @return void
  129. */
  130. public function testBadAssertHtml() {
  131. $test = new AssertHtmlTestCase('testBadAssertHtml');
  132. $result = $test->run();
  133. $this->assertEquals(0, $result->errorCount());
  134. $this->assertFalse($result->wasSuccessful());
  135. $this->assertEquals(1, $result->failureCount());
  136. $test = new AssertHtmlTestCase('testBadAssertHtml2');
  137. $result = $test->run();
  138. $this->assertEquals(0, $result->errorCount());
  139. $this->assertFalse($result->wasSuccessful());
  140. $this->assertEquals(1, $result->failureCount());
  141. }
  142. /**
  143. * testLoadFixturesOnDemand
  144. *
  145. * @return void
  146. */
  147. public function testLoadFixturesOnDemand() {
  148. $test = new FixturizedTestCase('testFixtureLoadOnDemand');
  149. $test->autoFixtures = false;
  150. $manager = $this->getMock('Cake\TestSuite\Fixture\FixtureManager');
  151. $manager->fixturize($test);
  152. $test->fixtureManager = $manager;
  153. $manager->expects($this->once())->method('loadSingle');
  154. $result = $test->run();
  155. $this->assertEquals(0, $result->errorCount());
  156. }
  157. /**
  158. * testSkipIf
  159. *
  160. * @return void
  161. */
  162. public function testSkipIf() {
  163. $test = new FixturizedTestCase('testSkipIfTrue');
  164. $result = $test->run();
  165. $this->assertEquals(1, $result->skippedCount());
  166. $test = new FixturizedTestCase('testSkipIfFalse');
  167. $result = $test->run();
  168. $this->assertEquals(0, $result->skippedCount());
  169. }
  170. /**
  171. * Test that TestCase::setUp() backs up values.
  172. *
  173. * @return void
  174. */
  175. public function testSetupBackUpValues() {
  176. $this->assertArrayHasKey('debug', $this->_configure);
  177. }
  178. /**
  179. * test assertTextNotEquals()
  180. *
  181. * @return void
  182. */
  183. public function testAssertTextNotEquals() {
  184. $one = "\r\nOne\rTwooo";
  185. $two = "\nOne\nTwo";
  186. $this->assertTextNotEquals($one, $two);
  187. }
  188. /**
  189. * test assertTextEquals()
  190. *
  191. * @return void
  192. */
  193. public function testAssertTextEquals() {
  194. $one = "\r\nOne\rTwo";
  195. $two = "\nOne\nTwo";
  196. $this->assertTextEquals($one, $two);
  197. }
  198. /**
  199. * test assertTextStartsWith()
  200. *
  201. * @return void
  202. */
  203. public function testAssertTextStartsWith() {
  204. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  205. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  206. $this->assertStringStartsWith("some\nstring", $stringDirty);
  207. $this->assertStringStartsNotWith("some\r\nstring\r\nwith", $stringDirty);
  208. $this->assertStringStartsNotWith("some\nstring\nwith", $stringDirty);
  209. $this->assertTextStartsWith("some\nstring\nwith", $stringDirty);
  210. $this->assertTextStartsWith("some\r\nstring\r\nwith", $stringDirty);
  211. }
  212. /**
  213. * test assertTextStartsNotWith()
  214. *
  215. * @return void
  216. */
  217. public function testAssertTextStartsNotWith() {
  218. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  219. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  220. $this->assertTextStartsNotWith("some\nstring\nwithout", $stringDirty);
  221. }
  222. /**
  223. * test assertTextEndsWith()
  224. *
  225. * @return void
  226. */
  227. public function testAssertTextEndsWith() {
  228. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  229. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  230. $this->assertTextEndsWith("string\nwith\r\ndifferent\rline endings!", $stringDirty);
  231. $this->assertTextEndsWith("string\r\nwith\ndifferent\nline endings!", $stringDirty);
  232. }
  233. /**
  234. * test assertTextEndsNotWith()
  235. *
  236. * @return void
  237. */
  238. public function testAssertTextEndsNotWith() {
  239. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  240. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  241. $this->assertStringEndsNotWith("different\nline endings", $stringDirty);
  242. $this->assertTextEndsNotWith("different\rline endings", $stringDirty);
  243. }
  244. /**
  245. * test assertTextContains()
  246. *
  247. * @return void
  248. */
  249. public function testAssertTextContains() {
  250. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  251. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  252. $this->assertContains("different", $stringDirty);
  253. $this->assertNotContains("different\rline", $stringDirty);
  254. $this->assertTextContains("different\rline", $stringDirty);
  255. }
  256. /**
  257. * test assertTextNotContains()
  258. *
  259. * @return void
  260. */
  261. public function testAssertTextNotContains() {
  262. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  263. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  264. $this->assertTextNotContains("different\rlines", $stringDirty);
  265. }
  266. /**
  267. * test getMockForModel()
  268. *
  269. * @return void
  270. */
  271. public function testGetMockForModel() {
  272. Configure::write('App.namespace', 'TestApp');
  273. $Posts = $this->getMockForModel('Posts');
  274. $entity = new \Cake\ORM\Entity(array());
  275. $this->assertInstanceOf('TestApp\Model\Table\PostsTable', $Posts);
  276. $this->assertNull($Posts->save($entity));
  277. $this->assertNull($Posts->table());
  278. $Posts = $this->getMockForModel('Posts', array('save'));
  279. $Posts->expects($this->at(0))
  280. ->method('save')
  281. ->will($this->returnValue('mocked'));
  282. $this->assertEquals('mocked', $Posts->save($entity));
  283. $Posts = $this->getMockForModel('Posts', ['doSomething']);
  284. $this->assertInstanceOf('Cake\Database\Connection', $Posts->connection());
  285. $this->assertEquals('test', $Posts->connection()->configName());
  286. }
  287. /**
  288. * test getMockForModel() with plugin models
  289. *
  290. * @return void
  291. */
  292. public function testGetMockForModelWithPlugin() {
  293. Configure::write('App.namespace', 'TestApp');
  294. Plugin::load('TestPlugin');
  295. $TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComments');
  296. $result = TableRegistry::get('TestPlugin.TestPluginComments');
  297. $this->assertInstanceOf('\TestPlugin\Model\Table\TestPluginCommentsTable', $result);
  298. $TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComments', array('save'));
  299. $this->assertInstanceOf('\TestPlugin\Model\Table\TestPluginCommentsTable', $TestPluginComment);
  300. $TestPluginComment->expects($this->at(0))
  301. ->method('save')
  302. ->will($this->returnValue(true));
  303. $TestPluginComment->expects($this->at(1))
  304. ->method('save')
  305. ->will($this->returnValue(false));
  306. $entity = new \Cake\ORM\Entity(array());
  307. $this->assertTrue($TestPluginComment->save($entity));
  308. $this->assertFalse($TestPluginComment->save($entity));
  309. }
  310. /**
  311. * testGetMockForModelTable
  312. *
  313. * @return void
  314. */
  315. public function testGetMockForModelTable() {
  316. $Mock = $this->getMockForModel(
  317. 'Table',
  318. array('save'),
  319. array('alias' => 'Comments', 'className' => '\Cake\ORM\Table')
  320. );
  321. $result = TableRegistry::get('Comments');
  322. $this->assertInstanceOf('Cake\ORM\Table', $result);
  323. $this->assertEquals('Comments', $Mock->alias());
  324. $Mock->expects($this->at(0))
  325. ->method('save')
  326. ->will($this->returnValue(true));
  327. $Mock->expects($this->at(1))
  328. ->method('save')
  329. ->will($this->returnValue(false));
  330. $entity = new \Cake\ORM\Entity(array());
  331. $this->assertTrue($Mock->save($entity));
  332. $this->assertFalse($Mock->save($entity));
  333. }
  334. }