TestCaseTest.php 16 KB

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