TestCaseTest.php 14 KB

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