ControllerTaskTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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\ORM\TableRegistry;
  23. use Cake\TestSuite\TestCase;
  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. /**
  35. * ControllerTaskTest class
  36. *
  37. */
  38. class ControllerTaskTest extends TestCase {
  39. /**
  40. * fixtures
  41. *
  42. * @var array
  43. */
  44. public $fixtures = ['core.bake_article', 'core.bake_articles_bake_tag', 'core.bake_comment', 'core.bake_tag'];
  45. /**
  46. * setUp method
  47. *
  48. * @return void
  49. */
  50. public function setUp() {
  51. parent::setUp();
  52. $out = $this->getMock('Cake\Console\ConsoleOutput', array(), array(), '', false);
  53. $in = $this->getMock('Cake\Console\ConsoleInput', array(), array(), '', false);
  54. $this->Task = $this->getMock('Cake\Console\Command\Task\ControllerTask',
  55. array('in', 'out', 'err', 'hr', 'createFile', '_stop'),
  56. array($out, $out, $in)
  57. );
  58. $this->Task->name = 'Controller';
  59. $this->Task->connection = 'test';
  60. $this->Task->Template = new TemplateTask($out, $out, $in);
  61. $this->Task->Template->params['theme'] = 'default';
  62. $this->Task->Model = $this->getMock('Cake\Console\Command\Task\ModelTask',
  63. array('in', 'out', 'err', 'createFile', '_stop'),
  64. array($out, $out, $in)
  65. );
  66. $this->Task->Project = $this->getMock('Cake\Console\Command\Task\ProjectTask',
  67. array('in', 'out', 'err', 'createFile', '_stop', 'getPrefix'),
  68. array($out, $out, $in)
  69. );
  70. $this->Task->Test = $this->getMock('Cake\Console\Command\Task\TestTask', array(), array($out, $out, $in));
  71. }
  72. /**
  73. * tearDown method
  74. *
  75. * @return void
  76. */
  77. public function tearDown() {
  78. unset($this->Task);
  79. parent::tearDown();
  80. }
  81. /**
  82. * test ListAll
  83. *
  84. * @return void
  85. */
  86. public function testListAll() {
  87. $count = count($this->Task->listAll('test'));
  88. if ($count != count($this->fixtures)) {
  89. $this->markTestSkipped('Additional tables detected.');
  90. }
  91. $result = $this->Task->listAll();
  92. $expected = array('bake_articles', 'bake_articles_bake_tags', 'bake_comments', 'bake_tags');
  93. $this->assertEquals($expected, $result);
  94. }
  95. /**
  96. * test component generation
  97. *
  98. * @return void
  99. */
  100. public function testGetComponents() {
  101. $result = $this->Task->getComponents();
  102. $this->assertSame(['Paginator'], $result);
  103. $this->Task->params['components'] = ' , Security, , Csrf';
  104. $result = $this->Task->getComponents();
  105. $this->assertSame(['Security', 'Csrf', 'Paginator'], $result);
  106. $this->Task->params['components'] = ' Paginator , Security, , Csrf';
  107. $result = $this->Task->getComponents();
  108. $this->assertSame(['Paginator', 'Security', 'Csrf'], $result);
  109. }
  110. /**
  111. * test helper generation
  112. *
  113. * @return void
  114. */
  115. public function testGetHelpers() {
  116. $result = $this->Task->getHelpers();
  117. $this->assertSame([], $result);
  118. $this->Task->params['helpers'] = ' , Session , , Number';
  119. $result = $this->Task->getHelpers();
  120. $this->assertSame(['Session', 'Number', 'Form'], $result);
  121. $this->Task->params['helpers'] = ' Session , Number , , Form';
  122. $result = $this->Task->getHelpers();
  123. $this->assertSame(['Session', 'Number', 'Form'], $result);
  124. }
  125. /**
  126. * test the bake method
  127. *
  128. * @return void
  129. */
  130. public function testBake() {
  131. $this->markTestIncomplete();
  132. $helpers = array('Js', 'Time');
  133. $components = array('Acl', 'Auth');
  134. $this->Task->expects($this->any())->method('createFile')->will($this->returnValue(true));
  135. $result = $this->Task->bake('Articles', null, $helpers, $components);
  136. $expected = file_get_contents(CAKE . 'Test' . DS . 'bake_compare' . DS . 'Controller' . DS . 'NoActions.ctp');
  137. $this->assertTextEquals($expected, $result);
  138. $result = $this->Task->bake('Articles', null, array(), array());
  139. $expected = file_get_contents(CAKE . 'Test' . DS . 'bake_compare' . DS . 'Controller' . DS . 'NoHelpersOrComponents.ctp');
  140. $this->assertTextEquals($expected, $result);
  141. $result = $this->Task->bake('Articles', 'scaffold', $helpers, $components);
  142. $expected = file_get_contents(CAKE . 'Test' . DS . 'bake_compare' . DS . 'Controller' . DS . 'Scaffold.ctp');
  143. $this->assertTextEquals($expected, $result);
  144. }
  145. /**
  146. * test bake() with a -plugin param
  147. *
  148. * @return void
  149. */
  150. public function testBakeWithPlugin() {
  151. $this->markTestIncomplete();
  152. $this->Task->plugin = 'ControllerTest';
  153. //fake plugin path
  154. Plugin::load('ControllerTest', array('path' => APP . 'Plugin/ControllerTest/'));
  155. $path = APP . 'Plugin/ControllerTest/Controller/ArticlesController.php';
  156. $this->Task->expects($this->at(1))->method('createFile')->with(
  157. $path,
  158. $this->anything()
  159. );
  160. $this->Task->expects($this->at(3))->method('createFile')->with(
  161. $path,
  162. $this->stringContains('ArticlesController extends ControllerTestAppController')
  163. )->will($this->returnValue(true));
  164. $this->Task->bake('Articles', '--actions--', array(), array(), array());
  165. $this->Task->plugin = 'ControllerTest';
  166. $path = APP . 'Plugin/ControllerTest/Controller/ArticlesController.php';
  167. $result = $this->Task->bake('Articles', '--actions--', array(), array(), array());
  168. $this->assertContains("App::uses('ControllerTestAppController', 'ControllerTest.Controller');", $result);
  169. $this->assertEquals('ControllerTest', $this->Task->Template->viewVars['plugin']);
  170. $this->assertEquals('ControllerTest.', $this->Task->Template->viewVars['pluginPath']);
  171. Plugin::unload();
  172. }
  173. /**
  174. * test that bakeActions is creating the correct controller Code. (Using sessions)
  175. *
  176. * @return void
  177. */
  178. public function testBakeActions() {
  179. TableRegistry::get('BakeArticles', [
  180. 'className' => __NAMESPACE__ . '\BakeArticlesTable'
  181. ]);
  182. $result = $this->Task->bakeActions('BakeArticles');
  183. $expected = file_get_contents(CORE_TESTS . 'bake_compare/Controller/Actions.ctp');
  184. $this->assertTextEquals($expected, $result);
  185. $result = $this->Task->bakeActions('BakeArticles', 'admin_', true);
  186. $this->assertContains('function admin_index() {', $result);
  187. $this->assertContains('function admin_add()', $result);
  188. $this->assertContains('function admin_view($id = null)', $result);
  189. $this->assertContains('function admin_edit($id = null)', $result);
  190. $this->assertContains('function admin_delete($id = null)', $result);
  191. }
  192. /**
  193. * test baking a test
  194. *
  195. * @return void
  196. */
  197. public function testBakeTest() {
  198. $this->Task->plugin = 'ControllerTest';
  199. $this->Task->connection = 'test';
  200. $this->Task->Test->expects($this->once())
  201. ->method('bake')
  202. ->with('Controller', 'BakeArticles');
  203. $this->Task->bakeTest('BakeArticles');
  204. $this->assertEquals($this->Task->plugin, $this->Task->Test->plugin);
  205. $this->assertEquals($this->Task->connection, $this->Task->Test->connection);
  206. }
  207. /**
  208. * test baking a test
  209. *
  210. * @return void
  211. */
  212. public function testBakeTestDisabled() {
  213. $this->Task->plugin = 'ControllerTest';
  214. $this->Task->connection = 'test';
  215. $this->Task->params['no-test'] = true;
  216. $this->Task->Test->expects($this->never())
  217. ->method('bake');
  218. $this->Task->bakeTest('BakeArticles');
  219. }
  220. /**
  221. * test that execute runs all when the first arg == all
  222. *
  223. * @return void
  224. */
  225. public function testExecuteIntoAll() {
  226. $this->markTestIncomplete();
  227. $count = count($this->Task->listAll('test'));
  228. if ($count != count($this->fixtures)) {
  229. $this->markTestSkipped('Additional tables detected.');
  230. }
  231. $this->Task->connection = 'test';
  232. $this->Task->path = '/my/path/';
  233. $this->Task->args = array('all');
  234. $this->Task->expects($this->any())->method('_checkUnitTest')->will($this->returnValue(true));
  235. $this->Task->Test->expects($this->once())->method('bake');
  236. $filename = '/my/path/BakeArticlesController.php';
  237. $this->Task->expects($this->once())->method('createFile')->with(
  238. $filename,
  239. $this->stringContains('class BakeArticlesController')
  240. )->will($this->returnValue(true));
  241. $this->Task->execute();
  242. }
  243. /**
  244. * Test execute() with all and --admin
  245. *
  246. * @return void
  247. */
  248. public function testExecuteIntoAllAdmin() {
  249. $this->markTestIncomplete();
  250. $count = count($this->Task->listAll('test'));
  251. if ($count != count($this->fixtures)) {
  252. $this->markTestSkipped('Additional tables detected.');
  253. }
  254. $this->Task->connection = 'test';
  255. $this->Task->path = '/my/path/';
  256. $this->Task->args = array('all');
  257. $this->Task->params['admin'] = true;
  258. $this->Task->Project->expects($this->any())
  259. ->method('getPrefix')
  260. ->will($this->returnValue('admin_'));
  261. $this->Task->expects($this->any())
  262. ->method('_checkUnitTest')
  263. ->will($this->returnValue(true));
  264. $this->Task->Test->expects($this->once())->method('bake');
  265. $filename = '/my/path/BakeArticlesController.php';
  266. $this->Task->expects($this->once())->method('createFile')->with(
  267. $filename,
  268. $this->stringContains('function admin_index')
  269. )->will($this->returnValue(true));
  270. $this->Task->execute();
  271. }
  272. /**
  273. * test that `cake bake controller foos` works.
  274. *
  275. * @return void
  276. */
  277. public function testExecuteWithController() {
  278. $this->markTestIncomplete();
  279. $this->Task->connection = 'test';
  280. $this->Task->path = '/my/path/';
  281. $this->Task->args = array('BakeArticles');
  282. $filename = '/my/path/BakeArticlesController.php';
  283. $this->Task->expects($this->once())->method('createFile')->with(
  284. $filename,
  285. $this->stringContains('$scaffold')
  286. );
  287. $this->Task->execute();
  288. }
  289. /**
  290. * data provider for testExecuteWithControllerNameVariations
  291. *
  292. * @return void
  293. */
  294. public static function nameVariations() {
  295. return array(
  296. array('BakeArticles'), array('BakeArticle'), array('bake_article'), array('bake_articles')
  297. );
  298. }
  299. /**
  300. * test that both plural and singular forms work for controller baking.
  301. *
  302. * @dataProvider nameVariations
  303. * @return void
  304. */
  305. public function testExecuteWithControllerNameVariations($name) {
  306. $this->markTestIncomplete();
  307. $this->Task->connection = 'test';
  308. $this->Task->path = '/my/path/';
  309. $this->Task->args = array($name);
  310. $filename = '/my/path/BakeArticlesController.php';
  311. $this->Task->expects($this->once())->method('createFile')->with(
  312. $filename, $this->stringContains('$scaffold')
  313. );
  314. $this->Task->execute();
  315. }
  316. /**
  317. * test that `cake bake controller foo scaffold` works.
  318. *
  319. * @return void
  320. */
  321. public function testExecuteWithPublicParam() {
  322. $this->markTestIncomplete();
  323. $this->Task->connection = 'test';
  324. $this->Task->path = '/my/path/';
  325. $this->Task->args = array('BakeArticles');
  326. $this->Task->params = array('public' => true);
  327. $filename = '/my/path/BakeArticlesController.php';
  328. $expected = new \PHPUnit_Framework_Constraint_Not($this->stringContains('$scaffold'));
  329. $this->Task->expects($this->once())->method('createFile')->with(
  330. $filename, $expected
  331. );
  332. $this->Task->execute();
  333. }
  334. /**
  335. * test that `cake bake controller foos both` works.
  336. *
  337. * @return void
  338. */
  339. public function testExecuteWithControllerAndBoth() {
  340. $this->markTestIncomplete();
  341. $this->Task->Project->expects($this->any())->method('getPrefix')->will($this->returnValue('admin_'));
  342. $this->Task->connection = 'test';
  343. $this->Task->path = '/my/path/';
  344. $this->Task->args = array('BakeArticles');
  345. $this->Task->params = array('public' => true, 'admin' => true);
  346. $filename = '/my/path/BakeArticlesController.php';
  347. $this->Task->expects($this->once())->method('createFile')->with(
  348. $filename, $this->stringContains('admin_index')
  349. );
  350. $this->Task->execute();
  351. }
  352. /**
  353. * test that `cake bake controller foos admin` works.
  354. *
  355. * @return void
  356. */
  357. public function testExecuteWithControllerAndAdmin() {
  358. $this->markTestIncomplete();
  359. $this->Task->Project->expects($this->any())->method('getPrefix')->will($this->returnValue('admin_'));
  360. $this->Task->connection = 'test';
  361. $this->Task->path = '/my/path/';
  362. $this->Task->args = array('BakeArticles');
  363. $this->Task->params = array('admin' => true);
  364. $filename = '/my/path/BakeArticlesController.php';
  365. $this->Task->expects($this->once())->method('createFile')->with(
  366. $filename, $this->stringContains('admin_index')
  367. );
  368. $this->Task->execute();
  369. }
  370. }