TestCaseTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP Project
  13. * @since 1.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\TestSuite;
  17. use Cake\Core\Configure;
  18. use Cake\Datasource\ConnectionManager;
  19. use Cake\Event\Event;
  20. use Cake\Event\EventList;
  21. use Cake\Event\EventManager;
  22. use Cake\ORM\Entity;
  23. use Cake\ORM\Table;
  24. use Cake\Routing\Exception\MissingRouteException;
  25. use Cake\Routing\Router;
  26. use Cake\Test\Fixture\FixturizedTestCase;
  27. use Cake\TestSuite\TestCase;
  28. use PHPUnit\Framework\AssertionFailedError;
  29. use TestApp\Model\Table\SecondaryPostsTable;
  30. /**
  31. * TestCaseTest
  32. */
  33. class TestCaseTest extends TestCase
  34. {
  35. /**
  36. * tests trying to assertEventFired without configuring an event list
  37. */
  38. public function testEventFiredMisconfiguredEventList(): void
  39. {
  40. $this->expectException(AssertionFailedError::class);
  41. $manager = EventManager::instance();
  42. $this->assertEventFired('my.event', $manager);
  43. }
  44. /**
  45. * tests trying to assertEventFired without configuring an event list
  46. */
  47. public function testEventFiredWithMisconfiguredEventList(): void
  48. {
  49. $this->expectException(AssertionFailedError::class);
  50. $manager = EventManager::instance();
  51. $this->assertEventFiredWith('my.event', 'some', 'data', $manager);
  52. }
  53. /**
  54. * tests assertEventFiredWith
  55. */
  56. public function testEventFiredWith(): void
  57. {
  58. $manager = EventManager::instance();
  59. $manager->setEventList(new EventList());
  60. $manager->trackEvents(true);
  61. $event = new Event('my.event', $this, [
  62. 'some' => 'data',
  63. ]);
  64. $manager->dispatch($event);
  65. $this->assertEventFiredWith('my.event', 'some', 'data');
  66. $manager = new EventManager();
  67. $manager->setEventList(new EventList());
  68. $manager->trackEvents(true);
  69. $event = new Event('my.event', $this, [
  70. 'other' => 'data',
  71. ]);
  72. $manager->dispatch($event);
  73. $this->assertEventFiredWith('my.event', 'other', 'data', $manager);
  74. }
  75. /**
  76. * tests assertEventFired
  77. */
  78. public function testEventFired(): void
  79. {
  80. $manager = EventManager::instance();
  81. $manager->setEventList(new EventList());
  82. $manager->trackEvents(true);
  83. $event = new Event('my.event');
  84. $manager->dispatch($event);
  85. $this->assertEventFired('my.event');
  86. $manager = new EventManager();
  87. $manager->setEventList(new EventList());
  88. $manager->trackEvents(true);
  89. $event = new Event('my.event');
  90. $manager->dispatch($event);
  91. $this->assertEventFired('my.event', $manager);
  92. }
  93. /**
  94. * testSkipIf
  95. */
  96. public function testSkipIf(): void
  97. {
  98. $test = new FixturizedTestCase('testSkipIfTrue');
  99. $result = $test->run();
  100. $this->assertSame(1, $result->skippedCount());
  101. $test = new FixturizedTestCase('testSkipIfFalse');
  102. $result = $test->run();
  103. $this->assertSame(0, $result->skippedCount());
  104. }
  105. /**
  106. * test withErrorReporting
  107. */
  108. public function testWithErrorReporting(): void
  109. {
  110. $errorLevel = error_reporting();
  111. $this->withErrorReporting(E_USER_WARNING, function (): void {
  112. $this->assertSame(E_USER_WARNING, error_reporting());
  113. });
  114. $this->assertSame($errorLevel, error_reporting());
  115. }
  116. /**
  117. * test withErrorReporting with exceptions
  118. */
  119. public function testWithErrorReportingWithException(): void
  120. {
  121. $this->expectException(AssertionFailedError::class);
  122. $errorLevel = error_reporting();
  123. try {
  124. $this->withErrorReporting(E_USER_WARNING, function (): void {
  125. $this->assertSame(1, 2);
  126. });
  127. } finally {
  128. $this->assertSame($errorLevel, error_reporting());
  129. }
  130. }
  131. /**
  132. * Test that TestCase::setUp() backs up values.
  133. */
  134. public function testSetupBackUpValues(): void
  135. {
  136. $this->assertArrayHasKey('debug', $this->_configure);
  137. }
  138. /**
  139. * test assertTextNotEquals()
  140. */
  141. public function testAssertTextNotEquals(): void
  142. {
  143. $one = "\r\nOne\rTwooo";
  144. $two = "\nOne\nTwo";
  145. $this->assertTextNotEquals($one, $two);
  146. }
  147. /**
  148. * test assertTextEquals()
  149. */
  150. public function testAssertTextEquals(): void
  151. {
  152. $one = "\r\nOne\rTwo";
  153. $two = "\nOne\nTwo";
  154. $this->assertTextEquals($one, $two);
  155. }
  156. /**
  157. * test assertTextStartsWith()
  158. */
  159. public function testAssertTextStartsWith(): void
  160. {
  161. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  162. $this->assertStringStartsWith("some\nstring", $stringDirty);
  163. $this->assertStringStartsNotWith("some\r\nstring\r\nwith", $stringDirty);
  164. $this->assertStringStartsNotWith("some\nstring\nwith", $stringDirty);
  165. $this->assertTextStartsWith("some\nstring\nwith", $stringDirty);
  166. $this->assertTextStartsWith("some\r\nstring\r\nwith", $stringDirty);
  167. }
  168. /**
  169. * test assertTextStartsNotWith()
  170. */
  171. public function testAssertTextStartsNotWith(): void
  172. {
  173. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  174. $this->assertTextStartsNotWith("some\nstring\nwithout", $stringDirty);
  175. }
  176. /**
  177. * test assertTextEndsWith()
  178. */
  179. public function testAssertTextEndsWith(): void
  180. {
  181. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  182. $this->assertTextEndsWith("string\nwith\r\ndifferent\rline endings!", $stringDirty);
  183. $this->assertTextEndsWith("string\r\nwith\ndifferent\nline endings!", $stringDirty);
  184. }
  185. /**
  186. * test assertTextEndsNotWith()
  187. */
  188. public function testAssertTextEndsNotWith(): void
  189. {
  190. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  191. $this->assertStringEndsNotWith("different\nline endings", $stringDirty);
  192. $this->assertTextEndsNotWith("different\rline endings", $stringDirty);
  193. }
  194. /**
  195. * test assertTextContains()
  196. */
  197. public function testAssertTextContains(): void
  198. {
  199. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  200. $this->assertStringContainsString('different', $stringDirty);
  201. $this->assertStringNotContainsString("different\rline", $stringDirty);
  202. $this->assertTextContains("different\rline", $stringDirty);
  203. }
  204. /**
  205. * test assertTextNotContains()
  206. */
  207. public function testAssertTextNotContains(): void
  208. {
  209. $stringDirty = "some\nstring\r\nwith\rdifferent\nline endings!";
  210. $this->assertTextNotContains("different\rlines", $stringDirty);
  211. }
  212. /**
  213. * test testAssertWithinRange()
  214. */
  215. public function testAssertWithinRange(): void
  216. {
  217. $this->assertWithinRange(21, 22, 1, 'Not within range');
  218. $this->assertWithinRange(21.3, 22.2, 1.0, 'Not within range');
  219. }
  220. /**
  221. * test testAssertNotWithinRange()
  222. */
  223. public function testAssertNotWithinRange(): void
  224. {
  225. $this->assertNotWithinRange(21, 23, 1, 'Within range');
  226. $this->assertNotWithinRange(21.3, 22.2, 0.7, 'Within range');
  227. }
  228. /**
  229. * test getMockForModel()
  230. */
  231. public function testGetMockForModel(): void
  232. {
  233. static::setAppNamespace();
  234. // No methods will be mocked if $methods argument of getMockForModel() is empty.
  235. $Posts = $this->getMockForModel('Posts');
  236. $entity = new Entity([]);
  237. $this->assertInstanceOf('TestApp\Model\Table\PostsTable', $Posts);
  238. $this->assertSame('posts', $Posts->getTable());
  239. $Posts = $this->getMockForModel('Posts', ['save']);
  240. $Posts->expects($this->once())
  241. ->method('save')
  242. ->will($this->returnValue(false));
  243. $this->assertSame(false, $Posts->save($entity));
  244. $this->assertSame('Cake\ORM\Entity', $Posts->getEntityClass());
  245. $this->assertInstanceOf('Cake\Database\Connection', $Posts->getConnection());
  246. $this->assertSame('test', $Posts->getConnection()->configName());
  247. $Tags = $this->getMockForModel('Tags', ['save']);
  248. $this->assertSame('TestApp\Model\Entity\Tag', $Tags->getEntityClass());
  249. $SluggedPosts = $this->getMockForModel('SluggedPosts', ['slugify']);
  250. $SluggedPosts->expects($this->once())
  251. ->method('slugify')
  252. ->with('some value')
  253. ->will($this->returnValue('mocked'));
  254. $this->assertSame('mocked', $SluggedPosts->slugify('some value'));
  255. $SluggedPosts = $this->getMockForModel('SluggedPosts', ['save', 'slugify']);
  256. $SluggedPosts->expects($this->once())
  257. ->method('slugify')
  258. ->with('some value two')
  259. ->will($this->returnValue('mocked'));
  260. $this->assertSame('mocked', $SluggedPosts->slugify('some value two'));
  261. }
  262. /**
  263. * Test getMockForModel on secondary datasources.
  264. */
  265. public function testGetMockForModelSecondaryDatasource(): void
  266. {
  267. ConnectionManager::alias('test', 'secondary');
  268. $post = $this->getMockForModel(SecondaryPostsTable::class, ['save']);
  269. $this->assertSame('test', $post->getConnection()->configName());
  270. }
  271. /**
  272. * test getMockForModel() with plugin models
  273. */
  274. public function testGetMockForModelWithPlugin(): void
  275. {
  276. static::setAppNamespace();
  277. $this->loadPlugins(['TestPlugin']);
  278. $TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComments');
  279. $result = $this->getTableLocator()->get('TestPlugin.TestPluginComments');
  280. $this->assertInstanceOf('TestPlugin\Model\Table\TestPluginCommentsTable', $result);
  281. $this->assertSame($TestPluginComment, $result);
  282. $TestPluginComment = $this->getMockForModel('TestPlugin.TestPluginComments', ['save']);
  283. $this->assertInstanceOf('TestPlugin\Model\Table\TestPluginCommentsTable', $TestPluginComment);
  284. $this->assertSame('Cake\ORM\Entity', $TestPluginComment->getEntityClass());
  285. $TestPluginComment->expects($this->exactly(1))
  286. ->method('save')
  287. ->will($this->returnValue(false));
  288. $entity = new Entity([]);
  289. $this->assertFalse($TestPluginComment->save($entity));
  290. $TestPluginAuthors = $this->getMockForModel('TestPlugin.Authors', ['save']);
  291. $this->assertInstanceOf('TestPlugin\Model\Table\AuthorsTable', $TestPluginAuthors);
  292. $this->assertSame('TestPlugin\Model\Entity\Author', $TestPluginAuthors->getEntityClass());
  293. $this->clearPlugins();
  294. }
  295. /**
  296. * testGetMockForModelTable
  297. */
  298. public function testGetMockForModelTable(): void
  299. {
  300. $Mock = $this->getMockForModel(
  301. 'Table',
  302. ['save'],
  303. ['alias' => 'Comments', 'className' => Table::class]
  304. );
  305. $result = $this->getTableLocator()->get('Comments');
  306. $this->assertInstanceOf(Table::class, $result);
  307. $this->assertSame('Comments', $Mock->getAlias());
  308. $Mock->expects($this->exactly(1))
  309. ->method('save')
  310. ->will($this->returnValue(false));
  311. $entity = new Entity([]);
  312. $this->assertFalse($Mock->save($entity));
  313. $allMethodsStubs = $this->getMockForModel(
  314. 'Table',
  315. [],
  316. ['alias' => 'Comments', 'className' => Table::class]
  317. );
  318. $result = $this->getTableLocator()->get('Comments');
  319. $this->assertInstanceOf(Table::class, $result);
  320. $this->assertEmpty([], $allMethodsStubs->getAlias());
  321. }
  322. /**
  323. * Test getting a table mock that doesn't have a preset table name sets the proper name
  324. */
  325. public function testGetMockForModelSetTable(): void
  326. {
  327. static::setAppNamespace();
  328. ConnectionManager::alias('test', 'custom_i18n_datasource');
  329. $I18n = $this->getMockForModel('CustomI18n', ['save']);
  330. $this->assertSame('custom_i18n_table', $I18n->getTable());
  331. $Tags = $this->getMockForModel('Tags', ['save']);
  332. $this->assertSame('tags', $Tags->getTable());
  333. ConnectionManager::dropAlias('custom_i18n_datasource');
  334. }
  335. /**
  336. * Test loadRoutes() helper
  337. */
  338. public function testLoadRoutes(): void
  339. {
  340. $url = ['controller' => 'Articles', 'action' => 'index'];
  341. try {
  342. Router::url($url);
  343. $this->fail('Missing URL should throw an exception');
  344. } catch (MissingRouteException $e) {
  345. }
  346. Configure::write('App.namespace', 'TestApp');
  347. $this->loadRoutes();
  348. $result = Router::url($url);
  349. $this->assertSame('/app/articles', $result);
  350. }
  351. }