ControllerTestCaseTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <?php
  2. /**
  3. * ControllerTestCaseTest file
  4. *
  5. * Test Case for ControllerTestCase class
  6. *
  7. * CakePHP : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP Project
  16. * @since 2.0.0
  17. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  18. */
  19. namespace Cake\Test\TestCase\TestSuite;
  20. use Cake\Controller\Controller;
  21. use Cake\Core\App;
  22. use Cake\Core\Configure;
  23. use Cake\Core\Plugin;
  24. use Cake\ORM\Table;
  25. use Cake\Routing\Router;
  26. use Cake\TestSuite\Reporter\HtmlReporter;
  27. use Cake\TestSuite\TestCase;
  28. use Cake\Utility\ClassRegistry;
  29. /**
  30. * AppController class
  31. *
  32. */
  33. class AppController extends Controller {
  34. /**
  35. * helpers property
  36. *
  37. * @var array
  38. */
  39. public $helpers = array('Html');
  40. /**
  41. * uses property
  42. *
  43. * @var array
  44. */
  45. public $uses = array('ControllerPost');
  46. /**
  47. * components property
  48. *
  49. * @var array
  50. */
  51. public $components = array('Cookie');
  52. }
  53. /**
  54. * ControllerTestCaseTest controller
  55. *
  56. */
  57. class ControllerTestCaseTestController extends AppController {
  58. /**
  59. * Uses array
  60. *
  61. * @param array
  62. */
  63. public $uses = array('TestPlugin.TestPluginComment');
  64. }
  65. /**
  66. * ControllerTestCaseTest
  67. *
  68. */
  69. class ControllerTestCaseTest extends TestCase {
  70. /**
  71. * fixtures property
  72. *
  73. * @var array
  74. */
  75. public $fixtures = array('core.post', 'core.author', 'core.test_plugin_comment');
  76. /**
  77. * reset environment.
  78. *
  79. * @return void
  80. */
  81. public function setUp() {
  82. parent::setUp();
  83. $this->markTestIncomplete('Need to revisit once models work again.');
  84. Configure::write('App.namespace', 'TestApp');
  85. Plugin::load(array('TestPlugin', 'TestPluginTwo'));
  86. $this->Case = $this->getMockForAbstractClass('Cake\TestSuite\ControllerTestCase');
  87. Router::reload();
  88. }
  89. /**
  90. * tearDown
  91. *
  92. * @return void
  93. */
  94. public function tearDown() {
  95. parent::tearDown();
  96. Plugin::unload();
  97. $this->Case->controller = null;
  98. }
  99. /**
  100. * Test that ControllerTestCase::generate() creates mock objects correctly
  101. */
  102. public function testGenerate() {
  103. $Posts = $this->Case->generate('TestApp\Controller\PostsController');
  104. $this->assertEquals('Posts', $Posts->name);
  105. $this->assertEquals('Post', $Posts->modelClass);
  106. $this->assertNull($Posts->response->send());
  107. $Posts = $this->Case->generate('TestApp\Controller\PostsController', array(
  108. 'methods' => array(
  109. 'render'
  110. )
  111. ));
  112. $this->assertNull($Posts->render('index'));
  113. $Posts = $this->Case->generate('TestApp\Controller\PostsController', array(
  114. 'models' => array('Post'),
  115. 'components' => array('RequestHandler')
  116. ));
  117. $this->assertInstanceOf('TestApp\Model\Post', $Posts->Post);
  118. $this->assertNull($Posts->Post->save(array()));
  119. $this->assertNull($Posts->Post->find('all'));
  120. $this->assertEquals('posts', $Posts->Post->useTable);
  121. $this->assertNull($Posts->RequestHandler->isAjax());
  122. $Posts = $this->Case->generate('TestApp\Controller\PostsController', array(
  123. 'models' => array(
  124. 'Post' => true
  125. )
  126. ));
  127. $this->assertNull($Posts->Post->save(array()));
  128. $this->assertNull($Posts->Post->find('all'));
  129. $Posts = $this->Case->generate('TestApp\Controller\PostsController', array(
  130. 'models' => array(
  131. 'Post' => array('save'),
  132. )
  133. ));
  134. $this->assertNull($Posts->Post->save(array()));
  135. $this->assertInternalType('array', $Posts->Post->find('all'));
  136. $Posts = $this->Case->generate('TestApp\Controller\PostsController', array(
  137. 'models' => array('Post'),
  138. 'components' => array(
  139. 'RequestHandler' => array('isPut'),
  140. 'Session'
  141. )
  142. ));
  143. $Posts->RequestHandler->expects($this->once())
  144. ->method('isPut')
  145. ->will($this->returnValue(true));
  146. $this->assertTrue($Posts->RequestHandler->isPut());
  147. $Posts->Auth->Session->expects($this->any())
  148. ->method('write')
  149. ->will($this->returnValue('written!'));
  150. $this->assertEquals('written!', $Posts->Auth->Session->write('something'));
  151. }
  152. /**
  153. * testGenerateWithComponentConfig
  154. */
  155. public function testGenerateWithComponentConfig() {
  156. $Tests = $this->Case->generate('TestConfigs', array(
  157. ));
  158. $expected = array('some' => 'config');
  159. $settings = array_intersect_key($Tests->RequestHandler->settings, array('some' => 'foo'));
  160. $this->assertSame($expected, $settings, 'A mocked component should have the same config as an unmocked component');
  161. $Tests = $this->Case->generate('TestConfigs', array(
  162. 'components' => array(
  163. 'RequestHandler' => array('isPut')
  164. )
  165. ));
  166. $expected = array('some' => 'config');
  167. $settings = array_intersect_key($Tests->RequestHandler->settings, array('some' => 'foo'));
  168. $this->assertSame($expected, $settings, 'A mocked component should have the same config as an unmocked component');
  169. }
  170. /**
  171. * Tests ControllerTestCase::generate() using classes from plugins
  172. */
  173. public function testGenerateWithPlugin() {
  174. $Tests = $this->Case->generate('TestPlugin.Tests', array(
  175. 'models' => array(
  176. 'TestPlugin.TestPluginComment'
  177. ),
  178. 'components' => array(
  179. 'TestPlugin.Plugins'
  180. )
  181. ));
  182. $this->assertEquals('Tests', $Tests->name);
  183. $this->assertInstanceOf('TestPlugin\Controller\Component\PluginsComponent', $Tests->Plugins);
  184. $result = ClassRegistry::init('TestPlugin.TestPluginComment');
  185. $this->assertInstanceOf('TestPlugin\Model\TestPluginComment', $result);
  186. }
  187. /**
  188. * Tests testAction
  189. */
  190. public function testTestAction() {
  191. $Controller = $this->Case->generate('TestsApps');
  192. $this->Case->testAction('/tests_apps/index');
  193. $this->assertInternalType('array', $this->Case->controller->viewVars);
  194. $this->Case->testAction('/tests_apps/set_action');
  195. $results = $this->Case->controller->viewVars;
  196. $expected = array(
  197. 'var' => 'string'
  198. );
  199. $this->assertEquals($expected, $results);
  200. $result = $this->Case->controller->response->body();
  201. $this->assertRegExp('/This is the TestsAppsController index view/', $result);
  202. $Controller = $this->Case->generate('TestsApps');
  203. $this->Case->testAction('/tests_apps/redirect_to');
  204. $results = $this->Case->headers;
  205. $expected = array(
  206. 'Location' => 'http://cakephp.org'
  207. );
  208. $this->assertEquals($expected, $results);
  209. }
  210. /**
  211. * Make sure testAction() can hit plugin controllers.
  212. *
  213. * @return void
  214. */
  215. public function testTestActionWithPlugin() {
  216. $this->Case->generate('TestPlugin.Tests');
  217. $this->Case->testAction('/test_plugin/tests/index');
  218. $this->assertEquals('It is a variable', $this->Case->controller->viewVars['test_value']);
  219. }
  220. /**
  221. * Tests using loaded routes during tests
  222. *
  223. * @return void
  224. */
  225. public function testUseRoutes() {
  226. Router::connect('/:controller/:action/*');
  227. include CAKE . 'Test/TestApp/Config/routes.php';
  228. $controller = $this->Case->generate('TestsApps');
  229. $controller->Components->load('RequestHandler');
  230. $result = $this->Case->testAction('/tests_apps/index.json', array('return' => 'contents'));
  231. $result = json_decode($result, true);
  232. $expected = array('cakephp' => 'cool');
  233. $this->assertEquals($expected, $result);
  234. include CAKE . 'Test/TestApp/Config/routes.php';
  235. $result = $this->Case->testAction('/some_alias');
  236. $this->assertEquals(5, $result);
  237. }
  238. /**
  239. * Tests not using loaded routes during tests
  240. *
  241. * @expectedException \Cake\Error\MissingActionException
  242. */
  243. public function testSkipRoutes() {
  244. Router::connect('/:controller/:action/*');
  245. include CAKE . 'Test/TestApp/Config/routes.php';
  246. $this->Case->loadRoutes = false;
  247. $this->Case->testAction('/tests_apps/missing_action.json', array('return' => 'view'));
  248. }
  249. /**
  250. * Tests backwards compatibility with setting the return type
  251. */
  252. public function testBCSetReturn() {
  253. $this->Case->autoMock = true;
  254. $result = $this->Case->testAction('/tests_apps/some_method');
  255. $this->assertEquals(5, $result);
  256. $data = array('var' => 'set');
  257. $result = $this->Case->testAction('/tests_apps_posts/post_var', array(
  258. 'data' => $data,
  259. 'return' => 'vars'
  260. ));
  261. $this->assertEquals($data, $result['data']);
  262. $result = $this->Case->testAction('/tests_apps/set_action', array(
  263. 'return' => 'view'
  264. ));
  265. $this->assertEquals('This is the TestsAppsController index view string', $result);
  266. $result = $this->Case->testAction('/tests_apps/set_action', array(
  267. 'return' => 'contents'
  268. ));
  269. $this->assertRegExp('/<html/', $result);
  270. $this->assertRegExp('/This is the TestsAppsController index view/', $result);
  271. $this->assertRegExp('/<\/html>/', $result);
  272. }
  273. /**
  274. * Tests sending POST data to testAction
  275. */
  276. public function testTestActionPostData() {
  277. $this->Case->autoMock = true;
  278. $data = array(
  279. 'Post' => array(
  280. 'name' => 'Some Post'
  281. )
  282. );
  283. $this->Case->testAction('/tests_apps_posts/post_var', array(
  284. 'method' => 'post',
  285. 'data' => $data
  286. ));
  287. $this->assertEquals($this->Case->controller->viewVars['data'], $data);
  288. $this->assertEquals($this->Case->controller->request->data, $data);
  289. $result = $this->Case->testAction('/tests_apps_posts/post_var', array(
  290. 'return' => 'vars',
  291. 'method' => 'post',
  292. 'data' => array(
  293. 'name' => 'is jonas',
  294. 'pork' => 'and beans',
  295. )
  296. ));
  297. $this->assertEquals(array('name', 'pork'), array_keys($result['data']));
  298. $result = $this->Case->testAction('/tests_apps_posts/add', array('return' => 'vars'));
  299. $this->assertTrue(array_key_exists('posts', $result));
  300. $this->assertEquals(4, count($result['posts']));
  301. $this->assertTrue($this->Case->controller->request->is('post'));
  302. }
  303. /**
  304. * Tests sending GET data to testAction
  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. public function testAutoMock() {
  352. $this->Case->autoMock = true;
  353. $this->Case->testAction('/tests_apps/set_action');
  354. $results = $this->Case->controller->viewVars;
  355. $expected = array(
  356. 'var' => 'string'
  357. );
  358. $this->assertEquals($expected, $results);
  359. }
  360. /**
  361. * Test using testAction and not mocking
  362. */
  363. public function testNoMocking() {
  364. $result = $this->Case->testAction('/tests_apps/some_method');
  365. $this->Case->assertEquals(5, $result);
  366. $data = array('var' => 'set');
  367. $result = $this->Case->testAction('/tests_apps_posts/post_var', array(
  368. 'method' => 'post',
  369. 'data' => $data,
  370. 'return' => 'vars'
  371. ));
  372. $this->assertEquals($data, $result['data']);
  373. $result = $this->Case->testAction('/tests_apps/set_action', array(
  374. 'return' => 'view'
  375. ));
  376. $this->assertEquals('This is the TestsAppsController index view string', $result);
  377. $result = $this->Case->testAction('/tests_apps/set_action', array(
  378. 'return' => 'contents'
  379. ));
  380. $this->assertRegExp('/<html/', $result);
  381. $this->assertRegExp('/This is the TestsAppsController index view/', $result);
  382. $this->assertRegExp('/<\/html>/', $result);
  383. }
  384. /**
  385. * Test that controllers don't get reused.
  386. *
  387. * @return void
  388. */
  389. public function testNoControllerReuse() {
  390. $this->Case->autoMock = true;
  391. $result = $this->Case->testAction('/tests_apps/index', array(
  392. 'data' => array('var' => 'first call'),
  393. 'method' => 'get',
  394. 'return' => 'contents',
  395. ));
  396. $this->assertContains('<html', $result);
  397. $this->assertContains('This is the TestsAppsController index view', $result);
  398. $this->assertContains('first call', $result);
  399. $this->assertContains('</html>', $result);
  400. $result = $this->Case->testAction('/tests_apps/index', array(
  401. 'data' => array('var' => 'second call'),
  402. 'method' => 'get',
  403. 'return' => 'contents'
  404. ));
  405. $this->assertContains('second call', $result);
  406. $result = $this->Case->testAction('/tests_apps/index', array(
  407. 'data' => array('var' => 'third call'),
  408. 'method' => 'get',
  409. 'return' => 'contents'
  410. ));
  411. $this->assertContains('third call', $result);
  412. }
  413. /**
  414. * Test that multiple calls to redirect in the same test method don't cause issues.
  415. *
  416. * @return void
  417. */
  418. public function testTestActionWithMultipleRedirect() {
  419. $this->Case->generate('TestsApps');
  420. $options = array('method' => 'get');
  421. $this->Case->testAction('/tests_apps/redirect_to', $options);
  422. $this->Case->testAction('/tests_apps/redirect_to', $options);
  423. }
  424. /**
  425. * Tests that Components storing response or request objects internally during construct
  426. * will always have a fresh reference to those object available
  427. *
  428. * @return void
  429. * @see https://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/2705-requesthandler-weird-behavior
  430. */
  431. public function testComponentsSameRequestAndResponse() {
  432. $this->Case->generate('TestsApps');
  433. $options = array('method' => 'get');
  434. $this->Case->testAction('/tests_apps/index', $options);
  435. $this->assertSame($this->Case->controller->response, $this->Case->controller->RequestHandler->response);
  436. $this->assertSame($this->Case->controller->request, $this->Case->controller->RequestHandler->request);
  437. }
  438. /**
  439. * Test that testAction() doesn't destroy data in GET & POST
  440. *
  441. * @return void
  442. */
  443. public function testRestoreGetPost() {
  444. $restored = array('new' => 'value');
  445. $_GET = $restored;
  446. $_POST = $restored;
  447. $this->Case->generate('TestsApps');
  448. $options = array('method' => 'get');
  449. $this->Case->testAction('/tests_apps/index', $options);
  450. $this->assertEquals($restored, $_GET);
  451. $this->assertEquals($restored, $_POST);
  452. }
  453. }