TestCaseTest.php 15 KB

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