TestCaseTest.php 13 KB

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