ControllerTestCaseTest.php 15 KB

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