TestCaseTest.php 11 KB

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