ControllerTaskTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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 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. $this->assertSameAsFile(__FUNCTION__ . '.php', $result);
  139. }
  140. /**
  141. * test bake with actions.
  142. *
  143. * @return void
  144. */
  145. public function testBakeActions() {
  146. $this->Task->params['helpers'] = 'Html,Time';
  147. $this->Task->params['components'] = 'Csrf, Auth';
  148. $filename = APP . 'Controller/BakeArticlesController.php';
  149. $this->Task->expects($this->at(1))
  150. ->method('createFile')
  151. ->with(
  152. $this->_normalizePath($filename),
  153. $this->stringContains('class BakeArticlesController')
  154. );
  155. $result = $this->Task->bake('BakeArticles');
  156. $this->assertSameAsFile(__FUNCTION__ . '.php', $result);
  157. }
  158. /**
  159. * test bake actions prefixed.
  160. *
  161. * @return void
  162. */
  163. public function testBakePrefixed() {
  164. $this->Task->params['prefix'] = 'admin';
  165. $filename = $this->_normalizePath(APP . 'Controller/Admin/BakeArticlesController.php');
  166. $this->Task->expects($this->at(1))
  167. ->method('createFile')
  168. ->with($filename, $this->anything());
  169. $this->Task->Test->expects($this->at(0))
  170. ->method('bake')
  171. ->with('Controller', 'Admin\BakeArticles');
  172. $result = $this->Task->bake('BakeArticles');
  173. $this->assertTextContains('namespace App\Controller\Admin;', $result);
  174. $this->assertTextContains('use App\Controller\AppController;', $result);
  175. }
  176. /**
  177. * test bake() with a -plugin param
  178. *
  179. * @return void
  180. */
  181. public function testBakeWithPlugin() {
  182. $this->Task->plugin = 'ControllerTest';
  183. Plugin::load('ControllerTest', array('path' => APP . 'Plugin/ControllerTest/'));
  184. $path = APP . 'Plugin/ControllerTest/src/Controller/BakeArticlesController.php';
  185. $this->Task->expects($this->at(1))
  186. ->method('createFile')
  187. ->with(
  188. $this->_normalizePath($path),
  189. $this->stringContains('BakeArticlesController extends AppController')
  190. )->will($this->returnValue(true));
  191. $result = $this->Task->bake('BakeArticles');
  192. $this->assertSameAsFile(__FUNCTION__ . '.php', $result);
  193. Plugin::unload();
  194. }
  195. /**
  196. *
  197. * test that bakeActions is creating the correct controller Code. (Using sessions)
  198. *
  199. * @return void
  200. */
  201. public function testBakeActionsContent() {
  202. $result = $this->Task->bake('BakeArticles');
  203. $this->assertSameAsFile(__FUNCTION__ . '.php', $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 execute no args.
  235. *
  236. * @return void
  237. */
  238. public function testMainNoArgs() {
  239. $this->Task->expects($this->never())
  240. ->method('createFile');
  241. $this->Task->expects($this->at(0))
  242. ->method('out')
  243. ->with($this->stringContains('Possible controllers based on your current database'));
  244. $this->Task->main();
  245. }
  246. /**
  247. * test that execute runs all when the first arg == all
  248. *
  249. * @return void
  250. */
  251. public function testMainIntoAll() {
  252. $count = count($this->Task->listAll());
  253. if ($count != count($this->fixtures)) {
  254. $this->markTestSkipped('Additional tables detected.');
  255. }
  256. $this->Task->connection = 'test';
  257. $this->Task->params = ['helpers' => 'Time,Text'];
  258. $this->Task->Test->expects($this->atLeastOnce())
  259. ->method('bake');
  260. $filename = $this->_normalizePath(APP . 'Controller/BakeArticlesController.php');
  261. $this->Task->expects($this->at(1))
  262. ->method('createFile')
  263. ->with($filename, $this->logicalAnd(
  264. $this->stringContains('class BakeArticlesController'),
  265. $this->stringContains("\$helpers = ['Time', 'Text']")
  266. ))
  267. ->will($this->returnValue(true));
  268. $this->Task->all();
  269. }
  270. /**
  271. * data provider for testMainWithControllerNameVariations
  272. *
  273. * @return void
  274. */
  275. public static function nameVariations() {
  276. return array(
  277. array('BakeArticles'), array('bake_articles')
  278. );
  279. }
  280. /**
  281. * test that both plural and singular forms work for controller baking.
  282. *
  283. * @dataProvider nameVariations
  284. * @return void
  285. */
  286. public function testMainWithControllerNameVariations($name) {
  287. $this->Task->connection = 'test';
  288. $filename = $this->_normalizePath(APP . 'Controller/BakeArticlesController.php');
  289. $this->Task->expects($this->once())
  290. ->method('createFile')
  291. ->with($filename, $this->stringContains('public function index()'));
  292. $this->Task->main($name);
  293. }
  294. /**
  295. * test main with plugin.name
  296. *
  297. * @return void
  298. */
  299. public function testMainWithPluginDot() {
  300. $this->Task->connection = 'test';
  301. Plugin::load('ControllerTest', array('path' => APP . 'Plugin/ControllerTest/'));
  302. $path = APP . 'Plugin/ControllerTest/src/Controller/BakeArticlesController.php';
  303. $this->Task->expects($this->at(1))
  304. ->method('createFile')
  305. ->with(
  306. $this->_normalizePath($path),
  307. $this->stringContains('BakeArticlesController extends AppController')
  308. )->will($this->returnValue(true));
  309. $this->Task->main('ControllerTest.BakeArticles');
  310. }
  311. }