ControllerTaskTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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\StringCompareTrait;
  24. use Cake\TestSuite\TestCase;
  25. use Cake\View\Helper;
  26. /**
  27. * Class BakeArticle
  28. */
  29. class BakeArticlesTable extends Table {
  30. public function initialize(array $config) {
  31. $this->belongsTo('BakeUsers');
  32. $this->hasMany('BakeComments');
  33. $this->belongsToMany('BakeTags');
  34. }
  35. }
  36. /**
  37. * ControllerTaskTest class
  38. *
  39. */
  40. class ControllerTaskTest extends TestCase {
  41. use StringCompareTrait;
  42. /**
  43. * fixtures
  44. *
  45. * @var array
  46. */
  47. public $fixtures = ['core.bake_articles', 'core.bake_articles_bake_tags', 'core.bake_comments', 'core.bake_tags'];
  48. /**
  49. * setUp method
  50. *
  51. * @return void
  52. */
  53. public function setUp() {
  54. parent::setUp();
  55. $this->_compareBasePath = CORE_TESTS . 'bake_compare' . DS . 'Controller' . DS;
  56. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  57. $this->Task = $this->getMock('Cake\Shell\Task\ControllerTask',
  58. array('in', 'out', 'err', 'hr', 'createFile', '_stop'),
  59. array($io)
  60. );
  61. $this->Task->name = 'Controller';
  62. $this->Task->connection = 'test';
  63. $this->Task->Template = new TemplateTask($io);
  64. $this->Task->Model = $this->getMock('Cake\Shell\Task\ModelTask',
  65. array('in', 'out', 'err', 'createFile', '_stop'),
  66. array($io)
  67. );
  68. $this->Task->Test = $this->getMock(
  69. 'Cake\Shell\Task\TestTask',
  70. [],
  71. [$io]
  72. );
  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 bake with various component name variants
  127. *
  128. * @return void
  129. */
  130. public function testBakeComponents() {
  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['components'] = 'Csrf, Auth, Company/TestPluginThree.Something, TestPlugin.Other, Apple, NonExistent';
  136. $result = $this->Task->bake('BakeArticles');
  137. $this->assertSameAsFile(__FUNCTION__ . '.php', $result);
  138. }
  139. /**
  140. * test the bake method
  141. *
  142. * @return void
  143. */
  144. public function testBakeNoActions() {
  145. $this->Task->expects($this->any())
  146. ->method('createFile')
  147. ->will($this->returnValue(true));
  148. $this->Task->params['no-actions'] = true;
  149. $this->Task->params['helpers'] = 'Html,Time';
  150. $this->Task->params['components'] = 'Csrf, Auth';
  151. $result = $this->Task->bake('BakeArticles');
  152. $this->assertSameAsFile(__FUNCTION__ . '.php', $result);
  153. }
  154. /**
  155. * test bake with actions.
  156. *
  157. * @return void
  158. */
  159. public function testBakeActions() {
  160. $this->Task->params['helpers'] = 'Html,Time';
  161. $this->Task->params['components'] = 'Csrf, Auth';
  162. $filename = APP . 'Controller/BakeArticlesController.php';
  163. $this->Task->expects($this->at(1))
  164. ->method('createFile')
  165. ->with(
  166. $this->_normalizePath($filename),
  167. $this->stringContains('class BakeArticlesController')
  168. );
  169. $result = $this->Task->bake('BakeArticles');
  170. $this->assertSameAsFile(__FUNCTION__ . '.php', $result);
  171. }
  172. /**
  173. * test bake actions prefixed.
  174. *
  175. * @return void
  176. */
  177. public function testBakePrefixed() {
  178. $this->Task->params['prefix'] = 'admin';
  179. $filename = $this->_normalizePath(APP . 'Controller/Admin/BakeArticlesController.php');
  180. $this->Task->expects($this->at(1))
  181. ->method('createFile')
  182. ->with($filename, $this->anything());
  183. $this->Task->Test->expects($this->at(0))
  184. ->method('bake')
  185. ->with('Controller', 'Admin\BakeArticles');
  186. $result = $this->Task->bake('BakeArticles');
  187. $this->assertTextContains('namespace App\Controller\Admin;', $result);
  188. $this->assertTextContains('use App\Controller\AppController;', $result);
  189. }
  190. /**
  191. * test bake() with a -plugin param
  192. *
  193. * @return void
  194. */
  195. public function testBakeWithPlugin() {
  196. $this->Task->plugin = 'ControllerTest';
  197. Plugin::load('ControllerTest', array('path' => APP . 'Plugin/ControllerTest/'));
  198. $path = APP . 'Plugin/ControllerTest/src/Controller/BakeArticlesController.php';
  199. $this->Task->expects($this->at(1))
  200. ->method('createFile')
  201. ->with(
  202. $this->_normalizePath($path),
  203. $this->stringContains('BakeArticlesController extends AppController')
  204. )->will($this->returnValue(true));
  205. $result = $this->Task->bake('BakeArticles');
  206. $this->assertSameAsFile(__FUNCTION__ . '.php', $result);
  207. Plugin::unload();
  208. }
  209. /**
  210. *
  211. * test that bakeActions is creating the correct controller Code. (Using sessions)
  212. *
  213. * @return void
  214. */
  215. public function testBakeActionsContent() {
  216. $result = $this->Task->bake('BakeArticles');
  217. $this->assertSameAsFile(__FUNCTION__ . '.php', $result);
  218. }
  219. /**
  220. * test baking a test
  221. *
  222. * @return void
  223. */
  224. public function testBakeTest() {
  225. $this->Task->plugin = 'ControllerTest';
  226. $this->Task->connection = 'test';
  227. $this->Task->Test->expects($this->once())
  228. ->method('bake')
  229. ->with('Controller', 'BakeArticles');
  230. $this->Task->bakeTest('BakeArticles');
  231. $this->assertEquals($this->Task->plugin, $this->Task->Test->plugin);
  232. $this->assertEquals($this->Task->connection, $this->Task->Test->connection);
  233. }
  234. /**
  235. * test baking a test
  236. *
  237. * @return void
  238. */
  239. public function testBakeTestDisabled() {
  240. $this->Task->plugin = 'ControllerTest';
  241. $this->Task->connection = 'test';
  242. $this->Task->params['no-test'] = true;
  243. $this->Task->Test->expects($this->never())
  244. ->method('bake');
  245. $this->Task->bakeTest('BakeArticles');
  246. }
  247. /**
  248. * Test execute no args.
  249. *
  250. * @return void
  251. */
  252. public function testMainNoArgs() {
  253. $this->Task->expects($this->never())
  254. ->method('createFile');
  255. $this->Task->expects($this->at(0))
  256. ->method('out')
  257. ->with($this->stringContains('Possible controllers based on your current database'));
  258. $this->Task->main();
  259. }
  260. /**
  261. * test that execute runs all when the first arg == all
  262. *
  263. * @return void
  264. */
  265. public function testMainIntoAll() {
  266. $count = count($this->Task->listAll());
  267. if ($count != count($this->fixtures)) {
  268. $this->markTestSkipped('Additional tables detected.');
  269. }
  270. $this->Task->connection = 'test';
  271. $this->Task->params = ['helpers' => 'Time,Text'];
  272. $this->Task->Test->expects($this->atLeastOnce())
  273. ->method('bake');
  274. $filename = $this->_normalizePath(APP . 'Controller/BakeArticlesController.php');
  275. $this->Task->expects($this->at(1))
  276. ->method('createFile')
  277. ->with($filename, $this->logicalAnd(
  278. $this->stringContains('class BakeArticlesController'),
  279. $this->stringContains("\$helpers = ['Time', 'Text']")
  280. ))
  281. ->will($this->returnValue(true));
  282. $this->Task->all();
  283. }
  284. /**
  285. * data provider for testMainWithControllerNameVariations
  286. *
  287. * @return void
  288. */
  289. public static function nameVariations() {
  290. return array(
  291. array('BakeArticles'), array('bake_articles')
  292. );
  293. }
  294. /**
  295. * test that both plural and singular forms work for controller baking.
  296. *
  297. * @dataProvider nameVariations
  298. * @return void
  299. */
  300. public function testMainWithControllerNameVariations($name) {
  301. $this->Task->connection = 'test';
  302. $filename = $this->_normalizePath(APP . 'Controller/BakeArticlesController.php');
  303. $this->Task->expects($this->once())
  304. ->method('createFile')
  305. ->with($filename, $this->stringContains('public function index()'));
  306. $this->Task->main($name);
  307. }
  308. /**
  309. * test main with plugin.name
  310. *
  311. * @return void
  312. */
  313. public function testMainWithPluginDot() {
  314. $this->Task->connection = 'test';
  315. Plugin::load('ControllerTest', array('path' => APP . 'Plugin/ControllerTest/'));
  316. $path = APP . 'Plugin/ControllerTest/src/Controller/BakeArticlesController.php';
  317. $this->Task->expects($this->at(1))
  318. ->method('createFile')
  319. ->with(
  320. $this->_normalizePath($path),
  321. $this->stringContains('BakeArticlesController extends AppController')
  322. )->will($this->returnValue(true));
  323. $this->Task->main('ControllerTest.BakeArticles');
  324. }
  325. }