ControllerTaskTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 = '/my/path/Admin/BakeArticlesController.php';
  167. $this->Task->expects($this->at(1))
  168. ->method('createFile')
  169. ->with($filename, $this->anything());
  170. $result = $this->Task->bake('BakeArticles');
  171. $this->assertTextContains('namespace App\Controller\Admin;', $result);
  172. $this->assertTextContains('use App\Controller\AppController;', $result);
  173. }
  174. /**
  175. * test bake() with a -plugin param
  176. *
  177. * @return void
  178. */
  179. public function testBakeWithPlugin() {
  180. $this->Task->plugin = 'ControllerTest';
  181. //fake plugin path
  182. Plugin::load('ControllerTest', array('path' => APP . 'Plugin/ControllerTest/'));
  183. $path = APP . 'Plugin/ControllerTest/Controller/BakeArticlesController.php';
  184. $this->Task->expects($this->at(1))
  185. ->method('createFile')
  186. ->with(
  187. $path,
  188. $this->stringContains('BakeArticlesController extends AppController')
  189. )->will($this->returnValue(true));
  190. $result = $this->Task->bake('BakeArticles');
  191. $this->assertContains('namespace ControllerTest\Controller;', $result);
  192. $this->assertContains('use ControllerTest\Controller\AppController;', $result);
  193. Plugin::unload();
  194. }
  195. /**
  196. * test that bakeActions is creating the correct controller Code. (Using sessions)
  197. *
  198. * @return void
  199. */
  200. public function testBakeActionsContent() {
  201. $result = $this->Task->bakeActions('BakeArticles');
  202. $expected = file_get_contents(CORE_TESTS . 'bake_compare/Controller/Actions.ctp');
  203. $this->assertTextEquals($expected, $result);
  204. }
  205. /**
  206. * test baking a test
  207. *
  208. * @return void
  209. */
  210. public function testBakeTest() {
  211. $this->Task->plugin = 'ControllerTest';
  212. $this->Task->connection = 'test';
  213. $this->Task->Test->expects($this->once())
  214. ->method('bake')
  215. ->with('Controller', 'BakeArticles');
  216. $this->Task->bakeTest('BakeArticles');
  217. $this->assertEquals($this->Task->plugin, $this->Task->Test->plugin);
  218. $this->assertEquals($this->Task->connection, $this->Task->Test->connection);
  219. }
  220. /**
  221. * test baking a test
  222. *
  223. * @return void
  224. */
  225. public function testBakeTestDisabled() {
  226. $this->Task->plugin = 'ControllerTest';
  227. $this->Task->connection = 'test';
  228. $this->Task->params['no-test'] = true;
  229. $this->Task->Test->expects($this->never())
  230. ->method('bake');
  231. $this->Task->bakeTest('BakeArticles');
  232. }
  233. /**
  234. * test that execute runs all when the first arg == all
  235. *
  236. * @return void
  237. */
  238. public function testExecuteIntoAll() {
  239. $count = count($this->Task->listAll());
  240. if ($count != count($this->fixtures)) {
  241. $this->markTestSkipped('Additional tables detected.');
  242. }
  243. $this->Task->connection = 'test';
  244. $this->Task->path = '/my/path/';
  245. $this->Task->args = ['all'];
  246. $this->Task->params = ['helpers' => 'Time,Text'];
  247. $this->Task->Test->expects($this->atLeastOnce())
  248. ->method('bake');
  249. $filename = '/my/path/BakeArticlesController.php';
  250. $this->Task->expects($this->at(1))
  251. ->method('createFile')
  252. ->with($filename, $this->logicalAnd(
  253. $this->stringContains('class BakeArticlesController'),
  254. $this->stringContains("\$helpers = ['Time', 'Text']")
  255. ))
  256. ->will($this->returnValue(true));
  257. $this->Task->execute();
  258. }
  259. /**
  260. * data provider for testExecuteWithControllerNameVariations
  261. *
  262. * @return void
  263. */
  264. public static function nameVariations() {
  265. return array(
  266. array('BakeArticles'), array('BakeArticle'), array('bake_article'), array('bake_articles')
  267. );
  268. }
  269. /**
  270. * test that both plural and singular forms work for controller baking.
  271. *
  272. * @dataProvider nameVariations
  273. * @return void
  274. */
  275. public function testExecuteWithControllerNameVariations($name) {
  276. $this->Task->connection = 'test';
  277. $this->Task->path = '/my/path/';
  278. $this->Task->args = [$name];
  279. $filename = '/my/path/BakeArticlesController.php';
  280. $this->Task->expects($this->once())
  281. ->method('createFile')
  282. ->with($filename, $this->stringContains('public function index()'));
  283. $this->Task->execute();
  284. }
  285. }