ControllerTaskTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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->belongsTo('BakeUsers');
  31. $this->hasMany('BakeComments');
  32. $this->belongsToMany('BakeTags');
  33. }
  34. }
  35. /**
  36. * ControllerTaskTest class
  37. *
  38. */
  39. class ControllerTaskTest extends TestCase {
  40. /**
  41. * fixtures
  42. *
  43. * @var array
  44. */
  45. public $fixtures = ['core.bake_article', 'core.bake_articles_bake_tag', 'core.bake_comment', 'core.bake_tag'];
  46. /**
  47. * setUp method
  48. *
  49. * @return void
  50. */
  51. public function setUp() {
  52. parent::setUp();
  53. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  54. $this->Task = $this->getMock('Cake\Console\Command\Task\ControllerTask',
  55. array('in', 'out', 'err', 'hr', 'createFile', '_stop'),
  56. array($io)
  57. );
  58. $this->Task->name = 'Controller';
  59. $this->Task->connection = 'test';
  60. $this->Task->Template = new TemplateTask($io);
  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($io)
  65. );
  66. $this->Task->Test = $this->getMock(
  67. 'Cake\Console\Command\Task\TestTask',
  68. [],
  69. [$io]
  70. );
  71. TableRegistry::get('BakeArticles', [
  72. 'className' => __NAMESPACE__ . '\BakeArticlesTable'
  73. ]);
  74. }
  75. /**
  76. * tearDown method
  77. *
  78. * @return void
  79. */
  80. public function tearDown() {
  81. unset($this->Task);
  82. TableRegistry::clear();
  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 component generation
  101. *
  102. * @return void
  103. */
  104. public function testGetComponents() {
  105. $result = $this->Task->getComponents();
  106. $this->assertSame([], $result);
  107. $this->Task->params['components'] = ' , Security, , Csrf';
  108. $result = $this->Task->getComponents();
  109. $this->assertSame(['Security', 'Csrf'], $result);
  110. }
  111. /**
  112. * test helper generation
  113. *
  114. * @return void
  115. */
  116. public function testGetHelpers() {
  117. $result = $this->Task->getHelpers();
  118. $this->assertSame([], $result);
  119. $this->Task->params['helpers'] = ' , Session , , Number';
  120. $result = $this->Task->getHelpers();
  121. $this->assertSame(['Session', 'Number'], $result);
  122. }
  123. /**
  124. * test the bake method
  125. *
  126. * @return void
  127. */
  128. public function testBakeNoActions() {
  129. $this->Task->expects($this->any())
  130. ->method('createFile')
  131. ->will($this->returnValue(true));
  132. $this->Task->params['no-actions'] = true;
  133. $this->Task->params['helpers'] = 'Html,Time';
  134. $this->Task->params['components'] = 'Csrf, Auth';
  135. $result = $this->Task->bake('BakeArticles');
  136. $expected = file_get_contents(CORE_TESTS . '/bake_compare/Controller/NoActions.ctp');
  137. $this->assertTextEquals($expected, $result);
  138. }
  139. /**
  140. * test bake with actions.
  141. *
  142. * @return void
  143. */
  144. public function testBakeActions() {
  145. $this->Task->params['helpers'] = 'Html,Time';
  146. $this->Task->params['components'] = 'Csrf, Auth';
  147. $filename = APP . 'Controller/BakeArticlesController.php';
  148. $this->Task->expects($this->at(1))
  149. ->method('createFile')
  150. ->with(
  151. $this->_normalizePath($filename),
  152. $this->stringContains('class BakeArticlesController')
  153. );
  154. $result = $this->Task->bake('BakeArticles');
  155. $this->assertTextContains('public function add(', $result);
  156. $this->assertTextContains('public function index(', $result);
  157. $this->assertTextContains('public function view(', $result);
  158. $this->assertTextContains('public function edit(', $result);
  159. $this->assertTextContains('public function delete(', $result);
  160. }
  161. /**
  162. * test bake actions prefixed.
  163. *
  164. * @return void
  165. */
  166. public function testBakePrefixed() {
  167. $this->Task->params['prefix'] = 'Admin';
  168. $filename = $this->_normalizePath(APP . 'Controller/Admin/BakeArticlesController.php');
  169. $this->Task->expects($this->at(1))
  170. ->method('createFile')
  171. ->with($filename, $this->anything());
  172. $this->Task->Test->expects($this->at(0))
  173. ->method('bake')
  174. ->with('Controller', 'Admin\BakeArticles');
  175. $result = $this->Task->bake('BakeArticles');
  176. $this->assertTextContains('namespace App\Controller\Admin;', $result);
  177. $this->assertTextContains('use App\Controller\AppController;', $result);
  178. }
  179. /**
  180. * test bake() with a -plugin param
  181. *
  182. * @return void
  183. */
  184. public function testBakeWithPlugin() {
  185. $this->Task->plugin = 'ControllerTest';
  186. //fake plugin path
  187. Plugin::load('ControllerTest', array('path' => APP . 'Plugin/ControllerTest/'));
  188. $path = APP . 'Plugin/ControllerTest/Controller/BakeArticlesController.php';
  189. $this->Task->expects($this->at(1))
  190. ->method('createFile')
  191. ->with(
  192. $this->_normalizePath($path),
  193. $this->stringContains('BakeArticlesController extends AppController')
  194. )->will($this->returnValue(true));
  195. $result = $this->Task->bake('BakeArticles');
  196. $this->assertContains('namespace ControllerTest\Controller;', $result);
  197. $this->assertContains('use ControllerTest\Controller\AppController;', $result);
  198. Plugin::unload();
  199. }
  200. /**
  201. * test that bakeActions is creating the correct controller Code. (Using sessions)
  202. *
  203. * @return void
  204. */
  205. public function testBakeActionsContent() {
  206. $result = $this->Task->bakeActions('BakeArticles');
  207. $expected = file_get_contents(CORE_TESTS . 'bake_compare/Controller/Actions.ctp');
  208. $this->assertTextEquals($expected, $result);
  209. }
  210. /**
  211. * test baking a test
  212. *
  213. * @return void
  214. */
  215. public function testBakeTest() {
  216. $this->Task->plugin = 'ControllerTest';
  217. $this->Task->connection = 'test';
  218. $this->Task->Test->expects($this->once())
  219. ->method('bake')
  220. ->with('Controller', 'BakeArticles');
  221. $this->Task->bakeTest('BakeArticles');
  222. $this->assertEquals($this->Task->plugin, $this->Task->Test->plugin);
  223. $this->assertEquals($this->Task->connection, $this->Task->Test->connection);
  224. }
  225. /**
  226. * test baking a test
  227. *
  228. * @return void
  229. */
  230. public function testBakeTestDisabled() {
  231. $this->Task->plugin = 'ControllerTest';
  232. $this->Task->connection = 'test';
  233. $this->Task->params['no-test'] = true;
  234. $this->Task->Test->expects($this->never())
  235. ->method('bake');
  236. $this->Task->bakeTest('BakeArticles');
  237. }
  238. /**
  239. * Test execute no args.
  240. *
  241. * @return void
  242. */
  243. public function testExecuteNoArgs() {
  244. $this->Task->expects($this->never())
  245. ->method('createFile');
  246. $this->Task->expects($this->at(0))
  247. ->method('out')
  248. ->with($this->stringContains('Possible controllers based on your current database'));
  249. $this->Task->execute();
  250. }
  251. /**
  252. * test that execute runs all when the first arg == all
  253. *
  254. * @return void
  255. */
  256. public function testExecuteIntoAll() {
  257. $count = count($this->Task->listAll());
  258. if ($count != count($this->fixtures)) {
  259. $this->markTestSkipped('Additional tables detected.');
  260. }
  261. $this->Task->connection = 'test';
  262. $this->Task->params = ['helpers' => 'Time,Text'];
  263. $this->Task->Test->expects($this->atLeastOnce())
  264. ->method('bake');
  265. $filename = $this->_normalizePath(APP . 'Controller/BakeArticlesController.php');
  266. $this->Task->expects($this->at(1))
  267. ->method('createFile')
  268. ->with($filename, $this->logicalAnd(
  269. $this->stringContains('class BakeArticlesController'),
  270. $this->stringContains("\$helpers = ['Time', 'Text']")
  271. ))
  272. ->will($this->returnValue(true));
  273. $this->Task->execute('all');
  274. }
  275. /**
  276. * data provider for testExecuteWithControllerNameVariations
  277. *
  278. * @return void
  279. */
  280. public static function nameVariations() {
  281. return array(
  282. array('BakeArticles'), array('BakeArticle'), array('bake_article'), array('bake_articles')
  283. );
  284. }
  285. /**
  286. * test that both plural and singular forms work for controller baking.
  287. *
  288. * @dataProvider nameVariations
  289. * @return void
  290. */
  291. public function testExecuteWithControllerNameVariations($name) {
  292. $this->Task->connection = 'test';
  293. $filename = $this->_normalizePath(APP . 'Controller/BakeArticlesController.php');
  294. $this->Task->expects($this->once())
  295. ->method('createFile')
  296. ->with($filename, $this->stringContains('public function index()'));
  297. $this->Task->execute($name);
  298. }
  299. }