TestCaseTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 TestApp\Model\Table\SecondaryPostsTable;
  27. /**
  28. * TestCaseTest
  29. */
  30. class TestCaseTest extends TestCase
  31. {
  32. /**
  33. * tests trying to assertEventFired without configuring an event list
  34. *
  35. */
  36. public function testEventFiredMisconfiguredEventList()
  37. {
  38. $this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
  39. $manager = EventManager::instance();
  40. $this->assertEventFired('my.event', $manager);
  41. }
  42. /**
  43. * tests trying to assertEventFired without configuring an event list
  44. *
  45. */
  46. public function testEventFiredWithMisconfiguredEventList()
  47. {
  48. $this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
  49. $manager = EventManager::instance();
  50. $this->assertEventFiredWith('my.event', 'some', 'data', $manager);
  51. }
  52. /**
  53. * tests assertEventFiredWith
  54. *
  55. * @return void
  56. */
  57. public function testEventFiredWith()
  58. {
  59. $manager = EventManager::instance();
  60. $manager->setEventList(new EventList());
  61. $manager->trackEvents(true);
  62. $event = new Event('my.event', $this, [
  63. 'some' => 'data',
  64. ]);
  65. $manager->dispatch($event);
  66. $this->assertEventFiredWith('my.event', 'some', 'data');
  67. $manager = new EventManager();
  68. $manager->setEventList(new EventList());
  69. $manager->trackEvents(true);
  70. $event = new Event('my.event', $this, [
  71. 'other' => 'data',
  72. ]);
  73. $manager->dispatch($event);
  74. $this->assertEventFiredWith('my.event', 'other', 'data', $manager);
  75. }
  76. /**
  77. * tests assertEventFired
  78. *
  79. * @return void
  80. */
  81. public function testEventFired()
  82. {
  83. $manager = EventManager::instance();
  84. $manager->setEventList(new EventList());
  85. $manager->trackEvents(true);
  86. $event = new Event('my.event');
  87. $manager->dispatch($event);
  88. $this->assertEventFired('my.event');
  89. $manager = new EventManager();
  90. $manager->setEventList(new EventList());
  91. $manager->trackEvents(true);
  92. $event = new Event('my.event');
  93. $manager->dispatch($event);
  94. $this->assertEventFired('my.event', $manager);
  95. }
  96. /**
  97. * testLoadFixturesOnDemand
  98. *
  99. * @return void
  100. */
  101. public function testLoadFixturesOnDemand()
  102. {
  103. $test = new FixturizedTestCase('testFixtureLoadOnDemand');
  104. $test->autoFixtures = false;
  105. $manager = $this->getMockBuilder('Cake\TestSuite\Fixture\FixtureManager')->getMock();
  106. $manager->fixturize($test);
  107. $test->fixtureManager = $manager;
  108. $manager->expects($this->once())->method('loadSingle');
  109. $result = $test->run();
  110. $this->assertEquals(0, $result->errorCount());
  111. }
  112. /**
  113. * tests loadFixtures loads all fixtures on the test
  114. *
  115. * @return void
  116. */
  117. public function testLoadAllFixtures()
  118. {
  119. $test = new FixturizedTestCase('testLoadAllFixtures');
  120. $test->autoFixtures = false;
  121. $manager = new FixtureManager();
  122. $manager->fixturize($test);
  123. $test->fixtureManager = $manager;
  124. $result = $test->run();
  125. $this->assertEquals(0, $result->errorCount());
  126. $this->assertCount(1, $result->passed());
  127. $this->assertFalse($test->autoFixtures);
  128. }
  129. /**
  130. * testSkipIf
  131. *
  132. * @return void
  133. */
  134. public function testSkipIf()
  135. {
  136. $test = new FixturizedTestCase('testSkipIfTrue');
  137. $result = $test->run();
  138. $this->assertEquals(1, $result->skippedCount());
  139. $test = new FixturizedTestCase('testSkipIfFalse');
  140. $result = $test->run();
  141. $this->assertEquals(0, $result->skippedCount());
  142. }
  143. /**
  144. * test withErrorReporting
  145. *
  146. * @return void
  147. */
  148. public function testWithErrorReporting()
  149. {
  150. $errorLevel = error_reporting();
  151. $this->withErrorReporting(E_USER_WARNING, function () {
  152. $this->assertSame(E_USER_WARNING, error_reporting());
  153. });
  154. $this->assertSame($errorLevel, error_reporting());
  155. }
  156. /**
  157. * test withErrorReporting with exceptions
  158. *
  159. * @expectedException \PHPUnit\Framework\AssertionFailedError
  160. * @return void
  161. */
  162. public function testWithErrorReportingWithException()
  163. {
  164. $errorLevel = error_reporting();
  165. try {
  166. $this->withErrorReporting(E_USER_WARNING, function () {
  167. $this->assertSame(1, 2);
  168. });
  169. } finally {
  170. $this->assertSame($errorLevel, error_reporting());
  171. }
  172. }
  173. /**
  174. * Test that TestCase::setUp() backs up values.
  175. *
  176. * @return void
  177. */
  178. public function testSetupBackUpValues()
  179. {
  180. $this->assertArrayHasKey('debug', $this->_configure);
  181. }
  182. /**
  183. * test assertTextNotEquals()
  184. *
  185. * @return void
  186. */
  187. public function testAssertTextNotEquals()
  188. {
  189. $one = "\r\nOne\rTwooo";
  190. $two = "\nOne\nTwo";
  191. $this->assertTextNotEquals($one, $two);
  192. }
  193. /**
  194. * test assertTextEquals()
  195. *
  196. * @return void
  197. */
  198. public function testAssertTextEquals()
  199. {
  200. $one = "\r\nOne\rTwo";
  201. $two = "\nOne\nTwo";
  202. $this->assertTextEquals($one, $two);
  203. }
  204. /**
  205. * test assertTextStartsWith()
  206. *
  207. * @return void
  208. */
  209. public function testAssertTextStartsWith()
  210. {
  211. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  212. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  213. $this->assertStringStartsWith("some\nstring", $stringDirty);
  214. $this->assertStringStartsNotWith("some\r\nstring\r\nwith", $stringDirty);
  215. $this->assertStringStartsNotWith("some\nstring\nwith", $stringDirty);
  216. $this->assertTextStartsWith("some\nstring\nwith", $stringDirty);
  217. $this->assertTextStartsWith("some\r\nstring\r\nwith", $stringDirty);
  218. }
  219. /**
  220. * test assertTextStartsNotWith()
  221. *
  222. * @return void
  223. */
  224. public function testAssertTextStartsNotWith()
  225. {
  226. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  227. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  228. $this->assertTextStartsNotWith("some\nstring\nwithout", $stringDirty);
  229. }
  230. /**
  231. * test assertTextEndsWith()
  232. *
  233. * @return void
  234. */
  235. public function testAssertTextEndsWith()
  236. {
  237. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  238. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  239. $this->assertTextEndsWith("string\nwith\r\ndifferent\rline endings!", $stringDirty);
  240. $this->assertTextEndsWith("string\r\nwith\ndifferent\nline endings!", $stringDirty);
  241. }
  242. /**
  243. * test assertTextEndsNotWith()
  244. *
  245. * @return void
  246. */
  247. public function testAssertTextEndsNotWith()
  248. {
  249. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  250. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  251. $this->assertStringEndsNotWith("different\nline endings", $stringDirty);
  252. $this->assertTextEndsNotWith("different\rline endings", $stringDirty);
  253. }
  254. /**
  255. * test assertTextContains()
  256. *
  257. * @return void
  258. */
  259. public function testAssertTextContains()
  260. {
  261. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  262. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  263. $this->assertContains('different', $stringDirty);
  264. $this->assertNotContains("different\rline", $stringDirty);
  265. $this->assertTextContains("different\rline", $stringDirty);
  266. }
  267. /**
  268. * test assertTextNotContains()
  269. *
  270. * @return void
  271. */
  272. public function testAssertTextNotContains()
  273. {
  274. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  275. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  276. $this->assertTextNotContains("different\rlines", $stringDirty);
  277. }
  278. /**
  279. * test testAssertWithinRange()
  280. *
  281. * @return void
  282. */
  283. public function testAssertWithinRange()
  284. {
  285. $this->assertWithinRange(21, 22, 1, 'Not within range');
  286. $this->assertWithinRange(21.3, 22.2, 1.0, 'Not within range');
  287. }
  288. /**
  289. * test testAssertNotWithinRange()
  290. *
  291. * @return void
  292. */
  293. public function testAssertNotWithinRange()
  294. {
  295. $this->assertNotWithinRange(21, 23, 1, 'Within range');
  296. $this->assertNotWithinRange(21.3, 22.2, 0.7, 'Within range');
  297. }
  298. /**
  299. * test getMockForModel()
  300. *
  301. * @return void
  302. */
  303. public function testGetMockForModel()
  304. {
  305. static::setAppNamespace();
  306. $Posts = $this->getMockForModel('Posts');
  307. $entity = new Entity([]);
  308. $this->assertInstanceOf('TestApp\Model\Table\PostsTable', $Posts);
  309. $this->assertNull($Posts->save($entity));
  310. $this->assertSame('', $Posts->getTable());
  311. $Posts = $this->getMockForModel('Posts', ['save']);
  312. $Posts->expects($this->at(0))
  313. ->method('save')
  314. ->will($this->returnValue('mocked'));
  315. $this->assertEquals('mocked', $Posts->save($entity));
  316. $this->assertEquals('Cake\ORM\Entity', $Posts->getEntityClass());
  317. $Posts = $this->getMockForModel('Posts', ['doSomething']);
  318. $this->assertInstanceOf('Cake\Database\Connection', $Posts->getConnection());
  319. $this->assertEquals('test', $Posts->getConnection()->configName());
  320. $Tags = $this->getMockForModel('Tags', ['doSomething']);
  321. $this->assertEquals('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->assertEquals('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->assertEquals('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', ['doSomething']);
  360. $this->assertInstanceOf('TestPlugin\Model\Table\AuthorsTable', $TestPluginAuthors);
  361. $this->assertEquals('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->assertEquals('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. $allMethodsMocks = $this->getMockForModel(
  397. 'Table',
  398. null,
  399. ['alias' => 'Comments', 'className' => Table::class]
  400. );
  401. $result = $this->getTableLocator()->get('Comments');
  402. $this->assertInstanceOf(Table::class, $result);
  403. $this->assertEquals('Comments', $allMethodsMocks->getAlias());
  404. $this->assertNotEquals($allMethodsStubs, $allMethodsMocks);
  405. }
  406. /**
  407. * Test getting a table mock that doesn't have a preset table name sets the proper name
  408. *
  409. * @return void
  410. */
  411. public function testGetMockForModelSetTable()
  412. {
  413. static::setAppNamespace();
  414. $I18n = $this->getMockForModel('I18n', ['doSomething']);
  415. $this->assertEquals('custom_i18n_table', $I18n->getTable());
  416. $Tags = $this->getMockForModel('Tags', ['doSomething']);
  417. $this->assertEquals('tags', $Tags->getTable());
  418. }
  419. }