ControllerTestCaseTest.php 14 KB

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