ControllerTaskTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. <?php
  2. /**
  3. * ControllerTask Test Case
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @package Cake.Test.Case.Console.Command.Task
  15. * @since CakePHP(tm) v 1.3
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('ConsoleOutput', 'Console');
  19. App::uses('ConsoleInput', 'Console');
  20. App::uses('ShellDispatcher', 'Console');
  21. App::uses('Shell', 'Console');
  22. App::uses('CakeSchema', 'Model');
  23. App::uses('ClassRegistry', 'Utility');
  24. App::uses('Helper', 'View/Helper');
  25. App::uses('ProjectTask', 'Console/Command/Task');
  26. App::uses('ControllerTask', 'Console/Command/Task');
  27. App::uses('ModelTask', 'Console/Command/Task');
  28. App::uses('TemplateTask', 'Console/Command/Task');
  29. App::uses('TestTask', 'Console/Command/Task');
  30. App::uses('Model', 'Model');
  31. App::uses('BakeArticle', 'Model');
  32. App::uses('BakeComment', 'Model');
  33. App::uses('BakeTags', 'Model');
  34. $imported = class_exists('BakeArticle') || class_exists('BakeComment') || class_exists('BakeTag');
  35. if (!$imported) {
  36. define('ARTICLE_MODEL_CREATED', true);
  37. /**
  38. * Class BakeArticle
  39. */
  40. class BakeArticle extends Model {
  41. public $hasMany = array('BakeComment');
  42. public $hasAndBelongsToMany = array('BakeTag');
  43. }
  44. }
  45. /**
  46. * ControllerTaskTest class
  47. *
  48. * @package Cake.Test.Case.Console.Command.Task
  49. */
  50. class ControllerTaskTest extends CakeTestCase {
  51. /**
  52. * fixtures
  53. *
  54. * @var array
  55. */
  56. public $fixtures = array('core.bake_article', 'core.bake_articles_bake_tag', 'core.bake_comment', 'core.bake_tag');
  57. /**
  58. * setUp method
  59. *
  60. * @return void
  61. */
  62. public function setUp() {
  63. parent::setUp();
  64. $out = $this->getMock('ConsoleOutput', array(), array(), '', false);
  65. $in = $this->getMock('ConsoleInput', array(), array(), '', false);
  66. $this->Task = $this->getMock('ControllerTask',
  67. array('in', 'out', 'err', 'hr', 'createFile', '_stop', '_checkUnitTest'),
  68. array($out, $out, $in)
  69. );
  70. $this->Task->name = 'Controller';
  71. $this->Task->Template = new TemplateTask($out, $out, $in);
  72. $this->Task->Template->params['theme'] = 'default';
  73. $this->Task->Model = $this->getMock('ModelTask',
  74. array('in', 'out', 'err', 'createFile', '_stop', '_checkUnitTest'),
  75. array($out, $out, $in)
  76. );
  77. $this->Task->Project = $this->getMock('ProjectTask',
  78. array('in', 'out', 'err', 'createFile', '_stop', '_checkUnitTest', 'getPrefix'),
  79. array($out, $out, $in)
  80. );
  81. $this->Task->Test = $this->getMock('TestTask', array(), array($out, $out, $in));
  82. if (!defined('ARTICLE_MODEL_CREATED')) {
  83. $this->markTestSkipped('Could not run as an Article, Tag or Comment model was already loaded.');
  84. }
  85. }
  86. /**
  87. * tearDown method
  88. *
  89. * @return void
  90. */
  91. public function tearDown() {
  92. unset($this->Task);
  93. ClassRegistry::flush();
  94. App::build();
  95. parent::tearDown();
  96. }
  97. /**
  98. * test ListAll
  99. *
  100. * @return void
  101. */
  102. public function testListAll() {
  103. $count = count($this->Task->listAll('test'));
  104. if ($count != count($this->fixtures)) {
  105. $this->markTestSkipped('Additional tables detected.');
  106. }
  107. $this->Task->connection = 'test';
  108. $this->Task->interactive = true;
  109. $this->Task->expects($this->at(2))->method('out')->with(' 1. BakeArticles');
  110. $this->Task->expects($this->at(3))->method('out')->with(' 2. BakeArticlesBakeTags');
  111. $this->Task->expects($this->at(4))->method('out')->with(' 3. BakeComments');
  112. $this->Task->expects($this->at(5))->method('out')->with(' 4. BakeTags');
  113. $expected = array('BakeArticles', 'BakeArticlesBakeTags', 'BakeComments', 'BakeTags');
  114. $result = $this->Task->listAll('test');
  115. $this->assertEquals($expected, $result);
  116. $this->Task->interactive = false;
  117. $result = $this->Task->listAll();
  118. $expected = array('bake_articles', 'bake_articles_bake_tags', 'bake_comments', 'bake_tags');
  119. $this->assertEquals($expected, $result);
  120. }
  121. /**
  122. * Test that getName interacts with the user and returns the controller name.
  123. *
  124. * @return void
  125. */
  126. public function testGetNameValidIndex() {
  127. $count = count($this->Task->listAll('test'));
  128. if ($count != count($this->fixtures)) {
  129. $this->markTestSkipped('Additional tables detected.');
  130. }
  131. $this->Task->interactive = true;
  132. $this->Task->expects($this->any())->method('in')->will(
  133. $this->onConsecutiveCalls(3, 1)
  134. );
  135. $result = $this->Task->getName('test');
  136. $expected = 'BakeComments';
  137. $this->assertEquals($expected, $result);
  138. $result = $this->Task->getName('test');
  139. $expected = 'BakeArticles';
  140. $this->assertEquals($expected, $result);
  141. }
  142. /**
  143. * test getting invalid indexes.
  144. *
  145. * @return void
  146. */
  147. public function testGetNameInvalidIndex() {
  148. $this->Task->interactive = true;
  149. $this->Task->expects($this->any())->method('in')
  150. ->will($this->onConsecutiveCalls(50, 'q'));
  151. $this->Task->expects($this->once())->method('err');
  152. $this->Task->expects($this->once())->method('_stop');
  153. $this->Task->getName('test');
  154. }
  155. /**
  156. * test helper interactions
  157. *
  158. * @return void
  159. */
  160. public function testDoHelpersNo() {
  161. $this->Task->expects($this->any())->method('in')->will($this->returnValue('n'));
  162. $result = $this->Task->doHelpers();
  163. $this->assertSame(array(), $result);
  164. }
  165. /**
  166. * test getting helper values
  167. *
  168. * @return void
  169. */
  170. public function testDoHelpersTrailingSpace() {
  171. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
  172. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue(' Text, Number, CustomOne '));
  173. $result = $this->Task->doHelpers();
  174. $expected = array('Text', 'Number', 'CustomOne');
  175. $this->assertEquals($expected, $result);
  176. }
  177. /**
  178. * test doHelpers with extra commas
  179. *
  180. * @return void
  181. */
  182. public function testDoHelpersTrailingCommas() {
  183. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
  184. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue(' Text, Number, CustomOne, , '));
  185. $result = $this->Task->doHelpers();
  186. $expected = array('Text', 'Number', 'CustomOne');
  187. $this->assertEquals($expected, $result);
  188. }
  189. /**
  190. * test component interactions
  191. *
  192. * @return void
  193. */
  194. public function testDoComponentsNo() {
  195. $this->Task->expects($this->any())->method('in')->will($this->returnValue('n'));
  196. $result = $this->Task->doComponents();
  197. $this->assertSame(array('Paginator'), $result);
  198. }
  199. /**
  200. * test components with spaces
  201. *
  202. * @return void
  203. */
  204. public function testDoComponentsTrailingSpaces() {
  205. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
  206. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue(' RequestHandler, Security '));
  207. $result = $this->Task->doComponents();
  208. $expected = array('Paginator', 'RequestHandler', 'Security');
  209. $this->assertEquals($expected, $result);
  210. }
  211. /**
  212. * test components with commas
  213. *
  214. * @return void
  215. */
  216. public function testDoComponentsTrailingCommas() {
  217. $this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
  218. $this->Task->expects($this->at(1))->method('in')->will($this->returnValue(' RequestHandler, Security, , '));
  219. $result = $this->Task->doComponents();
  220. $expected = array('Paginator', 'RequestHandler', 'Security');
  221. $this->assertEquals($expected, $result);
  222. }
  223. /**
  224. * test Confirming controller user interaction
  225. *
  226. * @return void
  227. */
  228. public function testConfirmController() {
  229. $controller = 'Posts';
  230. $scaffold = false;
  231. $helpers = array('Js', 'Time');
  232. $components = array('Acl', 'Auth');
  233. $this->Task->expects($this->at(4))->method('out')->with("Controller Name:\n\t$controller");
  234. $this->Task->expects($this->at(5))->method('out')->with("Helpers:\n\tJs, Time");
  235. $this->Task->expects($this->at(6))->method('out')->with("Components:\n\tAcl, Auth");
  236. $this->Task->confirmController($controller, $scaffold, $helpers, $components);
  237. }
  238. /**
  239. * test the bake method
  240. *
  241. * @return void
  242. */
  243. public function testBake() {
  244. $helpers = array('Js', 'Time');
  245. $components = array('Acl', 'Auth');
  246. $this->Task->expects($this->any())->method('createFile')->will($this->returnValue(true));
  247. $result = $this->Task->bake('Articles', null, $helpers, $components);
  248. $expected = file_get_contents(CAKE . 'Test' . DS . 'bake_compare' . DS . 'Controller' . DS . 'NoActions.ctp');
  249. $this->assertTextEquals($expected, $result);
  250. $result = $this->Task->bake('Articles', null, array(), array());
  251. $expected = file_get_contents(CAKE . 'Test' . DS . 'bake_compare' . DS . 'Controller' . DS . 'NoHelpersOrComponents.ctp');
  252. $this->assertTextEquals($expected, $result);
  253. $result = $this->Task->bake('Articles', 'scaffold', $helpers, $components);
  254. $expected = file_get_contents(CAKE . 'Test' . DS . 'bake_compare' . DS . 'Controller' . DS . 'Scaffold.ctp');
  255. $this->assertTextEquals($expected, $result);
  256. }
  257. /**
  258. * test bake() with a -plugin param
  259. *
  260. * @return void
  261. */
  262. public function testBakeWithPlugin() {
  263. $this->Task->plugin = 'ControllerTest';
  264. //fake plugin path
  265. CakePlugin::load('ControllerTest', array('path' => APP . 'Plugin' . DS . 'ControllerTest' . DS));
  266. $path = APP . 'Plugin' . DS . 'ControllerTest' . DS . 'Controller' . DS . 'ArticlesController.php';
  267. $this->Task->expects($this->at(1))->method('createFile')->with(
  268. $path,
  269. new PHPUnit_Framework_Constraint_IsAnything()
  270. );
  271. $this->Task->expects($this->at(3))->method('createFile')->with(
  272. $path,
  273. $this->stringContains('ArticlesController extends ControllerTestAppController')
  274. )->will($this->returnValue(true));
  275. $this->Task->bake('Articles', '--actions--', array(), array(), array());
  276. $this->Task->plugin = 'ControllerTest';
  277. $path = APP . 'Plugin' . DS . 'ControllerTest' . DS . 'Controller' . DS . 'ArticlesController.php';
  278. $result = $this->Task->bake('Articles', '--actions--', array(), array(), array());
  279. $this->assertContains("App::uses('ControllerTestAppController', 'ControllerTest.Controller');", $result);
  280. $this->assertEquals('ControllerTest', $this->Task->Template->templateVars['plugin']);
  281. $this->assertEquals('ControllerTest.', $this->Task->Template->templateVars['pluginPath']);
  282. CakePlugin::unload();
  283. }
  284. /**
  285. * test that bakeActions is creating the correct controller Code. (Using sessions)
  286. *
  287. * @return void
  288. */
  289. public function testBakeActionsUsingSessions() {
  290. $result = $this->Task->bakeActions('BakeArticles', null, true);
  291. $expected = file_get_contents(CAKE . 'Test' . DS . 'bake_compare' . DS . 'Controller' . DS . 'ActionsUsingSessions.ctp');
  292. $this->assertTextEquals($expected, $result);
  293. $result = $this->Task->bakeActions('BakeArticles', 'admin_', true);
  294. $this->assertContains('function admin_index() {', $result);
  295. $this->assertContains('function admin_add()', $result);
  296. $this->assertContains('function admin_view($id = null)', $result);
  297. $this->assertContains('function admin_edit($id = null)', $result);
  298. $this->assertContains('function admin_delete($id = null)', $result);
  299. }
  300. /**
  301. * Test baking with Controller::flash() or no sessions.
  302. *
  303. * @return void
  304. */
  305. public function testBakeActionsWithNoSessions() {
  306. $result = $this->Task->bakeActions('BakeArticles', null, false);
  307. $expected = file_get_contents(CAKE . 'Test' . DS . 'bake_compare' . DS . 'Controller' . DS . 'ActionsWithNoSessions.ctp');
  308. $this->assertTextEquals($expected, $result);
  309. }
  310. /**
  311. * test baking a test
  312. *
  313. * @return void
  314. */
  315. public function testBakeTest() {
  316. $this->Task->plugin = 'ControllerTest';
  317. $this->Task->connection = 'test';
  318. $this->Task->interactive = false;
  319. $this->Task->Test->expects($this->once())->method('bake')->with('Controller', 'BakeArticles');
  320. $this->Task->bakeTest('BakeArticles');
  321. $this->assertEquals($this->Task->plugin, $this->Task->Test->plugin);
  322. $this->assertEquals($this->Task->connection, $this->Task->Test->connection);
  323. $this->assertEquals($this->Task->interactive, $this->Task->Test->interactive);
  324. }
  325. /**
  326. * test Interactive mode.
  327. *
  328. * @return void
  329. */
  330. public function testInteractive() {
  331. $count = count($this->Task->listAll('test'));
  332. if ($count != count($this->fixtures)) {
  333. $this->markTestSkipped('Additional tables detected.');
  334. }
  335. $this->Task->connection = 'test';
  336. $this->Task->path = '/my/path/';
  337. $this->Task->expects($this->any())->method('in')
  338. ->will($this->onConsecutiveCalls(
  339. '1',
  340. 'y', // build interactive
  341. 'n', // build no scaffolds
  342. 'y', // build normal methods
  343. 'n', // build admin methods
  344. 'n', // helpers?
  345. 'n', // components?
  346. 'y', // sessions ?
  347. 'y' // looks good?
  348. ));
  349. $filename = '/my/path/BakeArticlesController.php';
  350. $this->Task->expects($this->once())->method('createFile')->with(
  351. $filename,
  352. $this->stringContains('class BakeArticlesController')
  353. );
  354. $this->Task->execute();
  355. }
  356. /**
  357. * test Interactive mode.
  358. *
  359. * @return void
  360. */
  361. public function testInteractiveAdminMethodsNotInteractive() {
  362. $count = count($this->Task->listAll('test'));
  363. if ($count != count($this->fixtures)) {
  364. $this->markTestSkipped('Additional tables detected.');
  365. }
  366. $this->Task->connection = 'test';
  367. $this->Task->interactive = true;
  368. $this->Task->path = '/my/path/';
  369. $this->Task->expects($this->any())->method('in')
  370. ->will($this->onConsecutiveCalls(
  371. '1',
  372. 'y', // build interactive
  373. 'n', // build no scaffolds
  374. 'y', // build normal methods
  375. 'y', // build admin methods
  376. 'n', // helpers?
  377. 'n', // components?
  378. 'y', // sessions ?
  379. 'y' // looks good?
  380. ));
  381. $this->Task->Project->expects($this->any())
  382. ->method('getPrefix')
  383. ->will($this->returnValue('admin_'));
  384. $filename = '/my/path/BakeArticlesController.php';
  385. $this->Task->expects($this->once())->method('createFile')->with(
  386. $filename,
  387. $this->stringContains('class BakeArticlesController')
  388. )->will($this->returnValue(true));
  389. $result = $this->Task->execute();
  390. $this->assertRegExp('/admin_index/', $result);
  391. }
  392. /**
  393. * test that execute runs all when the first arg == all
  394. *
  395. * @return void
  396. */
  397. public function testExecuteIntoAll() {
  398. $count = count($this->Task->listAll('test'));
  399. if ($count != count($this->fixtures)) {
  400. $this->markTestSkipped('Additional tables detected.');
  401. }
  402. $this->Task->connection = 'test';
  403. $this->Task->path = '/my/path/';
  404. $this->Task->args = array('all');
  405. $this->Task->expects($this->any())->method('_checkUnitTest')->will($this->returnValue(true));
  406. $this->Task->Test->expects($this->once())->method('bake');
  407. $filename = '/my/path/BakeArticlesController.php';
  408. $this->Task->expects($this->once())->method('createFile')->with(
  409. $filename,
  410. $this->stringContains('class BakeArticlesController')
  411. )->will($this->returnValue(true));
  412. $this->Task->execute();
  413. }
  414. /**
  415. * Test execute() with all and --admin
  416. *
  417. * @return void
  418. */
  419. public function testExecuteIntoAllAdmin() {
  420. $count = count($this->Task->listAll('test'));
  421. if ($count != count($this->fixtures)) {
  422. $this->markTestSkipped('Additional tables detected.');
  423. }
  424. $this->Task->connection = 'test';
  425. $this->Task->path = '/my/path/';
  426. $this->Task->args = array('all');
  427. $this->Task->params['admin'] = true;
  428. $this->Task->Project->expects($this->any())
  429. ->method('getPrefix')
  430. ->will($this->returnValue('admin_'));
  431. $this->Task->expects($this->any())
  432. ->method('_checkUnitTest')
  433. ->will($this->returnValue(true));
  434. $this->Task->Test->expects($this->once())->method('bake');
  435. $filename = '/my/path/BakeArticlesController.php';
  436. $this->Task->expects($this->once())->method('createFile')->with(
  437. $filename,
  438. $this->stringContains('function admin_index')
  439. )->will($this->returnValue(true));
  440. $this->Task->execute();
  441. }
  442. /**
  443. * test that `cake bake controller foos` works.
  444. *
  445. * @return void
  446. */
  447. public function testExecuteWithController() {
  448. $this->Task->connection = 'test';
  449. $this->Task->path = '/my/path/';
  450. $this->Task->args = array('BakeArticles');
  451. $filename = '/my/path/BakeArticlesController.php';
  452. $this->Task->expects($this->once())->method('createFile')->with(
  453. $filename,
  454. $this->stringContains('$scaffold')
  455. );
  456. $this->Task->execute();
  457. }
  458. /**
  459. * data provider for testExecuteWithControllerNameVariations
  460. *
  461. * @return void
  462. */
  463. public static function nameVariations() {
  464. return array(
  465. array('BakeArticles'), array('BakeArticle'), array('bake_article'), array('bake_articles')
  466. );
  467. }
  468. /**
  469. * test that both plural and singular forms work for controller baking.
  470. *
  471. * @dataProvider nameVariations
  472. * @return void
  473. */
  474. public function testExecuteWithControllerNameVariations($name) {
  475. $this->Task->connection = 'test';
  476. $this->Task->path = '/my/path/';
  477. $this->Task->args = array($name);
  478. $filename = '/my/path/BakeArticlesController.php';
  479. $this->Task->expects($this->once())->method('createFile')->with(
  480. $filename, $this->stringContains('$scaffold')
  481. );
  482. $this->Task->execute();
  483. }
  484. /**
  485. * test that `cake bake controller foo scaffold` works.
  486. *
  487. * @return void
  488. */
  489. public function testExecuteWithPublicParam() {
  490. $this->Task->connection = 'test';
  491. $this->Task->path = '/my/path/';
  492. $this->Task->args = array('BakeArticles');
  493. $this->Task->params = array('public' => true);
  494. $filename = '/my/path/BakeArticlesController.php';
  495. $expected = new PHPUnit_Framework_Constraint_Not($this->stringContains('$scaffold'));
  496. $this->Task->expects($this->once())->method('createFile')->with(
  497. $filename, $expected
  498. );
  499. $this->Task->execute();
  500. }
  501. /**
  502. * test that `cake bake controller foos both` works.
  503. *
  504. * @return void
  505. */
  506. public function testExecuteWithControllerAndBoth() {
  507. $this->Task->Project->expects($this->any())->method('getPrefix')->will($this->returnValue('admin_'));
  508. $this->Task->connection = 'test';
  509. $this->Task->path = '/my/path/';
  510. $this->Task->args = array('BakeArticles');
  511. $this->Task->params = array('public' => true, 'admin' => true);
  512. $filename = '/my/path/BakeArticlesController.php';
  513. $this->Task->expects($this->once())->method('createFile')->with(
  514. $filename, $this->stringContains('admin_index')
  515. );
  516. $this->Task->execute();
  517. }
  518. /**
  519. * test that `cake bake controller foos admin` works.
  520. *
  521. * @return void
  522. */
  523. public function testExecuteWithControllerAndAdmin() {
  524. $this->Task->Project->expects($this->any())->method('getPrefix')->will($this->returnValue('admin_'));
  525. $this->Task->connection = 'test';
  526. $this->Task->path = '/my/path/';
  527. $this->Task->args = array('BakeArticles');
  528. $this->Task->params = array('admin' => true);
  529. $filename = '/my/path/BakeArticlesController.php';
  530. $this->Task->expects($this->once())->method('createFile')->with(
  531. $filename, $this->stringContains('admin_index')
  532. );
  533. $this->Task->execute();
  534. }
  535. }