ControllerTaskTest.php 9.1 KB

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