ControllerTestCaseTest.php 14 KB

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