ControllerTestCaseTest.php 14 KB

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