ControllerTaskTest.php 8.8 KB

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