TestCaseTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <?php
  2. /**
  3. * CakePHP : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP Project
  12. * @since 1.2.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\TestSuite;
  16. use Cake\Core\Plugin;
  17. use Cake\Datasource\ConnectionManager;
  18. use Cake\Event\Event;
  19. use Cake\Event\EventList;
  20. use Cake\Event\EventManager;
  21. use Cake\ORM\Entity;
  22. use Cake\ORM\Table;
  23. use Cake\ORM\TableRegistry;
  24. use Cake\TestSuite\TestCase;
  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. class TestCaseTest extends TestCase
  43. {
  44. /**
  45. * tests trying to assertEventFired without configuring an event list
  46. *
  47. * @expectedException \PHPUnit\Framework\AssertionFailedError
  48. */
  49. public function testEventFiredMisconfiguredEventList()
  50. {
  51. $manager = EventManager::instance();
  52. $this->assertEventFired('my.event', $manager);
  53. }
  54. /**
  55. * tests trying to assertEventFired without configuring an event list
  56. *
  57. * @expectedException \PHPUnit\Framework\AssertionFailedError
  58. */
  59. public function testEventFiredWithMisconfiguredEventList()
  60. {
  61. $manager = EventManager::instance();
  62. $this->assertEventFiredWith('my.event', 'some', 'data', $manager);
  63. }
  64. /**
  65. * tests assertEventFiredWith
  66. *
  67. * @return void
  68. */
  69. public function testEventFiredWith()
  70. {
  71. $manager = EventManager::instance();
  72. $manager->setEventList(new EventList());
  73. $manager->trackEvents(true);
  74. $event = new Event('my.event', $this, [
  75. 'some' => 'data'
  76. ]);
  77. $manager->dispatch($event);
  78. $this->assertEventFiredWith('my.event', 'some', 'data');
  79. $manager = new EventManager();
  80. $manager->setEventList(new EventList());
  81. $manager->trackEvents(true);
  82. $event = new Event('my.event', $this, [
  83. 'other' => 'data'
  84. ]);
  85. $manager->dispatch($event);
  86. $this->assertEventFiredWith('my.event', 'other', 'data', $manager);
  87. }
  88. /**
  89. * tests assertEventFired
  90. *
  91. * @return void
  92. */
  93. public function testEventFired()
  94. {
  95. $manager = EventManager::instance();
  96. $manager->setEventList(new EventList());
  97. $manager->trackEvents(true);
  98. $event = new Event('my.event');
  99. $manager->dispatch($event);
  100. $this->assertEventFired('my.event');
  101. $manager = new EventManager();
  102. $manager->setEventList(new EventList());
  103. $manager->trackEvents(true);
  104. $event = new Event('my.event');
  105. $manager->dispatch($event);
  106. $this->assertEventFired('my.event', $manager);
  107. }
  108. /**
  109. * testLoadFixturesOnDemand
  110. *
  111. * @return void
  112. */
  113. public function testLoadFixturesOnDemand()
  114. {
  115. $test = new FixturizedTestCase('testFixtureLoadOnDemand');
  116. $test->autoFixtures = false;
  117. $manager = $this->getMockBuilder('Cake\TestSuite\Fixture\FixtureManager')->getMock();
  118. $manager->fixturize($test);
  119. $test->fixtureManager = $manager;
  120. $manager->expects($this->once())->method('loadSingle');
  121. $result = $test->run();
  122. $this->assertEquals(0, $result->errorCount());
  123. }
  124. /**
  125. * testSkipIf
  126. *
  127. * @return void
  128. */
  129. public function testSkipIf()
  130. {
  131. $test = new FixturizedTestCase('testSkipIfTrue');
  132. $result = $test->run();
  133. $this->assertEquals(1, $result->skippedCount());
  134. $test = new FixturizedTestCase('testSkipIfFalse');
  135. $result = $test->run();
  136. $this->assertEquals(0, $result->skippedCount());
  137. }
  138. /**
  139. * Test that TestCase::setUp() backs up values.
  140. *
  141. * @return void
  142. */
  143. public function testSetupBackUpValues()
  144. {
  145. $this->assertArrayHasKey('debug', $this->_configure);
  146. }
  147. /**
  148. * test assertTextNotEquals()
  149. *
  150. * @return void
  151. */
  152. public function testAssertTextNotEquals()
  153. {
  154. $one = "\r\nOne\rTwooo";
  155. $two = "\nOne\nTwo";
  156. $this->assertTextNotEquals($one, $two);
  157. }
  158. /**
  159. * test assertTextEquals()
  160. *
  161. * @return void
  162. */
  163. public function testAssertTextEquals()
  164. {
  165. $one = "\r\nOne\rTwo";
  166. $two = "\nOne\nTwo";
  167. $this->assertTextEquals($one, $two);
  168. }
  169. /**
  170. * test assertTextStartsWith()
  171. *
  172. * @return void
  173. */
  174. public function testAssertTextStartsWith()
  175. {
  176. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  177. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  178. $this->assertStringStartsWith("some\nstring", $stringDirty);
  179. $this->assertStringStartsNotWith("some\r\nstring\r\nwith", $stringDirty);
  180. $this->assertStringStartsNotWith("some\nstring\nwith", $stringDirty);
  181. $this->assertTextStartsWith("some\nstring\nwith", $stringDirty);
  182. $this->assertTextStartsWith("some\r\nstring\r\nwith", $stringDirty);
  183. }
  184. /**
  185. * test assertTextStartsNotWith()
  186. *
  187. * @return void
  188. */
  189. public function testAssertTextStartsNotWith()
  190. {
  191. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  192. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  193. $this->assertTextStartsNotWith("some\nstring\nwithout", $stringDirty);
  194. }
  195. /**
  196. * test assertTextEndsWith()
  197. *
  198. * @return void
  199. */
  200. public function testAssertTextEndsWith()
  201. {
  202. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  203. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  204. $this->assertTextEndsWith("string\nwith\r\ndifferent\rline endings!", $stringDirty);
  205. $this->assertTextEndsWith("string\r\nwith\ndifferent\nline endings!", $stringDirty);
  206. }
  207. /**
  208. * test assertTextEndsNotWith()
  209. *
  210. * @return void
  211. */
  212. public function testAssertTextEndsNotWith()
  213. {
  214. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  215. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  216. $this->assertStringEndsNotWith("different\nline endings", $stringDirty);
  217. $this->assertTextEndsNotWith("different\rline endings", $stringDirty);
  218. }
  219. /**
  220. * test assertTextContains()
  221. *
  222. * @return void
  223. */
  224. public function testAssertTextContains()
  225. {
  226. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  227. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  228. $this->assertContains("different", $stringDirty);
  229. $this->assertNotContains("different\rline", $stringDirty);
  230. $this->assertTextContains("different\rline", $stringDirty);
  231. }
  232. /**
  233. * test assertTextNotContains()
  234. *
  235. * @return void
  236. */
  237. public function testAssertTextNotContains()
  238. {
  239. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  240. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  241. $this->assertTextNotContains("different\rlines", $stringDirty);
  242. }
  243. /**
  244. * test testAssertWithinRange()
  245. *
  246. * @return void
  247. */
  248. public function testAssertWithinRange()
  249. {
  250. $this->assertWithinRange(21, 22, 1, 'Not within range');
  251. $this->assertWithinRange(21.3, 22.2, 1.0, 'Not within range');
  252. }
  253. /**
  254. * test testAssertNotWithinRange()
  255. *
  256. * @return void
  257. */
  258. public function testAssertNotWithinRange()
  259. {
  260. $this->assertNotWithinRange(21, 23, 1, 'Within range');
  261. $this->assertNotWithinRange(21.3, 22.2, 0.7, 'Within range');
  262. }
  263. /**
  264. * test getMockForModel()
  265. *
  266. * @return void
  267. */
  268. public function testGetMockForModel()
  269. {
  270. static::setAppNamespace();
  271. $Posts = $this->getMockForModel('Posts');
  272. $entity = new Entity([]);
  273. $this->assertInstanceOf('TestApp\Model\Table\PostsTable', $Posts);
  274. $this->assertNull($Posts->save($entity));
  275. $this->assertNull($Posts->table());
  276. $Posts = $this->getMockForModel('Posts', ['save']);
  277. $Posts->expects($this->at(0))
  278. ->method('save')
  279. ->will($this->returnValue('mocked'));
  280. $this->assertEquals('mocked', $Posts->save($entity));
  281. $this->assertEquals('\Cake\ORM\Entity', $Posts->entityClass());
  282. $Posts = $this->getMockForModel('Posts', ['doSomething']);
  283. $this->assertInstanceOf('Cake\Database\Connection', $Posts->connection());
  284. $this->assertEquals('test', $Posts->connection()->configName());
  285. $Tags = $this->getMockForModel('Tags', ['doSomething']);
  286. $this->assertEquals('TestApp\Model\Entity\Tag', $Tags->entityClass());
  287. }
  288. /**
  289. * Test getMockForModel on secondary datasources.
  290. *
  291. * @return void
  292. */
  293. public function testGetMockForModelSecondaryDatasource()
  294. {
  295. ConnectionManager::alias('test', 'secondary');
  296. $post = $this->getMockForModel(__NAMESPACE__ . '\SecondaryPostsTable', ['save']);
  297. $this->assertEquals('test', $post->connection()->configName());
  298. }
  299. /**
  300. * test getMockForModel() with plugin models
  301. *
  302. * @return void
  303. */
  304. public function testGetMockForModelWithPlugin()
  305. {
  306. static::setAppNamespace();
  307. Plugin::load('TestPlugin');
  308. $TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComments');
  309. $result = TableRegistry::get('TestPlugin.TestPluginComments');
  310. $this->assertInstanceOf('TestPlugin\Model\Table\TestPluginCommentsTable', $result);
  311. $this->assertSame($TestPluginComment, $result);
  312. $TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComments', ['save']);
  313. $this->assertInstanceOf('TestPlugin\Model\Table\TestPluginCommentsTable', $TestPluginComment);
  314. $this->assertEquals('\Cake\ORM\Entity', $TestPluginComment->entityClass());
  315. $TestPluginComment->expects($this->at(0))
  316. ->method('save')
  317. ->will($this->returnValue(true));
  318. $TestPluginComment->expects($this->at(1))
  319. ->method('save')
  320. ->will($this->returnValue(false));
  321. $entity = new Entity([]);
  322. $this->assertTrue($TestPluginComment->save($entity));
  323. $this->assertFalse($TestPluginComment->save($entity));
  324. $TestPluginAuthors = $this->getMockForModel('TestPlugin.Authors', ['doSomething']);
  325. $this->assertInstanceOf('TestPlugin\Model\Table\AuthorsTable', $TestPluginAuthors);
  326. $this->assertEquals('TestPlugin\Model\Entity\Author', $TestPluginAuthors->entityClass());
  327. }
  328. /**
  329. * testGetMockForModelTable
  330. *
  331. * @return void
  332. */
  333. public function testGetMockForModelTable()
  334. {
  335. $Mock = $this->getMockForModel(
  336. 'Table',
  337. ['save'],
  338. ['alias' => 'Comments', 'className' => '\Cake\ORM\Table']
  339. );
  340. $result = TableRegistry::get('Comments');
  341. $this->assertInstanceOf('Cake\ORM\Table', $result);
  342. $this->assertEquals('Comments', $Mock->alias());
  343. $Mock->expects($this->at(0))
  344. ->method('save')
  345. ->will($this->returnValue(true));
  346. $Mock->expects($this->at(1))
  347. ->method('save')
  348. ->will($this->returnValue(false));
  349. $entity = new Entity([]);
  350. $this->assertTrue($Mock->save($entity));
  351. $this->assertFalse($Mock->save($entity));
  352. }
  353. /**
  354. * Test getting a table mock that doesn't have a preset table name sets the proper name
  355. *
  356. * @return void
  357. */
  358. public function testGetMockForModelSetTable()
  359. {
  360. static::setAppNamespace();
  361. $I18n = $this->getMockForModel('I18n', ['doSomething']);
  362. $this->assertEquals('custom_i18n_table', $I18n->table());
  363. $Tags = $this->getMockForModel('Tags', ['doSomething']);
  364. $this->assertEquals('tags', $Tags->table());
  365. }
  366. }