ControllerTestCaseTest.php 14 KB

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