TestCaseTest.php 13 KB

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