ControllerTestCaseTest.php 15 KB

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