TestCaseTest.php 16 KB

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