ControllerTestCaseTest.php 14 KB

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