TestCaseTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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\TestSuite\Fixture\FixtureManager;
  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. */
  48. public function testEventFiredMisconfiguredEventList()
  49. {
  50. $this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
  51. $manager = EventManager::instance();
  52. $this->assertEventFired('my.event', $manager);
  53. }
  54. /**
  55. * tests trying to assertEventFired without configuring an event list
  56. *
  57. */
  58. public function testEventFiredWithMisconfiguredEventList()
  59. {
  60. $this->expectException(\PHPUnit\Framework\AssertionFailedError::class);
  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. * tests loadFixtures loads all fixtures on the test
  126. *
  127. * @return void
  128. */
  129. public function testLoadAllFixtures()
  130. {
  131. $test = new FixturizedTestCase('testLoadAllFixtures');
  132. $test->autoFixtures = false;
  133. $manager = new FixtureManager();
  134. $manager->fixturize($test);
  135. $test->fixtureManager = $manager;
  136. $result = $test->run();
  137. $this->assertEquals(0, $result->errorCount());
  138. $this->assertCount(1, $result->passed());
  139. $this->assertFalse($test->autoFixtures);
  140. }
  141. /**
  142. * testSkipIf
  143. *
  144. * @return void
  145. */
  146. public function testSkipIf()
  147. {
  148. $test = new FixturizedTestCase('testSkipIfTrue');
  149. $result = $test->run();
  150. $this->assertEquals(1, $result->skippedCount());
  151. $test = new FixturizedTestCase('testSkipIfFalse');
  152. $result = $test->run();
  153. $this->assertEquals(0, $result->skippedCount());
  154. }
  155. /**
  156. * test withErrorReporting
  157. *
  158. * @return void
  159. */
  160. public function testWithErrorReporting()
  161. {
  162. $errorLevel = error_reporting();
  163. $this->withErrorReporting(E_USER_WARNING, function () {
  164. $this->assertSame(E_USER_WARNING, error_reporting());
  165. });
  166. $this->assertSame($errorLevel, error_reporting());
  167. }
  168. /**
  169. * test withErrorReporting with exceptions
  170. *
  171. * @expectedException \PHPUnit\Framework\AssertionFailedError
  172. * @return void
  173. */
  174. public function testWithErrorReportingWithException()
  175. {
  176. $errorLevel = error_reporting();
  177. try {
  178. $this->withErrorReporting(E_USER_WARNING, function () {
  179. $this->assertSame(1, 2);
  180. });
  181. } finally {
  182. $this->assertSame($errorLevel, error_reporting());
  183. }
  184. }
  185. /**
  186. * testDeprecated
  187. *
  188. * @return void
  189. */
  190. public function testDeprecated()
  191. {
  192. $value = 'custom';
  193. $setter = 'setLayout';
  194. $getter = 'getLayout';
  195. $property = 'layout';
  196. $controller = new \Cake\Controller\Controller();
  197. $controller->viewBuilder()->{$setter}($value);
  198. $this->deprecated(function () use ($value, $getter, $controller, $property) {
  199. $this->assertSame($value, $controller->$property);
  200. $this->assertSame($value, $controller->viewBuilder()->{$getter}());
  201. });
  202. }
  203. /**
  204. * testDeprecated
  205. *
  206. * @expectedException \PHPUnit\Framework\AssertionFailedError
  207. * @return void
  208. */
  209. public function testDeprecatedWithException()
  210. {
  211. $value = 'custom';
  212. $setter = 'setLayout';
  213. $getter = 'getLayout';
  214. $property = 'layout';
  215. $controller = new \Cake\Controller\Controller();
  216. $controller->viewBuilder()->{$setter}($value);
  217. $this->deprecated(function () use ($value, $getter, $controller, $property) {
  218. $this->assertSame($value, $controller->$property);
  219. $this->assertSame('Derp', $controller->viewBuilder()->{$getter}());
  220. });
  221. }
  222. /**
  223. * Test that TestCase::setUp() backs up values.
  224. *
  225. * @return void
  226. */
  227. public function testSetupBackUpValues()
  228. {
  229. $this->assertArrayHasKey('debug', $this->_configure);
  230. }
  231. /**
  232. * test assertTextNotEquals()
  233. *
  234. * @return void
  235. */
  236. public function testAssertTextNotEquals()
  237. {
  238. $one = "\r\nOne\rTwooo";
  239. $two = "\nOne\nTwo";
  240. $this->assertTextNotEquals($one, $two);
  241. }
  242. /**
  243. * test assertTextEquals()
  244. *
  245. * @return void
  246. */
  247. public function testAssertTextEquals()
  248. {
  249. $one = "\r\nOne\rTwo";
  250. $two = "\nOne\nTwo";
  251. $this->assertTextEquals($one, $two);
  252. }
  253. /**
  254. * test assertTextStartsWith()
  255. *
  256. * @return void
  257. */
  258. public function testAssertTextStartsWith()
  259. {
  260. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  261. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  262. $this->assertStringStartsWith("some\nstring", $stringDirty);
  263. $this->assertStringStartsNotWith("some\r\nstring\r\nwith", $stringDirty);
  264. $this->assertStringStartsNotWith("some\nstring\nwith", $stringDirty);
  265. $this->assertTextStartsWith("some\nstring\nwith", $stringDirty);
  266. $this->assertTextStartsWith("some\r\nstring\r\nwith", $stringDirty);
  267. }
  268. /**
  269. * test assertTextStartsNotWith()
  270. *
  271. * @return void
  272. */
  273. public function testAssertTextStartsNotWith()
  274. {
  275. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  276. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  277. $this->assertTextStartsNotWith("some\nstring\nwithout", $stringDirty);
  278. }
  279. /**
  280. * test assertTextEndsWith()
  281. *
  282. * @return void
  283. */
  284. public function testAssertTextEndsWith()
  285. {
  286. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  287. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  288. $this->assertTextEndsWith("string\nwith\r\ndifferent\rline endings!", $stringDirty);
  289. $this->assertTextEndsWith("string\r\nwith\ndifferent\nline endings!", $stringDirty);
  290. }
  291. /**
  292. * test assertTextEndsNotWith()
  293. *
  294. * @return void
  295. */
  296. public function testAssertTextEndsNotWith()
  297. {
  298. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  299. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  300. $this->assertStringEndsNotWith("different\nline endings", $stringDirty);
  301. $this->assertTextEndsNotWith("different\rline endings", $stringDirty);
  302. }
  303. /**
  304. * test assertTextContains()
  305. *
  306. * @return void
  307. */
  308. public function testAssertTextContains()
  309. {
  310. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  311. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  312. $this->assertContains('different', $stringDirty);
  313. $this->assertNotContains("different\rline", $stringDirty);
  314. $this->assertTextContains("different\rline", $stringDirty);
  315. }
  316. /**
  317. * test assertTextNotContains()
  318. *
  319. * @return void
  320. */
  321. public function testAssertTextNotContains()
  322. {
  323. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  324. $stringClean = "some\nstring\nwith\ndifferent\nline endings!";
  325. $this->assertTextNotContains("different\rlines", $stringDirty);
  326. }
  327. /**
  328. * test testAssertWithinRange()
  329. *
  330. * @return void
  331. */
  332. public function testAssertWithinRange()
  333. {
  334. $this->assertWithinRange(21, 22, 1, 'Not within range');
  335. $this->assertWithinRange(21.3, 22.2, 1.0, 'Not within range');
  336. }
  337. /**
  338. * test testAssertNotWithinRange()
  339. *
  340. * @return void
  341. */
  342. public function testAssertNotWithinRange()
  343. {
  344. $this->assertNotWithinRange(21, 23, 1, 'Within range');
  345. $this->assertNotWithinRange(21.3, 22.2, 0.7, 'Within range');
  346. }
  347. /**
  348. * test getMockForModel()
  349. *
  350. * @return void
  351. */
  352. public function testGetMockForModel()
  353. {
  354. static::setAppNamespace();
  355. $Posts = $this->getMockForModel('Posts');
  356. $entity = new Entity([]);
  357. $this->assertInstanceOf('TestApp\Model\Table\PostsTable', $Posts);
  358. $this->assertNull($Posts->save($entity));
  359. $this->assertNull($Posts->getTable());
  360. $Posts = $this->getMockForModel('Posts', ['save']);
  361. $Posts->expects($this->at(0))
  362. ->method('save')
  363. ->will($this->returnValue('mocked'));
  364. $this->assertEquals('mocked', $Posts->save($entity));
  365. $this->assertEquals('Cake\ORM\Entity', $Posts->getEntityClass());
  366. $Posts = $this->getMockForModel('Posts', ['doSomething']);
  367. $this->assertInstanceOf('Cake\Database\Connection', $Posts->getConnection());
  368. $this->assertEquals('test', $Posts->getConnection()->configName());
  369. $Tags = $this->getMockForModel('Tags', ['doSomething']);
  370. $this->assertEquals('TestApp\Model\Entity\Tag', $Tags->getEntityClass());
  371. }
  372. /**
  373. * Test getMockForModel on secondary datasources.
  374. *
  375. * @return void
  376. */
  377. public function testGetMockForModelSecondaryDatasource()
  378. {
  379. ConnectionManager::alias('test', 'secondary');
  380. $post = $this->getMockForModel(__NAMESPACE__ . '\SecondaryPostsTable', ['save']);
  381. $this->assertEquals('test', $post->getConnection()->configName());
  382. }
  383. /**
  384. * test getMockForModel() with plugin models
  385. *
  386. * @return void
  387. */
  388. public function testGetMockForModelWithPlugin()
  389. {
  390. static::setAppNamespace();
  391. Plugin::load('TestPlugin');
  392. $TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComments');
  393. $result = $this->getTableLocator()->get('TestPlugin.TestPluginComments');
  394. $this->assertInstanceOf('TestPlugin\Model\Table\TestPluginCommentsTable', $result);
  395. $this->assertSame($TestPluginComment, $result);
  396. $TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComments', ['save']);
  397. $this->assertInstanceOf('TestPlugin\Model\Table\TestPluginCommentsTable', $TestPluginComment);
  398. $this->assertEquals('Cake\ORM\Entity', $TestPluginComment->getEntityClass());
  399. $TestPluginComment->expects($this->at(0))
  400. ->method('save')
  401. ->will($this->returnValue(true));
  402. $TestPluginComment->expects($this->at(1))
  403. ->method('save')
  404. ->will($this->returnValue(false));
  405. $entity = new Entity([]);
  406. $this->assertTrue($TestPluginComment->save($entity));
  407. $this->assertFalse($TestPluginComment->save($entity));
  408. $TestPluginAuthors = $this->getMockForModel('TestPlugin.Authors', ['doSomething']);
  409. $this->assertInstanceOf('TestPlugin\Model\Table\AuthorsTable', $TestPluginAuthors);
  410. $this->assertEquals('TestPlugin\Model\Entity\Author', $TestPluginAuthors->getEntityClass());
  411. }
  412. /**
  413. * testGetMockForModelTable
  414. *
  415. * @return void
  416. */
  417. public function testGetMockForModelTable()
  418. {
  419. $Mock = $this->getMockForModel(
  420. 'Table',
  421. ['save'],
  422. ['alias' => 'Comments', 'className' => '\Cake\ORM\Table']
  423. );
  424. $result = $this->getTableLocator()->get('Comments');
  425. $this->assertInstanceOf('Cake\ORM\Table', $result);
  426. $this->assertEquals('Comments', $Mock->getAlias());
  427. $Mock->expects($this->at(0))
  428. ->method('save')
  429. ->will($this->returnValue(true));
  430. $Mock->expects($this->at(1))
  431. ->method('save')
  432. ->will($this->returnValue(false));
  433. $entity = new Entity([]);
  434. $this->assertTrue($Mock->save($entity));
  435. $this->assertFalse($Mock->save($entity));
  436. }
  437. /**
  438. * Test getting a table mock that doesn't have a preset table name sets the proper name
  439. *
  440. * @return void
  441. */
  442. public function testGetMockForModelSetTable()
  443. {
  444. static::setAppNamespace();
  445. $I18n = $this->getMockForModel('I18n', ['doSomething']);
  446. $this->assertEquals('custom_i18n_table', $I18n->getTable());
  447. $Tags = $this->getMockForModel('Tags', ['doSomething']);
  448. $this->assertEquals('tags', $Tags->getTable());
  449. }
  450. }