ControllerTestCaseTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. <?php
  2. /**
  3. * ControllerTestCaseTest file
  4. *
  5. * Test Case for ControllerTestCase class
  6. *
  7. * PHP 5
  8. *
  9. * CakePHP : Rapid Development Framework (http://cakephp.org)
  10. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * For full copyright and license information, please see the LICENSE.txt
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. * @link http://cakephp.org CakePHP Project
  18. * @package Cake.Test.Case.TestSuite
  19. * @since CakePHP v 2.0
  20. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  21. */
  22. App::uses('Controller', 'Controller');
  23. App::uses('Model', 'Model');
  24. App::uses('AppModel', 'Model');
  25. App::uses('CakeHtmlReporter', 'TestSuite/Reporter');
  26. require_once dirname(dirname(__FILE__)) . DS . 'Model' . DS . 'models.php';
  27. if (!class_exists('AppController', false)) {
  28. /**
  29. * AppController class
  30. *
  31. * @package Cake.Test.Case.TestSuite
  32. */
  33. class AppController extends Controller {
  34. /**
  35. * helpers property
  36. *
  37. * @var array
  38. * @access public
  39. */
  40. public $helpers = array('Html');
  41. /**
  42. * uses property
  43. *
  44. * @var array
  45. * @access public
  46. */
  47. public $uses = array('ControllerPost');
  48. /**
  49. * components property
  50. *
  51. * @var array
  52. * @access public
  53. */
  54. public $components = array('Cookie');
  55. }
  56. } elseif (!defined('APP_CONTROLLER_EXISTS')) {
  57. define('APP_CONTROLLER_EXISTS', true);
  58. }
  59. /**
  60. * PostsController class
  61. */
  62. if (!class_exists('PostsController')) {
  63. /**
  64. * Class PostsController
  65. *
  66. * @package Cake.Test.Case.TestSuite
  67. */
  68. class PostsController extends AppController {
  69. /**
  70. * Components array
  71. *
  72. * @var array
  73. */
  74. public $components = array(
  75. 'RequestHandler',
  76. 'Email',
  77. 'Auth'
  78. );
  79. }
  80. }
  81. /**
  82. * ControllerTestCaseTest controller
  83. *
  84. * @package Cake.Test.Case.TestSuite
  85. */
  86. class ControllerTestCaseTestController extends AppController {
  87. /**
  88. * Uses array
  89. *
  90. * @param array
  91. */
  92. public $uses = array('TestPlugin.TestPluginComment');
  93. }
  94. /**
  95. * ControllerTestCaseTest
  96. *
  97. * @package Cake.Test.Case.TestSuite
  98. */
  99. class ControllerTestCaseTest extends CakeTestCase {
  100. /**
  101. * fixtures property
  102. *
  103. * @var array
  104. */
  105. public $fixtures = array('core.post', 'core.author', 'core.test_plugin_comment');
  106. /**
  107. * reset environment.
  108. *
  109. * @return void
  110. */
  111. public function setUp() {
  112. parent::setUp();
  113. App::build(array(
  114. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
  115. 'Controller' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Controller' . DS),
  116. 'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
  117. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
  118. ), App::RESET);
  119. CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
  120. $this->Case = $this->getMockForAbstractClass('ControllerTestCase');
  121. Router::reload();
  122. }
  123. /**
  124. * tearDown
  125. *
  126. * @return void
  127. */
  128. public function tearDown() {
  129. parent::tearDown();
  130. CakePlugin::unload();
  131. $this->Case->controller = null;
  132. }
  133. /**
  134. * Test that ControllerTestCase::generate() creates mock objects correctly
  135. */
  136. public function testGenerate() {
  137. if (defined('APP_CONTROLLER_EXISTS')) {
  138. $this->markTestSkipped('AppController exists, cannot run.');
  139. }
  140. $Posts = $this->Case->generate('Posts');
  141. $this->assertEquals('Posts', $Posts->name);
  142. $this->assertEquals('Post', $Posts->modelClass);
  143. $this->assertNull($Posts->response->send());
  144. $Posts = $this->Case->generate('Posts', array(
  145. 'methods' => array(
  146. 'render'
  147. )
  148. ));
  149. $this->assertNull($Posts->render('index'));
  150. $Posts = $this->Case->generate('Posts', array(
  151. 'models' => array('Post'),
  152. 'components' => array('RequestHandler')
  153. ));
  154. $this->assertInstanceOf('Post', $Posts->Post);
  155. $this->assertNull($Posts->Post->save(array()));
  156. $this->assertNull($Posts->Post->find('all'));
  157. $this->assertEquals('posts', $Posts->Post->useTable);
  158. $this->assertNull($Posts->RequestHandler->isAjax());
  159. $Posts = $this->Case->generate('Posts', array(
  160. 'models' => array(
  161. 'Post' => true
  162. )
  163. ));
  164. $this->assertNull($Posts->Post->save(array()));
  165. $this->assertNull($Posts->Post->find('all'));
  166. $Posts = $this->Case->generate('Posts', array(
  167. 'models' => array(
  168. 'Post' => array('save'),
  169. )
  170. ));
  171. $this->assertNull($Posts->Post->save(array()));
  172. $this->assertInternalType('array', $Posts->Post->find('all'));
  173. $Posts = $this->Case->generate('Posts', array(
  174. 'models' => array('Post'),
  175. 'components' => array(
  176. 'RequestHandler' => array('isPut'),
  177. 'Email' => array('send'),
  178. 'Session'
  179. )
  180. ));
  181. $Posts->RequestHandler->expects($this->once())
  182. ->method('isPut')
  183. ->will($this->returnValue(true));
  184. $this->assertTrue($Posts->RequestHandler->isPut());
  185. $Posts->Auth->Session->expects($this->any())
  186. ->method('write')
  187. ->will($this->returnValue('written!'));
  188. $this->assertEquals('written!', $Posts->Auth->Session->write('something'));
  189. }
  190. /**
  191. * Tests ControllerTestCase::generate() using classes from plugins
  192. */
  193. public function testGenerateWithPlugin() {
  194. $Tests = $this->Case->generate('TestPlugin.Tests', array(
  195. 'models' => array(
  196. 'TestPlugin.TestPluginComment'
  197. ),
  198. 'components' => array(
  199. 'TestPlugin.Plugins'
  200. )
  201. ));
  202. $this->assertEquals('Tests', $Tests->name);
  203. $this->assertInstanceOf('PluginsComponent', $Tests->Plugins);
  204. $result = ClassRegistry::init('TestPlugin.TestPluginComment');
  205. $this->assertInstanceOf('TestPluginComment', $result);
  206. $Tests = $this->Case->generate('ControllerTestCaseTest', array(
  207. 'models' => array(
  208. 'TestPlugin.TestPluginComment' => array('save')
  209. )
  210. ));
  211. $this->assertInstanceOf('TestPluginComment', $Tests->TestPluginComment);
  212. $Tests->TestPluginComment->expects($this->at(0))
  213. ->method('save')
  214. ->will($this->returnValue(true));
  215. $Tests->TestPluginComment->expects($this->at(1))
  216. ->method('save')
  217. ->will($this->returnValue(false));
  218. $this->assertTrue($Tests->TestPluginComment->save(array()));
  219. $this->assertFalse($Tests->TestPluginComment->save(array()));
  220. }
  221. /**
  222. * Tests testAction
  223. */
  224. public function testTestAction() {
  225. $Controller = $this->Case->generate('TestsApps');
  226. $this->Case->testAction('/tests_apps/index');
  227. $this->assertInternalType('array', $this->Case->controller->viewVars);
  228. $this->Case->testAction('/tests_apps/set_action');
  229. $results = $this->Case->controller->viewVars;
  230. $expected = array(
  231. 'var' => 'string'
  232. );
  233. $this->assertEquals($expected, $results);
  234. $result = $this->Case->controller->response->body();
  235. $this->assertRegExp('/This is the TestsAppsController index view/', $result);
  236. $Controller = $this->Case->generate('TestsApps');
  237. $this->Case->testAction('/tests_apps/redirect_to');
  238. $results = $this->Case->headers;
  239. $expected = array(
  240. 'Location' => 'http://cakephp.org'
  241. );
  242. $this->assertEquals($expected, $results);
  243. }
  244. /**
  245. * Make sure testAction() can hit plugin controllers.
  246. *
  247. * @return void
  248. */
  249. public function testTestActionWithPlugin() {
  250. $this->Case->generate('TestPlugin.Tests');
  251. $this->Case->testAction('/test_plugin/tests/index');
  252. $this->assertEquals('It is a variable', $this->Case->controller->viewVars['test_value']);
  253. }
  254. /**
  255. * Tests using loaded routes during tests
  256. *
  257. * @return void
  258. */
  259. public function testUseRoutes() {
  260. Router::connect('/:controller/:action/*');
  261. include CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS . 'routes.php';
  262. $controller = $this->Case->generate('TestsApps');
  263. $controller->Components->load('RequestHandler');
  264. $result = $this->Case->testAction('/tests_apps/index.json', array('return' => 'contents'));
  265. $result = json_decode($result, true);
  266. $expected = array('cakephp' => 'cool');
  267. $this->assertEquals($expected, $result);
  268. include CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS . 'routes.php';
  269. $result = $this->Case->testAction('/some_alias');
  270. $this->assertEquals(5, $result);
  271. }
  272. /**
  273. * Tests not using loaded routes during tests
  274. *
  275. * @expectedException MissingActionException
  276. */
  277. public function testSkipRoutes() {
  278. Router::connect('/:controller/:action/*');
  279. include CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS . 'routes.php';
  280. $this->Case->loadRoutes = false;
  281. $this->Case->testAction('/tests_apps/missing_action.json', array('return' => 'view'));
  282. }
  283. /**
  284. * Tests backwards compatibility with setting the return type
  285. */
  286. public function testBCSetReturn() {
  287. $this->Case->autoMock = true;
  288. $result = $this->Case->testAction('/tests_apps/some_method');
  289. $this->assertEquals(5, $result);
  290. $data = array('var' => 'set');
  291. $result = $this->Case->testAction('/tests_apps_posts/post_var', array(
  292. 'data' => $data,
  293. 'return' => 'vars'
  294. ));
  295. $this->assertEquals($data, $result['data']);
  296. $result = $this->Case->testAction('/tests_apps/set_action', array(
  297. 'return' => 'view'
  298. ));
  299. $this->assertEquals('This is the TestsAppsController index view string', $result);
  300. $result = $this->Case->testAction('/tests_apps/set_action', array(
  301. 'return' => 'contents'
  302. ));
  303. $this->assertRegExp('/<html/', $result);
  304. $this->assertRegExp('/This is the TestsAppsController index view/', $result);
  305. $this->assertRegExp('/<\/html>/', $result);
  306. }
  307. /**
  308. * Tests sending POST data to testAction
  309. */
  310. public function testTestActionPostData() {
  311. $this->Case->autoMock = true;
  312. $data = array(
  313. 'Post' => array(
  314. 'name' => 'Some Post'
  315. )
  316. );
  317. $this->Case->testAction('/tests_apps_posts/post_var', array(
  318. 'data' => $data
  319. ));
  320. $this->assertEquals($this->Case->controller->viewVars['data'], $data);
  321. $this->assertEquals($this->Case->controller->data, $data);
  322. $this->Case->testAction('/tests_apps_posts/post_var/named:param', array(
  323. 'data' => $data
  324. ));
  325. $expected = array(
  326. 'named' => 'param'
  327. );
  328. $this->assertEquals($expected, $this->Case->controller->request->named);
  329. $this->assertEquals($this->Case->controller->data, $data);
  330. $result = $this->Case->testAction('/tests_apps_posts/post_var', array(
  331. 'return' => 'vars',
  332. 'method' => 'post',
  333. 'data' => array(
  334. 'name' => 'is jonas',
  335. 'pork' => 'and beans',
  336. )
  337. ));
  338. $this->assertEquals(array('name', 'pork'), array_keys($result['data']));
  339. $result = $this->Case->testAction('/tests_apps_posts/add', array('return' => 'vars'));
  340. $this->assertTrue(array_key_exists('posts', $result));
  341. $this->assertEquals(4, count($result['posts']));
  342. $this->assertTrue($this->Case->controller->request->is('post'));
  343. }
  344. /**
  345. * Tests sending GET data to testAction
  346. */
  347. public function testTestActionGetData() {
  348. $this->Case->autoMock = true;
  349. $result = $this->Case->testAction('/tests_apps_posts/url_var', array(
  350. 'method' => 'get',
  351. 'data' => array(
  352. 'some' => 'var',
  353. 'lackof' => 'creativity'
  354. )
  355. ));
  356. $this->assertEquals('var', $this->Case->controller->request->query['some']);
  357. $this->assertEquals('creativity', $this->Case->controller->request->query['lackof']);
  358. $result = $this->Case->testAction('/tests_apps_posts/url_var/var1:value1/var2:val2', array(
  359. 'return' => 'vars',
  360. 'method' => 'get',
  361. ));
  362. $this->assertEquals(array('var1', 'var2'), array_keys($result['params']['named']));
  363. $result = $this->Case->testAction('/tests_apps_posts/url_var/gogo/val2', array(
  364. 'return' => 'vars',
  365. 'method' => 'get',
  366. ));
  367. $this->assertEquals(array('gogo', 'val2'), $result['params']['pass']);
  368. $result = $this->Case->testAction('/tests_apps_posts/url_var', array(
  369. 'return' => 'vars',
  370. 'method' => 'get',
  371. 'data' => array(
  372. 'red' => 'health',
  373. 'blue' => 'mana'
  374. )
  375. ));
  376. $query = $this->Case->controller->request->query;
  377. $this->assertTrue(isset($query['red']));
  378. $this->assertTrue(isset($query['blue']));
  379. }
  380. /**
  381. * Test that REST actions with XML/JSON input work.
  382. *
  383. * @return void
  384. */
  385. public function testTestActionJsonData() {
  386. $result = $this->Case->testAction('/tests_apps_posts/input_data', array(
  387. 'return' => 'vars',
  388. 'method' => 'post',
  389. 'data' => '{"key":"value","json":true}'
  390. ));
  391. $this->assertEquals('value', $result['data']['key']);
  392. $this->assertTrue($result['data']['json']);
  393. }
  394. /**
  395. * Tests autoMock ability
  396. */
  397. public function testAutoMock() {
  398. $this->Case->autoMock = true;
  399. $this->Case->testAction('/tests_apps/set_action');
  400. $results = $this->Case->controller->viewVars;
  401. $expected = array(
  402. 'var' => 'string'
  403. );
  404. $this->assertEquals($expected, $results);
  405. }
  406. /**
  407. * Test using testAction and not mocking
  408. */
  409. public function testNoMocking() {
  410. $result = $this->Case->testAction('/tests_apps/some_method');
  411. $this->Case->assertEquals(5, $result);
  412. $data = array('var' => 'set');
  413. $result = $this->Case->testAction('/tests_apps_posts/post_var', array(
  414. 'data' => $data,
  415. 'return' => 'vars'
  416. ));
  417. $this->assertEquals($data, $result['data']);
  418. $result = $this->Case->testAction('/tests_apps/set_action', array(
  419. 'return' => 'view'
  420. ));
  421. $this->assertEquals('This is the TestsAppsController index view string', $result);
  422. $result = $this->Case->testAction('/tests_apps/set_action', array(
  423. 'return' => 'contents'
  424. ));
  425. $this->assertRegExp('/<html/', $result);
  426. $this->assertRegExp('/This is the TestsAppsController index view/', $result);
  427. $this->assertRegExp('/<\/html>/', $result);
  428. }
  429. /**
  430. * Test that controllers don't get reused.
  431. *
  432. * @return void
  433. */
  434. public function testNoControllerReuse() {
  435. $this->Case->autoMock = true;
  436. $result = $this->Case->testAction('/tests_apps/index', array(
  437. 'data' => array('var' => 'first call'),
  438. 'method' => 'get',
  439. 'return' => 'contents',
  440. ));
  441. $this->assertContains('<html', $result);
  442. $this->assertContains('This is the TestsAppsController index view', $result);
  443. $this->assertContains('first call', $result);
  444. $this->assertContains('</html>', $result);
  445. $result = $this->Case->testAction('/tests_apps/index', array(
  446. 'data' => array('var' => 'second call'),
  447. 'method' => 'get',
  448. 'return' => 'contents'
  449. ));
  450. $this->assertContains('second call', $result);
  451. $result = $this->Case->testAction('/tests_apps/index', array(
  452. 'data' => array('var' => 'third call'),
  453. 'method' => 'get',
  454. 'return' => 'contents'
  455. ));
  456. $this->assertContains('third call', $result);
  457. }
  458. /**
  459. * Test that multiple calls to redirect in the same test method don't cause issues.
  460. *
  461. * @return void
  462. */
  463. public function testTestActionWithMultipleRedirect() {
  464. $this->Case->generate('TestsApps');
  465. $options = array('method' => 'get');
  466. $this->Case->testAction('/tests_apps/redirect_to', $options);
  467. $this->Case->testAction('/tests_apps/redirect_to', $options);
  468. }
  469. /**
  470. * Tests that Components storing response or request objects internally during construct
  471. * will always have a fresh reference to those object available
  472. *
  473. * @return void
  474. * @see https://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/2705-requesthandler-weird-behavior
  475. */
  476. public function testComponentsSameRequestAndResponse() {
  477. $this->Case->generate('TestsApps');
  478. $options = array('method' => 'get');
  479. $this->Case->testAction('/tests_apps/index', $options);
  480. $this->assertSame($this->Case->controller->response, $this->Case->controller->RequestHandler->response);
  481. $this->assertSame($this->Case->controller->request, $this->Case->controller->RequestHandler->request);
  482. }
  483. /**
  484. * Test that testAction() doesn't destroy data in GET & POST
  485. *
  486. * @return void
  487. */
  488. public function testRestoreGetPost() {
  489. $restored = array('new' => 'value');
  490. $_GET = $restored;
  491. $_POST = $restored;
  492. $this->Case->generate('TestsApps');
  493. $options = array('method' => 'get');
  494. $this->Case->testAction('/tests_apps/index', $options);
  495. $this->assertEquals($restored, $_GET);
  496. $this->assertEquals($restored, $_POST);
  497. }
  498. }