TestCaseTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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\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. * testLoadFixturesOnDemand
  111. *
  112. * @return void
  113. */
  114. public function testLoadFixturesOnDemand()
  115. {
  116. $test = new FixturizedTestCase('testFixtureLoadOnDemand');
  117. $test->autoFixtures = false;
  118. $manager = $this->getMockBuilder('Cake\TestSuite\Fixture\FixtureManager')->getMock();
  119. $manager->fixturize($test);
  120. $test->fixtureManager = $manager;
  121. $manager->expects($this->once())->method('loadSingle');
  122. $result = $test->run();
  123. $this->assertEquals(0, $result->errorCount());
  124. }
  125. /**
  126. * testSkipIf
  127. *
  128. * @return void
  129. */
  130. public function testSkipIf()
  131. {
  132. $test = new FixturizedTestCase('testSkipIfTrue');
  133. $result = $test->run();
  134. $this->assertEquals(1, $result->skippedCount());
  135. $test = new FixturizedTestCase('testSkipIfFalse');
  136. $result = $test->run();
  137. $this->assertEquals(0, $result->skippedCount());
  138. }
  139. /**
  140. * Test that TestCase::setUp() backs up values.
  141. *
  142. * @return void
  143. */
  144. public function testSetupBackUpValues()
  145. {
  146. $this->assertArrayHasKey('debug', $this->_configure);
  147. }
  148. /**
  149. * test assertTextNotEquals()
  150. *
  151. * @return void
  152. */
  153. public function testAssertTextNotEquals()
  154. {
  155. $one = "\r\nOne\rTwooo";
  156. $two = "\nOne\nTwo";
  157. $this->assertTextNotEquals($one, $two);
  158. }
  159. /**
  160. * test assertTextEquals()
  161. *
  162. * @return void
  163. */
  164. public function testAssertTextEquals()
  165. {
  166. $one = "\r\nOne\rTwo";
  167. $two = "\nOne\nTwo";
  168. $this->assertTextEquals($one, $two);
  169. }
  170. /**
  171. * test assertTextStartsWith()
  172. *
  173. * @return void
  174. */
  175. public function testAssertTextStartsWith()
  176. {
  177. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  178. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  179. $this->assertStringStartsWith("some\nstring", $stringDirty);
  180. $this->assertStringStartsNotWith("some\r\nstring\r\nwith", $stringDirty);
  181. $this->assertStringStartsNotWith("some\nstring\nwith", $stringDirty);
  182. $this->assertTextStartsWith("some\nstring\nwith", $stringDirty);
  183. $this->assertTextStartsWith("some\r\nstring\r\nwith", $stringDirty);
  184. }
  185. /**
  186. * test assertTextStartsNotWith()
  187. *
  188. * @return void
  189. */
  190. public function testAssertTextStartsNotWith()
  191. {
  192. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  193. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  194. $this->assertTextStartsNotWith("some\nstring\nwithout", $stringDirty);
  195. }
  196. /**
  197. * test assertTextEndsWith()
  198. *
  199. * @return void
  200. */
  201. public function testAssertTextEndsWith()
  202. {
  203. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  204. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  205. $this->assertTextEndsWith("string\nwith\r\ndifferent\rline endings!", $stringDirty);
  206. $this->assertTextEndsWith("string\r\nwith\ndifferent\nline endings!", $stringDirty);
  207. }
  208. /**
  209. * test assertTextEndsNotWith()
  210. *
  211. * @return void
  212. */
  213. public function testAssertTextEndsNotWith()
  214. {
  215. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  216. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  217. $this->assertStringEndsNotWith("different\nline endings", $stringDirty);
  218. $this->assertTextEndsNotWith("different\rline endings", $stringDirty);
  219. }
  220. /**
  221. * test assertTextContains()
  222. *
  223. * @return void
  224. */
  225. public function testAssertTextContains()
  226. {
  227. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  228. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  229. $this->assertContains("different", $stringDirty);
  230. $this->assertNotContains("different\rline", $stringDirty);
  231. $this->assertTextContains("different\rline", $stringDirty);
  232. }
  233. /**
  234. * test assertTextNotContains()
  235. *
  236. * @return void
  237. */
  238. public function testAssertTextNotContains()
  239. {
  240. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  241. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  242. $this->assertTextNotContains("different\rlines", $stringDirty);
  243. }
  244. /**
  245. * test testAssertWithinRange()
  246. *
  247. * @return void
  248. */
  249. public function testAssertWithinRange()
  250. {
  251. $this->assertWithinRange(21, 22, 1, 'Not within range');
  252. $this->assertWithinRange(21.3, 22.2, 1.0, 'Not within range');
  253. }
  254. /**
  255. * test testAssertNotWithinRange()
  256. *
  257. * @return void
  258. */
  259. public function testAssertNotWithinRange()
  260. {
  261. $this->assertNotWithinRange(21, 23, 1, 'Within range');
  262. $this->assertNotWithinRange(21.3, 22.2, 0.7, 'Within range');
  263. }
  264. /**
  265. * test getMockForModel()
  266. *
  267. * @return void
  268. */
  269. public function testGetMockForModel()
  270. {
  271. Configure::write('App.namespace', 'TestApp');
  272. $Posts = $this->getMockForModel('Posts');
  273. $entity = new Entity([]);
  274. $this->assertInstanceOf('TestApp\Model\Table\PostsTable', $Posts);
  275. $this->assertNull($Posts->save($entity));
  276. $this->assertNull($Posts->table());
  277. $Posts = $this->getMockForModel('Posts', ['save']);
  278. $Posts->expects($this->at(0))
  279. ->method('save')
  280. ->will($this->returnValue('mocked'));
  281. $this->assertEquals('mocked', $Posts->save($entity));
  282. $this->assertEquals('\Cake\ORM\Entity', $Posts->entityClass());
  283. $Posts = $this->getMockForModel('Posts', ['doSomething']);
  284. $this->assertInstanceOf('Cake\Database\Connection', $Posts->connection());
  285. $this->assertEquals('test', $Posts->connection()->configName());
  286. $Tags = $this->getMockForModel('Tags', ['doSomething']);
  287. $this->assertEquals('TestApp\Model\Entity\Tag', $Tags->entityClass());
  288. }
  289. /**
  290. * Test getMockForModel on secondary datasources.
  291. *
  292. * @return void
  293. */
  294. public function testGetMockForModelSecondaryDatasource()
  295. {
  296. ConnectionManager::alias('test', 'secondary');
  297. $post = $this->getMockForModel(__NAMESPACE__ . '\SecondaryPostsTable', ['save']);
  298. $this->assertEquals('test', $post->connection()->configName());
  299. }
  300. /**
  301. * test getMockForModel() with plugin models
  302. *
  303. * @return void
  304. */
  305. public function testGetMockForModelWithPlugin()
  306. {
  307. Configure::write('App.namespace', 'TestApp');
  308. Plugin::load('TestPlugin');
  309. $TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComments');
  310. $result = TableRegistry::get('TestPlugin.TestPluginComments');
  311. $this->assertInstanceOf('TestPlugin\Model\Table\TestPluginCommentsTable', $result);
  312. $this->assertSame($TestPluginComment, $result);
  313. $TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComments', ['save']);
  314. $this->assertInstanceOf('TestPlugin\Model\Table\TestPluginCommentsTable', $TestPluginComment);
  315. $this->assertEquals('\Cake\ORM\Entity', $TestPluginComment->entityClass());
  316. $TestPluginComment->expects($this->at(0))
  317. ->method('save')
  318. ->will($this->returnValue(true));
  319. $TestPluginComment->expects($this->at(1))
  320. ->method('save')
  321. ->will($this->returnValue(false));
  322. $entity = new Entity([]);
  323. $this->assertTrue($TestPluginComment->save($entity));
  324. $this->assertFalse($TestPluginComment->save($entity));
  325. $TestPluginAuthors = $this->getMockForModel('TestPlugin.Authors', ['doSomething']);
  326. $this->assertInstanceOf('TestPlugin\Model\Table\AuthorsTable', $TestPluginAuthors);
  327. $this->assertEquals('TestPlugin\Model\Entity\Author', $TestPluginAuthors->entityClass());
  328. }
  329. /**
  330. * testGetMockForModelTable
  331. *
  332. * @return void
  333. */
  334. public function testGetMockForModelTable()
  335. {
  336. $Mock = $this->getMockForModel(
  337. 'Table',
  338. ['save'],
  339. ['alias' => 'Comments', 'className' => '\Cake\ORM\Table']
  340. );
  341. $result = TableRegistry::get('Comments');
  342. $this->assertInstanceOf('Cake\ORM\Table', $result);
  343. $this->assertEquals('Comments', $Mock->alias());
  344. $Mock->expects($this->at(0))
  345. ->method('save')
  346. ->will($this->returnValue(true));
  347. $Mock->expects($this->at(1))
  348. ->method('save')
  349. ->will($this->returnValue(false));
  350. $entity = new Entity([]);
  351. $this->assertTrue($Mock->save($entity));
  352. $this->assertFalse($Mock->save($entity));
  353. }
  354. /**
  355. * Test getting a table mock that doesn't have a preset table name sets the proper name
  356. *
  357. * @return void
  358. */
  359. public function testGetMockForModelSetTable()
  360. {
  361. Configure::write('App.namespace', 'TestApp');
  362. $I18n = $this->getMockForModel('I18n', ['doSomething']);
  363. $this->assertEquals('custom_i18n_table', $I18n->table());
  364. $Tags = $this->getMockForModel('Tags', ['doSomething']);
  365. $this->assertEquals('tags', $Tags->table());
  366. }
  367. }