ControllerTaskTest.php 9.1 KB

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