TestCaseTest.php 13 KB

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