TestCaseTest.php 16 KB

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