ControllerTestCaseTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. * Tests testAction with call to request::method()
  194. *
  195. * @return void
  196. */
  197. public function testTestActionWithRequestMethod() {
  198. $Controller = $this->Case->generate('TestsApps');
  199. $this->Case->testAction('/tests_apps/index', [
  200. 'method' => 'get'
  201. ]);
  202. $this->assertSame('GET', $this->Case->controller->request->method());
  203. $this->Case->testAction('/tests_apps/set_action', [
  204. 'method' => 'post'
  205. ]);
  206. $this->assertSame('POST', $this->Case->controller->request->method());
  207. }
  208. /**
  209. * Test testAction() with prefix routes.
  210. *
  211. * @return void
  212. */
  213. public function testActionWithPrefix() {
  214. $result = $this->Case->testAction('/admin/posts/index', ['return' => 'view']);
  215. $expected = '<h1>Admin Post Index</h1>';
  216. $this->assertContains($expected, $result);
  217. $result = $this->Case->testAction('/admin/test_plugin/comments/index', ['return' => 'view']);
  218. $expected = '<h1>TestPlugin Admin Comments</h1>';
  219. $this->assertContains($expected, $result);
  220. }
  221. /**
  222. * Make sure testAction() can hit plugin controllers.
  223. *
  224. * @return void
  225. */
  226. public function testTestActionWithPlugin() {
  227. $this->Case->generate('TestPlugin.Tests');
  228. $this->Case->testAction('/test_plugin/tests/index');
  229. $this->assertEquals('It is a variable', $this->Case->controller->viewVars['test_value']);
  230. }
  231. /**
  232. * Tests not using loaded routes during tests
  233. *
  234. * @expectedException \Cake\Controller\Exception\MissingActionException
  235. * @return void
  236. */
  237. public function testSkipRoutes() {
  238. $this->Case->loadRoutes = false;
  239. $this->Case->testAction('/tests_apps/missing_action.json', array('return' => 'view'));
  240. }
  241. /**
  242. * Tests backwards compatibility with setting the return type
  243. *
  244. * @return void
  245. */
  246. public function testBCSetReturn() {
  247. $this->Case->autoMock = true;
  248. $result = $this->Case->testAction('/tests_apps/some_method');
  249. $this->assertEquals(5, $result);
  250. $data = array('var' => 'set');
  251. $result = $this->Case->testAction('/tests_apps_posts/post_var', array(
  252. 'method' => 'post',
  253. 'data' => $data,
  254. 'return' => 'vars'
  255. ));
  256. $this->assertEquals($data, $result['data']);
  257. $result = $this->Case->testAction('/tests_apps/set_action', array(
  258. 'return' => 'view'
  259. ));
  260. $this->assertEquals('This is the TestsAppsController index view string', $result);
  261. $result = $this->Case->testAction('/tests_apps/set_action', array(
  262. 'return' => 'contents'
  263. ));
  264. $this->assertRegExp('/<html/', $result);
  265. $this->assertRegExp('/This is the TestsAppsController index view/', $result);
  266. $this->assertRegExp('/<\/html>/', $result);
  267. }
  268. /**
  269. * Tests sending POST data to testAction
  270. *
  271. * @return void
  272. */
  273. public function testTestActionPostData() {
  274. $this->Case->autoMock = true;
  275. $data = array(
  276. 'name' => 'Some Post'
  277. );
  278. $this->Case->testAction('/tests_apps_posts/post_var', array(
  279. 'method' => 'post',
  280. 'data' => $data
  281. ));
  282. $this->assertEquals($this->Case->controller->viewVars['data'], $data);
  283. $this->assertEquals($this->Case->controller->request->data, $data);
  284. $result = $this->Case->testAction('/tests_apps_posts/post_var', array(
  285. 'return' => 'vars',
  286. 'method' => 'post',
  287. 'data' => array(
  288. 'name' => 'is jonas',
  289. 'pork' => 'and beans',
  290. )
  291. ));
  292. $this->assertEquals(array('name', 'pork'), array_keys($result['data']));
  293. $result = $this->Case->testAction('/tests_apps_posts/add', array(
  294. 'method' => 'post',
  295. 'return' => 'vars'
  296. ));
  297. $this->assertTrue(array_key_exists('posts', $result));
  298. $this->assertInstanceOf('Cake\ORM\Query', $result['posts']);
  299. $this->assertTrue($this->Case->controller->request->is('post'));
  300. }
  301. /**
  302. * Tests sending GET data to testAction
  303. *
  304. * @return void
  305. */
  306. public function testTestActionGetData() {
  307. $this->Case->autoMock = true;
  308. $result = $this->Case->testAction('/tests_apps_posts/url_var', array(
  309. 'method' => 'get',
  310. 'data' => array(
  311. 'some' => 'var',
  312. 'lackof' => 'creativity'
  313. )
  314. ));
  315. $this->assertEquals('var', $this->Case->controller->request->query['some']);
  316. $this->assertEquals('creativity', $this->Case->controller->request->query['lackof']);
  317. $result = $this->Case->testAction('/tests_apps_posts/url_var/gogo/val2', array(
  318. 'return' => 'vars',
  319. 'method' => 'get',
  320. ));
  321. $this->assertEquals(array('gogo', 'val2'), $result['params']['pass']);
  322. $result = $this->Case->testAction('/tests_apps_posts/url_var', array(
  323. 'return' => 'vars',
  324. 'method' => 'get',
  325. 'data' => array(
  326. 'red' => 'health',
  327. 'blue' => 'mana'
  328. )
  329. ));
  330. $query = $this->Case->controller->request->query;
  331. $this->assertTrue(isset($query['red']));
  332. $this->assertTrue(isset($query['blue']));
  333. }
  334. /**
  335. * Test that REST actions with XML/JSON input work.
  336. *
  337. * @return void
  338. */
  339. public function testTestActionJsonData() {
  340. $result = $this->Case->testAction('/tests_apps_posts/input_data', array(
  341. 'return' => 'vars',
  342. 'method' => 'post',
  343. 'data' => '{"key":"value","json":true}'
  344. ));
  345. $this->assertEquals('value', $result['data']['key']);
  346. $this->assertTrue($result['data']['json']);
  347. }
  348. /**
  349. * Tests autoMock ability
  350. *
  351. * @return void
  352. */
  353. public function testAutoMock() {
  354. $this->Case->autoMock = true;
  355. $this->Case->testAction('/tests_apps/set_action');
  356. $results = $this->Case->controller->viewVars;
  357. $expected = array(
  358. 'var' => 'string'
  359. );
  360. $this->assertEquals($expected, $results);
  361. }
  362. /**
  363. * Test using testAction and not mocking
  364. *
  365. * @return void
  366. */
  367. public function testNoMocking() {
  368. $result = $this->Case->testAction('/tests_apps/some_method');
  369. $this->Case->assertEquals(5, $result);
  370. $data = array('var' => 'set');
  371. $result = $this->Case->testAction('/tests_apps_posts/post_var', array(
  372. 'method' => 'post',
  373. 'data' => $data,
  374. 'return' => 'vars'
  375. ));
  376. $this->assertEquals($data, $result['data']);
  377. $result = $this->Case->testAction('/tests_apps/set_action', array(
  378. 'return' => 'view'
  379. ));
  380. $this->assertEquals('This is the TestsAppsController index view string', $result);
  381. $result = $this->Case->testAction('/tests_apps/set_action', array(
  382. 'return' => 'contents'
  383. ));
  384. $this->assertRegExp('/<html/', $result);
  385. $this->assertRegExp('/This is the TestsAppsController index view/', $result);
  386. $this->assertRegExp('/<\/html>/', $result);
  387. }
  388. /**
  389. * Test that controllers don't get reused.
  390. *
  391. * @return void
  392. */
  393. public function testNoControllerReuse() {
  394. $this->Case->autoMock = true;
  395. $result = $this->Case->testAction('/tests_apps/index', array(
  396. 'data' => array('var' => 'first call'),
  397. 'method' => 'get',
  398. 'return' => 'contents',
  399. ));
  400. $this->assertContains('<html', $result);
  401. $this->assertContains('This is the TestsAppsController index view', $result);
  402. $this->assertContains('first call', $result);
  403. $this->assertContains('</html>', $result);
  404. $result = $this->Case->testAction('/tests_apps/index', array(
  405. 'data' => array('var' => 'second call'),
  406. 'method' => 'get',
  407. 'return' => 'contents'
  408. ));
  409. $this->assertContains('second call', $result);
  410. $result = $this->Case->testAction('/tests_apps/index', array(
  411. 'data' => array('var' => 'third call'),
  412. 'method' => 'get',
  413. 'return' => 'contents'
  414. ));
  415. $this->assertContains('third call', $result);
  416. }
  417. /**
  418. * Test that multiple calls to redirect in the same test method don't cause issues.
  419. * - Asserting true, to avoid test to be shown as "Risky".
  420. *
  421. * @return void
  422. */
  423. public function testTestActionWithMultipleRedirect() {
  424. $this->Case->generate('TestsApps');
  425. $options = array('method' => 'get');
  426. $this->Case->testAction('/tests_apps/redirect_to', $options);
  427. $this->Case->testAction('/tests_apps/redirect_to', $options);
  428. $this->assertTrue(true);
  429. }
  430. /**
  431. * Tests that Components storing response or request objects internally during construct
  432. * will always have a fresh reference to those object available
  433. *
  434. * @return void
  435. * @see https://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/2705-requesthandler-weird-behavior
  436. */
  437. public function testComponentsSameRequestAndResponse() {
  438. $this->Case->generate('TestsApps');
  439. $options = array('method' => 'get');
  440. $this->Case->testAction('/tests_apps/index', $options);
  441. $this->assertSame($this->Case->controller->response, $this->Case->controller->RequestHandler->response);
  442. $this->assertSame($this->Case->controller->request, $this->Case->controller->RequestHandler->request);
  443. }
  444. /**
  445. * Test that testAction() doesn't destroy data in GET & POST
  446. *
  447. * @return void
  448. */
  449. public function testRestoreGetPost() {
  450. $restored = array('new' => 'value');
  451. $_GET = $restored;
  452. $_POST = $restored;
  453. $this->Case->generate('TestsApps');
  454. $options = array('method' => 'get');
  455. $this->Case->testAction('/tests_apps/index', $options);
  456. $this->assertEquals($restored, $_GET);
  457. $this->assertEquals($restored, $_POST);
  458. }
  459. }