ControllerTaskTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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\Shell\Task;
  16. use Cake\Console\Shell;
  17. use Cake\Core\App;
  18. use Cake\Core\Plugin;
  19. use Cake\ORM\Table;
  20. use Cake\ORM\TableRegistry;
  21. use Cake\Shell\Task\ControllerTask;
  22. use Cake\Shell\Task\TemplateTask;
  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_articles', 'core.bake_articles_bake_tags', 'core.bake_comments', 'core.bake_tags'];
  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\Shell\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->Model = $this->getMock('Cake\Shell\Task\ModelTask',
  62. array('in', 'out', 'err', 'createFile', '_stop'),
  63. array($io)
  64. );
  65. $this->Task->Test = $this->getMock(
  66. 'Cake\Shell\Task\TestTask',
  67. [],
  68. [$io]
  69. );
  70. TableRegistry::get('BakeArticles', [
  71. 'className' => __NAMESPACE__ . '\BakeArticlesTable'
  72. ]);
  73. }
  74. /**
  75. * tearDown method
  76. *
  77. * @return void
  78. */
  79. public function tearDown() {
  80. unset($this->Task);
  81. TableRegistry::clear();
  82. parent::tearDown();
  83. }
  84. /**
  85. * test ListAll
  86. *
  87. * @return void
  88. */
  89. public function testListAll() {
  90. $count = count($this->Task->listAll('test'));
  91. if ($count != count($this->fixtures)) {
  92. $this->markTestSkipped('Additional tables detected.');
  93. }
  94. $result = $this->Task->listAll();
  95. $expected = array('bake_articles', 'bake_articles_bake_tags', 'bake_comments', 'bake_tags');
  96. $this->assertEquals($expected, $result);
  97. }
  98. /**
  99. * test component generation
  100. *
  101. * @return void
  102. */
  103. public function testGetComponents() {
  104. $result = $this->Task->getComponents();
  105. $this->assertSame([], $result);
  106. $this->Task->params['components'] = ' , Security, , Csrf';
  107. $result = $this->Task->getComponents();
  108. $this->assertSame(['Security', 'Csrf'], $result);
  109. }
  110. /**
  111. * test helper generation
  112. *
  113. * @return void
  114. */
  115. public function testGetHelpers() {
  116. $result = $this->Task->getHelpers();
  117. $this->assertSame([], $result);
  118. $this->Task->params['helpers'] = ' , Session , , Number';
  119. $result = $this->Task->getHelpers();
  120. $this->assertSame(['Session', 'Number'], $result);
  121. }
  122. /**
  123. * test the bake method
  124. *
  125. * @return void
  126. */
  127. public function testBakeNoActions() {
  128. $this->Task->expects($this->any())
  129. ->method('createFile')
  130. ->will($this->returnValue(true));
  131. $this->Task->params['no-actions'] = true;
  132. $this->Task->params['helpers'] = 'Html,Time';
  133. $this->Task->params['components'] = 'Csrf, Auth';
  134. $result = $this->Task->bake('BakeArticles');
  135. $expected = file_get_contents(CORE_TESTS . '/bake_compare/Controller/NoActions.ctp');
  136. $this->assertTextEquals($expected, $result);
  137. }
  138. /**
  139. * test bake with actions.
  140. *
  141. * @return void
  142. */
  143. public function testBakeActions() {
  144. $this->Task->params['helpers'] = 'Html,Time';
  145. $this->Task->params['components'] = 'Csrf, Auth';
  146. $filename = APP . 'Controller/BakeArticlesController.php';
  147. $this->Task->expects($this->at(1))
  148. ->method('createFile')
  149. ->with(
  150. $this->_normalizePath($filename),
  151. $this->stringContains('class BakeArticlesController')
  152. );
  153. $result = $this->Task->bake('BakeArticles');
  154. $this->assertTextContains('public function add(', $result);
  155. $this->assertTextContains('public function index(', $result);
  156. $this->assertTextContains('public function view(', $result);
  157. $this->assertTextContains('public function edit(', $result);
  158. $this->assertTextContains('public function delete(', $result);
  159. }
  160. /**
  161. * test bake actions prefixed.
  162. *
  163. * @return void
  164. */
  165. public function testBakePrefixed() {
  166. $this->Task->params['prefix'] = 'admin';
  167. $filename = $this->_normalizePath(APP . 'Controller/Admin/BakeArticlesController.php');
  168. $this->Task->expects($this->at(1))
  169. ->method('createFile')
  170. ->with($filename, $this->anything());
  171. $this->Task->Test->expects($this->at(0))
  172. ->method('bake')
  173. ->with('Controller', 'Admin\BakeArticles');
  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. Plugin::load('ControllerTest', array('path' => APP . 'Plugin/ControllerTest/'));
  186. $path = APP . 'Plugin/ControllerTest/src/Controller/BakeArticlesController.php';
  187. $this->Task->expects($this->at(1))
  188. ->method('createFile')
  189. ->with(
  190. $this->_normalizePath($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. *
  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 execute no args.
  239. *
  240. * @return void
  241. */
  242. public function testMainNoArgs() {
  243. $this->Task->expects($this->never())
  244. ->method('createFile');
  245. $this->Task->expects($this->at(0))
  246. ->method('out')
  247. ->with($this->stringContains('Possible controllers based on your current database'));
  248. $this->Task->main();
  249. }
  250. /**
  251. * test that execute runs all when the first arg == all
  252. *
  253. * @return void
  254. */
  255. public function testMainIntoAll() {
  256. $count = count($this->Task->listAll());
  257. if ($count != count($this->fixtures)) {
  258. $this->markTestSkipped('Additional tables detected.');
  259. }
  260. $this->Task->connection = 'test';
  261. $this->Task->params = ['helpers' => 'Time,Text'];
  262. $this->Task->Test->expects($this->atLeastOnce())
  263. ->method('bake');
  264. $filename = $this->_normalizePath(APP . 'Controller/BakeArticlesController.php');
  265. $this->Task->expects($this->at(1))
  266. ->method('createFile')
  267. ->with($filename, $this->logicalAnd(
  268. $this->stringContains('class BakeArticlesController'),
  269. $this->stringContains("\$helpers = ['Time', 'Text']")
  270. ))
  271. ->will($this->returnValue(true));
  272. $this->Task->all();
  273. }
  274. /**
  275. * data provider for testMainWithControllerNameVariations
  276. *
  277. * @return void
  278. */
  279. public static function nameVariations() {
  280. return array(
  281. array('BakeArticles'), array('bake_articles')
  282. );
  283. }
  284. /**
  285. * test that both plural and singular forms work for controller baking.
  286. *
  287. * @dataProvider nameVariations
  288. * @return void
  289. */
  290. public function testMainWithControllerNameVariations($name) {
  291. $this->Task->connection = 'test';
  292. $filename = $this->_normalizePath(APP . 'Controller/BakeArticlesController.php');
  293. $this->Task->expects($this->once())
  294. ->method('createFile')
  295. ->with($filename, $this->stringContains('public function index()'));
  296. $this->Task->main($name);
  297. }
  298. /**
  299. * test main with plugin.name
  300. *
  301. * @return void
  302. */
  303. public function testMainWithPluginDot() {
  304. $this->Task->connection = 'test';
  305. Plugin::load('ControllerTest', array('path' => APP . 'Plugin/ControllerTest/'));
  306. $path = APP . 'Plugin/ControllerTest/src/Controller/BakeArticlesController.php';
  307. $this->Task->expects($this->at(1))
  308. ->method('createFile')
  309. ->with(
  310. $this->_normalizePath($path),
  311. $this->stringContains('BakeArticlesController extends AppController')
  312. )->will($this->returnValue(true));
  313. $this->Task->main('ControllerTest.BakeArticles');
  314. }
  315. }