ControllerTaskTest.php 17 KB

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