ControllerTaskTest.php 18 KB

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