ControllerTaskTest.php 19 KB

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