ControllerTestCaseTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. <?php
  2. /**
  3. * CakePHP : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP Project
  12. * @since 2.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\TestSuite;
  16. use Cake\Controller\Controller;
  17. use Cake\Core\App;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\ORM\Table;
  21. use Cake\ORM\TableRegistry;
  22. use Cake\Routing\DispatcherFactory;
  23. use Cake\Routing\Router;
  24. use Cake\TestSuite\Reporter\HtmlReporter;
  25. use Cake\TestSuite\TestCase;
  26. /**
  27. * ControllerTestCaseTest
  28. *
  29. */
  30. class ControllerTestCaseTest extends TestCase {
  31. /**
  32. * fixtures property
  33. *
  34. * @var array
  35. */
  36. public $fixtures = array('core.post', 'core.author', 'core.test_plugin_comment');
  37. /**
  38. * reset environment.
  39. *
  40. * @return void
  41. */
  42. public function setUp() {
  43. parent::setUp();
  44. Configure::write('App.namespace', 'TestApp');
  45. Plugin::load(array('TestPlugin', 'TestPluginTwo'));
  46. $this->Case = $this->getMockForAbstractClass('Cake\TestSuite\ControllerTestCase');
  47. DispatcherFactory::add('Routing');
  48. DispatcherFactory::add('ControllerFactory');
  49. Router::reload();
  50. require CAKE . 'Config/routes.php';
  51. TableRegistry::clear();
  52. }
  53. /**
  54. * tearDown
  55. *
  56. * @return void
  57. */
  58. public function tearDown() {
  59. parent::tearDown();
  60. Plugin::unload();
  61. DispatcherFactory::clear();
  62. $this->Case->controller = null;
  63. }
  64. /**
  65. * Test that ControllerTestCase::generate() creates mock objects correctly
  66. *
  67. * @return void
  68. */
  69. public function testGenerate() {
  70. $Posts = $this->Case->generate('TestApp\Controller\PostsController');
  71. $this->assertEquals('Posts', $Posts->name);
  72. $this->assertEquals('Posts', $Posts->modelClass);
  73. $this->assertEquals('Posts', $Posts->viewPath);
  74. $this->assertNull($Posts->response->send());
  75. $Posts = $this->Case->generate('TestApp\Controller\PostsController', array(
  76. 'methods' => array(
  77. 'render'
  78. )
  79. ));
  80. $this->assertNull($Posts->render('index'));
  81. $Posts = $this->Case->generate('TestApp\Controller\PostsController', array(
  82. 'models' => array('Posts'),
  83. 'components' => array('RequestHandler')
  84. ));
  85. $this->assertInstanceOf('TestApp\Model\Table\PostsTable', $Posts->Posts);
  86. $this->assertNull($Posts->Posts->deleteAll(array()));
  87. $this->assertNull($Posts->Posts->find('all'));
  88. $this->assertNull($Posts->RequestHandler->isXml());
  89. $Posts = $this->Case->generate('TestApp\Controller\PostsController', array(
  90. 'models' => array(
  91. 'Posts' => true
  92. )
  93. ));
  94. $this->assertNull($Posts->Posts->deleteAll([]));
  95. $this->assertNull($Posts->Posts->find('all'));
  96. $Posts = $this->Case->generate('TestApp\Controller\PostsController', array(
  97. 'models' => array(
  98. 'Posts' => array('deleteAll'),
  99. )
  100. ));
  101. $this->assertNull($Posts->Posts->deleteAll([]));
  102. $this->assertEquals('posts', $Posts->Posts->table());
  103. $Posts = $this->Case->generate('TestApp\Controller\PostsController', array(
  104. 'models' => array('Posts'),
  105. 'components' => array(
  106. 'RequestHandler' => array('isPut'),
  107. )
  108. ));
  109. $Posts->RequestHandler->expects($this->once())
  110. ->method('isPut')
  111. ->will($this->returnValue(true));
  112. $this->assertTrue($Posts->RequestHandler->isPut());
  113. }
  114. /**
  115. * testGenerateWithComponentConfig
  116. *
  117. * @return void
  118. */
  119. public function testGenerateWithComponentConfig() {
  120. $Tests = $this->Case->generate('TestConfigs', array(
  121. ));
  122. $expected = array('some' => 'config');
  123. $settings = array_intersect_key($Tests->RequestHandler->config(), array('some' => 'foo'));
  124. $this->assertSame($expected, $settings, 'A mocked component should have the same config as an unmocked component');
  125. $Tests = $this->Case->generate('TestConfigs', array(
  126. 'components' => array(
  127. 'RequestHandler' => array('isPut')
  128. )
  129. ));
  130. $expected = array('some' => 'config');
  131. $settings = array_intersect_key($Tests->RequestHandler->config(), array('some' => 'foo'));
  132. $this->assertSame($expected, $settings, 'A mocked component should have the same config as an unmocked component');
  133. }
  134. /**
  135. * Tests ControllerTestCase::generate() using classes from plugins
  136. *
  137. * @return void
  138. */
  139. public function testGenerateWithPlugin() {
  140. $Tests = $this->Case->generate('TestPlugin.Tests', array(
  141. 'models' => array(
  142. 'TestPlugin.TestPluginComments'
  143. ),
  144. 'components' => array(
  145. 'TestPlugin.Plugins'
  146. )
  147. ));
  148. $this->assertEquals('Tests', $Tests->name);
  149. $this->assertInstanceOf('TestPlugin\Controller\Component\PluginsComponent', $Tests->Plugins);
  150. $result = TableRegistry::get('TestPlugin.TestPluginComments');
  151. $this->assertInstanceOf('TestPlugin\Model\Table\TestPluginCommentsTable', $result);
  152. }
  153. /**
  154. * Tests testAction
  155. *
  156. * @return void
  157. */
  158. public function testTestAction() {
  159. $Controller = $this->Case->generate('TestsApps');
  160. $this->Case->testAction('/tests_apps/index');
  161. $this->assertInternalType('array', $this->Case->controller->viewVars);
  162. $this->Case->testAction('/tests_apps/set_action');
  163. $results = $this->Case->controller->viewVars;
  164. $expected = array(
  165. 'var' => 'string'
  166. );
  167. $this->assertEquals($expected, $results);
  168. $result = $this->Case->controller->response->body();
  169. $this->assertRegExp('/This is the TestsAppsController index view/', $result);
  170. $Controller = $this->Case->generate('TestsApps');
  171. $this->Case->testAction('/tests_apps/redirect_to');
  172. $results = $this->Case->headers;
  173. $expected = array(
  174. 'Location' => 'http://cakephp.org'
  175. );
  176. $this->assertEquals($expected, $results);
  177. }
  178. /**
  179. * Test testAction() with prefix routes.
  180. *
  181. * @return void
  182. */
  183. public function testActionWithPrefix() {
  184. Configure::write('Routing.prefixes', ['admin']);
  185. Plugin::load('TestPlugin');
  186. $result = $this->Case->testAction('/admin/posts/index', ['return' => 'view']);
  187. $expected = '<h1>Admin Post Index</h1>';
  188. $this->assertContains($expected, $result);
  189. $result = $this->Case->testAction('/admin/test_plugin/comments/index', ['return' => 'view']);
  190. $expected = '<h1>TestPlugin Admin Comments</h1>';
  191. $this->assertContains($expected, $result);
  192. }
  193. /**
  194. * Make sure testAction() can hit plugin controllers.
  195. *
  196. * @return void
  197. */
  198. public function testTestActionWithPlugin() {
  199. $this->Case->generate('TestPlugin.Tests');
  200. $this->Case->testAction('/test_plugin/tests/index');
  201. $this->assertEquals('It is a variable', $this->Case->controller->viewVars['test_value']);
  202. }
  203. /**
  204. * Tests not using loaded routes during tests
  205. *
  206. * @expectedException \Cake\Controller\Error\MissingActionException
  207. * @return void
  208. */
  209. public function testSkipRoutes() {
  210. $this->Case->loadRoutes = false;
  211. $this->Case->testAction('/tests_apps/missing_action.json', array('return' => 'view'));
  212. }
  213. /**
  214. * Tests backwards compatibility with setting the return type
  215. *
  216. * @return void
  217. */
  218. public function testBCSetReturn() {
  219. $this->Case->autoMock = true;
  220. $result = $this->Case->testAction('/tests_apps/some_method');
  221. $this->assertEquals(5, $result);
  222. $data = array('var' => 'set');
  223. $result = $this->Case->testAction('/tests_apps_posts/post_var', array(
  224. 'method' => 'post',
  225. 'data' => $data,
  226. 'return' => 'vars'
  227. ));
  228. $this->assertEquals($data, $result['data']);
  229. $result = $this->Case->testAction('/tests_apps/set_action', array(
  230. 'return' => 'view'
  231. ));
  232. $this->assertEquals('This is the TestsAppsController index view string', $result);
  233. $result = $this->Case->testAction('/tests_apps/set_action', array(
  234. 'return' => 'contents'
  235. ));
  236. $this->assertRegExp('/<html/', $result);
  237. $this->assertRegExp('/This is the TestsAppsController index view/', $result);
  238. $this->assertRegExp('/<\/html>/', $result);
  239. }
  240. /**
  241. * Tests sending POST data to testAction
  242. *
  243. * @return void
  244. */
  245. public function testTestActionPostData() {
  246. $this->Case->autoMock = true;
  247. $data = array(
  248. 'name' => 'Some Post'
  249. );
  250. $this->Case->testAction('/tests_apps_posts/post_var', array(
  251. 'method' => 'post',
  252. 'data' => $data
  253. ));
  254. $this->assertEquals($this->Case->controller->viewVars['data'], $data);
  255. $this->assertEquals($this->Case->controller->request->data, $data);
  256. $result = $this->Case->testAction('/tests_apps_posts/post_var', array(
  257. 'return' => 'vars',
  258. 'method' => 'post',
  259. 'data' => array(
  260. 'name' => 'is jonas',
  261. 'pork' => 'and beans',
  262. )
  263. ));
  264. $this->assertEquals(array('name', 'pork'), array_keys($result['data']));
  265. $result = $this->Case->testAction('/tests_apps_posts/add', array(
  266. 'method' => 'post',
  267. 'return' => 'vars'
  268. ));
  269. $this->assertTrue(array_key_exists('posts', $result));
  270. $this->assertInstanceOf('Cake\ORM\Query', $result['posts']);
  271. $this->assertTrue($this->Case->controller->request->is('post'));
  272. }
  273. /**
  274. * Tests sending GET data to testAction
  275. *
  276. * @return void
  277. */
  278. public function testTestActionGetData() {
  279. $this->Case->autoMock = true;
  280. $result = $this->Case->testAction('/tests_apps_posts/url_var', array(
  281. 'method' => 'get',
  282. 'data' => array(
  283. 'some' => 'var',
  284. 'lackof' => 'creativity'
  285. )
  286. ));
  287. $this->assertEquals('var', $this->Case->controller->request->query['some']);
  288. $this->assertEquals('creativity', $this->Case->controller->request->query['lackof']);
  289. $result = $this->Case->testAction('/tests_apps_posts/url_var/gogo/val2', array(
  290. 'return' => 'vars',
  291. 'method' => 'get',
  292. ));
  293. $this->assertEquals(array('gogo', 'val2'), $result['params']['pass']);
  294. $result = $this->Case->testAction('/tests_apps_posts/url_var', array(
  295. 'return' => 'vars',
  296. 'method' => 'get',
  297. 'data' => array(
  298. 'red' => 'health',
  299. 'blue' => 'mana'
  300. )
  301. ));
  302. $query = $this->Case->controller->request->query;
  303. $this->assertTrue(isset($query['red']));
  304. $this->assertTrue(isset($query['blue']));
  305. }
  306. /**
  307. * Test that REST actions with XML/JSON input work.
  308. *
  309. * @return void
  310. */
  311. public function testTestActionJsonData() {
  312. $result = $this->Case->testAction('/tests_apps_posts/input_data', array(
  313. 'return' => 'vars',
  314. 'method' => 'post',
  315. 'data' => '{"key":"value","json":true}'
  316. ));
  317. $this->assertEquals('value', $result['data']['key']);
  318. $this->assertTrue($result['data']['json']);
  319. }
  320. /**
  321. * Tests autoMock ability
  322. *
  323. * @return void
  324. */
  325. public function testAutoMock() {
  326. $this->Case->autoMock = true;
  327. $this->Case->testAction('/tests_apps/set_action');
  328. $results = $this->Case->controller->viewVars;
  329. $expected = array(
  330. 'var' => 'string'
  331. );
  332. $this->assertEquals($expected, $results);
  333. }
  334. /**
  335. * Test using testAction and not mocking
  336. *
  337. * @return void
  338. */
  339. public function testNoMocking() {
  340. $result = $this->Case->testAction('/tests_apps/some_method');
  341. $this->Case->assertEquals(5, $result);
  342. $data = array('var' => 'set');
  343. $result = $this->Case->testAction('/tests_apps_posts/post_var', array(
  344. 'method' => 'post',
  345. 'data' => $data,
  346. 'return' => 'vars'
  347. ));
  348. $this->assertEquals($data, $result['data']);
  349. $result = $this->Case->testAction('/tests_apps/set_action', array(
  350. 'return' => 'view'
  351. ));
  352. $this->assertEquals('This is the TestsAppsController index view string', $result);
  353. $result = $this->Case->testAction('/tests_apps/set_action', array(
  354. 'return' => 'contents'
  355. ));
  356. $this->assertRegExp('/<html/', $result);
  357. $this->assertRegExp('/This is the TestsAppsController index view/', $result);
  358. $this->assertRegExp('/<\/html>/', $result);
  359. }
  360. /**
  361. * Test that controllers don't get reused.
  362. *
  363. * @return void
  364. */
  365. public function testNoControllerReuse() {
  366. $this->Case->autoMock = true;
  367. $result = $this->Case->testAction('/tests_apps/index', array(
  368. 'data' => array('var' => 'first call'),
  369. 'method' => 'get',
  370. 'return' => 'contents',
  371. ));
  372. $this->assertContains('<html', $result);
  373. $this->assertContains('This is the TestsAppsController index view', $result);
  374. $this->assertContains('first call', $result);
  375. $this->assertContains('</html>', $result);
  376. $result = $this->Case->testAction('/tests_apps/index', array(
  377. 'data' => array('var' => 'second call'),
  378. 'method' => 'get',
  379. 'return' => 'contents'
  380. ));
  381. $this->assertContains('second call', $result);
  382. $result = $this->Case->testAction('/tests_apps/index', array(
  383. 'data' => array('var' => 'third call'),
  384. 'method' => 'get',
  385. 'return' => 'contents'
  386. ));
  387. $this->assertContains('third call', $result);
  388. }
  389. /**
  390. * Test that multiple calls to redirect in the same test method don't cause issues.
  391. *
  392. * @return void
  393. */
  394. public function testTestActionWithMultipleRedirect() {
  395. $this->Case->generate('TestsApps');
  396. $options = array('method' => 'get');
  397. $this->Case->testAction('/tests_apps/redirect_to', $options);
  398. $this->Case->testAction('/tests_apps/redirect_to', $options);
  399. }
  400. /**
  401. * Tests that Components storing response or request objects internally during construct
  402. * will always have a fresh reference to those object available
  403. *
  404. * @return void
  405. * @see https://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/2705-requesthandler-weird-behavior
  406. */
  407. public function testComponentsSameRequestAndResponse() {
  408. $this->Case->generate('TestsApps');
  409. $options = array('method' => 'get');
  410. $this->Case->testAction('/tests_apps/index', $options);
  411. $this->assertSame($this->Case->controller->response, $this->Case->controller->RequestHandler->response);
  412. $this->assertSame($this->Case->controller->request, $this->Case->controller->RequestHandler->request);
  413. }
  414. /**
  415. * Test that testAction() doesn't destroy data in GET & POST
  416. *
  417. * @return void
  418. */
  419. public function testRestoreGetPost() {
  420. $restored = array('new' => 'value');
  421. $_GET = $restored;
  422. $_POST = $restored;
  423. $this->Case->generate('TestsApps');
  424. $options = array('method' => 'get');
  425. $this->Case->testAction('/tests_apps/index', $options);
  426. $this->assertEquals($restored, $_GET);
  427. $this->assertEquals($restored, $_POST);
  428. }
  429. }