TestCaseTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. * testDeprecated
  158. *
  159. * @return void
  160. */
  161. public function testDeprecated()
  162. {
  163. $value = 'custom';
  164. $setter = 'setLayout';
  165. $getter = 'getLayout';
  166. $property = 'layout';
  167. $controller = new \Cake\Controller\Controller();
  168. $controller->viewBuilder()->{$setter}($value);
  169. $this->deprecated(function () use ($value, $getter, $controller, $property) {
  170. $this->assertSame($value, $controller->$property);
  171. $this->assertSame($value, $controller->viewBuilder()->{$getter}());
  172. });
  173. }
  174. /**
  175. * testDeprecated
  176. *
  177. * @expectedException \PHPUnit\Framework\AssertionFailedError
  178. * @return void
  179. */
  180. public function testDeprecatedWithException()
  181. {
  182. $value = 'custom';
  183. $setter = 'setLayout';
  184. $getter = 'getLayout';
  185. $property = 'layout';
  186. $controller = new \Cake\Controller\Controller();
  187. $controller->viewBuilder()->{$setter}($value);
  188. $this->deprecated(function () use ($value, $getter, $controller, $property) {
  189. $this->assertSame($value, $controller->$property);
  190. $this->assertSame('Derp', $controller->viewBuilder()->{$getter}());
  191. });
  192. }
  193. /**
  194. * Test that TestCase::setUp() backs up values.
  195. *
  196. * @return void
  197. */
  198. public function testSetupBackUpValues()
  199. {
  200. $this->assertArrayHasKey('debug', $this->_configure);
  201. }
  202. /**
  203. * test assertTextNotEquals()
  204. *
  205. * @return void
  206. */
  207. public function testAssertTextNotEquals()
  208. {
  209. $one = "\r\nOne\rTwooo";
  210. $two = "\nOne\nTwo";
  211. $this->assertTextNotEquals($one, $two);
  212. }
  213. /**
  214. * test assertTextEquals()
  215. *
  216. * @return void
  217. */
  218. public function testAssertTextEquals()
  219. {
  220. $one = "\r\nOne\rTwo";
  221. $two = "\nOne\nTwo";
  222. $this->assertTextEquals($one, $two);
  223. }
  224. /**
  225. * test assertTextStartsWith()
  226. *
  227. * @return void
  228. */
  229. public function testAssertTextStartsWith()
  230. {
  231. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  232. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  233. $this->assertStringStartsWith("some\nstring", $stringDirty);
  234. $this->assertStringStartsNotWith("some\r\nstring\r\nwith", $stringDirty);
  235. $this->assertStringStartsNotWith("some\nstring\nwith", $stringDirty);
  236. $this->assertTextStartsWith("some\nstring\nwith", $stringDirty);
  237. $this->assertTextStartsWith("some\r\nstring\r\nwith", $stringDirty);
  238. }
  239. /**
  240. * test assertTextStartsNotWith()
  241. *
  242. * @return void
  243. */
  244. public function testAssertTextStartsNotWith()
  245. {
  246. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  247. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  248. $this->assertTextStartsNotWith("some\nstring\nwithout", $stringDirty);
  249. }
  250. /**
  251. * test assertTextEndsWith()
  252. *
  253. * @return void
  254. */
  255. public function testAssertTextEndsWith()
  256. {
  257. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  258. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  259. $this->assertTextEndsWith("string\nwith\r\ndifferent\rline endings!", $stringDirty);
  260. $this->assertTextEndsWith("string\r\nwith\ndifferent\nline endings!", $stringDirty);
  261. }
  262. /**
  263. * test assertTextEndsNotWith()
  264. *
  265. * @return void
  266. */
  267. public function testAssertTextEndsNotWith()
  268. {
  269. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  270. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  271. $this->assertStringEndsNotWith("different\nline endings", $stringDirty);
  272. $this->assertTextEndsNotWith("different\rline endings", $stringDirty);
  273. }
  274. /**
  275. * test assertTextContains()
  276. *
  277. * @return void
  278. */
  279. public function testAssertTextContains()
  280. {
  281. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  282. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  283. $this->assertContains('different', $stringDirty);
  284. $this->assertNotContains("different\rline", $stringDirty);
  285. $this->assertTextContains("different\rline", $stringDirty);
  286. }
  287. /**
  288. * test assertTextNotContains()
  289. *
  290. * @return void
  291. */
  292. public function testAssertTextNotContains()
  293. {
  294. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  295. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  296. $this->assertTextNotContains("different\rlines", $stringDirty);
  297. }
  298. /**
  299. * test testAssertWithinRange()
  300. *
  301. * @return void
  302. */
  303. public function testAssertWithinRange()
  304. {
  305. $this->assertWithinRange(21, 22, 1, 'Not within range');
  306. $this->assertWithinRange(21.3, 22.2, 1.0, 'Not within range');
  307. }
  308. /**
  309. * test testAssertNotWithinRange()
  310. *
  311. * @return void
  312. */
  313. public function testAssertNotWithinRange()
  314. {
  315. $this->assertNotWithinRange(21, 23, 1, 'Within range');
  316. $this->assertNotWithinRange(21.3, 22.2, 0.7, 'Within range');
  317. }
  318. /**
  319. * test getMockForModel()
  320. *
  321. * @return void
  322. */
  323. public function testGetMockForModel()
  324. {
  325. static::setAppNamespace();
  326. $Posts = $this->getMockForModel('Posts');
  327. $entity = new Entity([]);
  328. $this->assertInstanceOf('TestApp\Model\Table\PostsTable', $Posts);
  329. $this->assertNull($Posts->save($entity));
  330. $this->assertNull($Posts->table());
  331. $Posts = $this->getMockForModel('Posts', ['save']);
  332. $Posts->expects($this->at(0))
  333. ->method('save')
  334. ->will($this->returnValue('mocked'));
  335. $this->assertEquals('mocked', $Posts->save($entity));
  336. $this->assertEquals('\Cake\ORM\Entity', $Posts->entityClass());
  337. $Posts = $this->getMockForModel('Posts', ['doSomething']);
  338. $this->assertInstanceOf('Cake\Database\Connection', $Posts->connection());
  339. $this->assertEquals('test', $Posts->connection()->configName());
  340. $Tags = $this->getMockForModel('Tags', ['doSomething']);
  341. $this->assertEquals('TestApp\Model\Entity\Tag', $Tags->entityClass());
  342. }
  343. /**
  344. * Test getMockForModel on secondary datasources.
  345. *
  346. * @return void
  347. */
  348. public function testGetMockForModelSecondaryDatasource()
  349. {
  350. ConnectionManager::alias('test', 'secondary');
  351. $post = $this->getMockForModel(__NAMESPACE__ . '\SecondaryPostsTable', ['save']);
  352. $this->assertEquals('test', $post->connection()->configName());
  353. }
  354. /**
  355. * test getMockForModel() with plugin models
  356. *
  357. * @return void
  358. */
  359. public function testGetMockForModelWithPlugin()
  360. {
  361. static::setAppNamespace();
  362. Plugin::load('TestPlugin');
  363. $TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComments');
  364. $result = TableRegistry::get('TestPlugin.TestPluginComments');
  365. $this->assertInstanceOf('TestPlugin\Model\Table\TestPluginCommentsTable', $result);
  366. $this->assertSame($TestPluginComment, $result);
  367. $TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComments', ['save']);
  368. $this->assertInstanceOf('TestPlugin\Model\Table\TestPluginCommentsTable', $TestPluginComment);
  369. $this->assertEquals('\Cake\ORM\Entity', $TestPluginComment->entityClass());
  370. $TestPluginComment->expects($this->at(0))
  371. ->method('save')
  372. ->will($this->returnValue(true));
  373. $TestPluginComment->expects($this->at(1))
  374. ->method('save')
  375. ->will($this->returnValue(false));
  376. $entity = new Entity([]);
  377. $this->assertTrue($TestPluginComment->save($entity));
  378. $this->assertFalse($TestPluginComment->save($entity));
  379. $TestPluginAuthors = $this->getMockForModel('TestPlugin.Authors', ['doSomething']);
  380. $this->assertInstanceOf('TestPlugin\Model\Table\AuthorsTable', $TestPluginAuthors);
  381. $this->assertEquals('TestPlugin\Model\Entity\Author', $TestPluginAuthors->entityClass());
  382. }
  383. /**
  384. * testGetMockForModelTable
  385. *
  386. * @return void
  387. */
  388. public function testGetMockForModelTable()
  389. {
  390. $Mock = $this->getMockForModel(
  391. 'Table',
  392. ['save'],
  393. ['alias' => 'Comments', 'className' => '\Cake\ORM\Table']
  394. );
  395. $result = TableRegistry::get('Comments');
  396. $this->assertInstanceOf('Cake\ORM\Table', $result);
  397. $this->assertEquals('Comments', $Mock->alias());
  398. $Mock->expects($this->at(0))
  399. ->method('save')
  400. ->will($this->returnValue(true));
  401. $Mock->expects($this->at(1))
  402. ->method('save')
  403. ->will($this->returnValue(false));
  404. $entity = new Entity([]);
  405. $this->assertTrue($Mock->save($entity));
  406. $this->assertFalse($Mock->save($entity));
  407. }
  408. /**
  409. * Test getting a table mock that doesn't have a preset table name sets the proper name
  410. *
  411. * @return void
  412. */
  413. public function testGetMockForModelSetTable()
  414. {
  415. static::setAppNamespace();
  416. $I18n = $this->getMockForModel('I18n', ['doSomething']);
  417. $this->assertEquals('custom_i18n_table', $I18n->table());
  418. $Tags = $this->getMockForModel('Tags', ['doSomething']);
  419. $this->assertEquals('tags', $Tags->table());
  420. }
  421. }