ControllerTestCaseTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. 'data' => $data
  285. ));
  286. $this->assertEquals($this->Case->controller->viewVars['data'], $data);
  287. $this->assertEquals($this->Case->controller->request->data, $data);
  288. $result = $this->Case->testAction('/tests_apps_posts/post_var', array(
  289. 'return' => 'vars',
  290. 'method' => 'post',
  291. 'data' => array(
  292. 'name' => 'is jonas',
  293. 'pork' => 'and beans',
  294. )
  295. ));
  296. $this->assertEquals(array('name', 'pork'), array_keys($result['data']));
  297. $result = $this->Case->testAction('/tests_apps_posts/add', array('return' => 'vars'));
  298. $this->assertTrue(array_key_exists('posts', $result));
  299. $this->assertEquals(4, count($result['posts']));
  300. $this->assertTrue($this->Case->controller->request->is('post'));
  301. }
  302. /**
  303. * Tests sending GET data to testAction
  304. */
  305. public function testTestActionGetData() {
  306. $this->Case->autoMock = true;
  307. $result = $this->Case->testAction('/tests_apps_posts/url_var', array(
  308. 'method' => 'get',
  309. 'data' => array(
  310. 'some' => 'var',
  311. 'lackof' => 'creativity'
  312. )
  313. ));
  314. $this->assertEquals('var', $this->Case->controller->request->query['some']);
  315. $this->assertEquals('creativity', $this->Case->controller->request->query['lackof']);
  316. $result = $this->Case->testAction('/tests_apps_posts/url_var/gogo/val2', array(
  317. 'return' => 'vars',
  318. 'method' => 'get',
  319. ));
  320. $this->assertEquals(array('gogo', 'val2'), $result['params']['pass']);
  321. $result = $this->Case->testAction('/tests_apps_posts/url_var', array(
  322. 'return' => 'vars',
  323. 'method' => 'get',
  324. 'data' => array(
  325. 'red' => 'health',
  326. 'blue' => 'mana'
  327. )
  328. ));
  329. $query = $this->Case->controller->request->query;
  330. $this->assertTrue(isset($query['red']));
  331. $this->assertTrue(isset($query['blue']));
  332. }
  333. /**
  334. * Test that REST actions with XML/JSON input work.
  335. *
  336. * @return void
  337. */
  338. public function testTestActionJsonData() {
  339. $result = $this->Case->testAction('/tests_apps_posts/input_data', array(
  340. 'return' => 'vars',
  341. 'method' => 'post',
  342. 'data' => '{"key":"value","json":true}'
  343. ));
  344. $this->assertEquals('value', $result['data']['key']);
  345. $this->assertTrue($result['data']['json']);
  346. }
  347. /**
  348. * Tests autoMock ability
  349. */
  350. public function testAutoMock() {
  351. $this->Case->autoMock = true;
  352. $this->Case->testAction('/tests_apps/set_action');
  353. $results = $this->Case->controller->viewVars;
  354. $expected = array(
  355. 'var' => 'string'
  356. );
  357. $this->assertEquals($expected, $results);
  358. }
  359. /**
  360. * Test using testAction and not mocking
  361. */
  362. public function testNoMocking() {
  363. $result = $this->Case->testAction('/tests_apps/some_method');
  364. $this->Case->assertEquals(5, $result);
  365. $data = array('var' => 'set');
  366. $result = $this->Case->testAction('/tests_apps_posts/post_var', array(
  367. 'data' => $data,
  368. 'return' => 'vars'
  369. ));
  370. $this->assertEquals($data, $result['data']);
  371. $result = $this->Case->testAction('/tests_apps/set_action', array(
  372. 'return' => 'view'
  373. ));
  374. $this->assertEquals('This is the TestsAppsController index view string', $result);
  375. $result = $this->Case->testAction('/tests_apps/set_action', array(
  376. 'return' => 'contents'
  377. ));
  378. $this->assertRegExp('/<html/', $result);
  379. $this->assertRegExp('/This is the TestsAppsController index view/', $result);
  380. $this->assertRegExp('/<\/html>/', $result);
  381. }
  382. /**
  383. * Test that controllers don't get reused.
  384. *
  385. * @return void
  386. */
  387. public function testNoControllerReuse() {
  388. $this->Case->autoMock = true;
  389. $result = $this->Case->testAction('/tests_apps/index', array(
  390. 'data' => array('var' => 'first call'),
  391. 'method' => 'get',
  392. 'return' => 'contents',
  393. ));
  394. $this->assertContains('<html', $result);
  395. $this->assertContains('This is the TestsAppsController index view', $result);
  396. $this->assertContains('first call', $result);
  397. $this->assertContains('</html>', $result);
  398. $result = $this->Case->testAction('/tests_apps/index', array(
  399. 'data' => array('var' => 'second call'),
  400. 'method' => 'get',
  401. 'return' => 'contents'
  402. ));
  403. $this->assertContains('second call', $result);
  404. $result = $this->Case->testAction('/tests_apps/index', array(
  405. 'data' => array('var' => 'third call'),
  406. 'method' => 'get',
  407. 'return' => 'contents'
  408. ));
  409. $this->assertContains('third call', $result);
  410. }
  411. /**
  412. * Test that multiple calls to redirect in the same test method don't cause issues.
  413. *
  414. * @return void
  415. */
  416. public function testTestActionWithMultipleRedirect() {
  417. $this->Case->generate('TestsApps');
  418. $options = array('method' => 'get');
  419. $this->Case->testAction('/tests_apps/redirect_to', $options);
  420. $this->Case->testAction('/tests_apps/redirect_to', $options);
  421. }
  422. /**
  423. * Tests that Components storing response or request objects internally during construct
  424. * will always have a fresh reference to those object available
  425. *
  426. * @return void
  427. * @see https://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/2705-requesthandler-weird-behavior
  428. */
  429. public function testComponentsSameRequestAndResponse() {
  430. $this->Case->generate('TestsApps');
  431. $options = array('method' => 'get');
  432. $this->Case->testAction('/tests_apps/index', $options);
  433. $this->assertSame($this->Case->controller->response, $this->Case->controller->RequestHandler->response);
  434. $this->assertSame($this->Case->controller->request, $this->Case->controller->RequestHandler->request);
  435. }
  436. /**
  437. * Test that testAction() doesn't destroy data in GET & POST
  438. *
  439. * @return void
  440. */
  441. public function testRestoreGetPost() {
  442. $restored = array('new' => 'value');
  443. $_GET = $restored;
  444. $_POST = $restored;
  445. $this->Case->generate('TestsApps');
  446. $options = array('method' => 'get');
  447. $this->Case->testAction('/tests_apps/index', $options);
  448. $this->assertEquals($restored, $_GET);
  449. $this->assertEquals($restored, $_POST);
  450. }
  451. }