ModelTaskTest.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. <?php
  2. /**
  3. * CakePHP : 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 Project
  12. * @since 1.2.6
  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\ModelTask;
  17. use Cake\Console\Command\Task\TemplateTask;
  18. use Cake\Core\Plugin;
  19. use Cake\Model\Model;
  20. use Cake\ORM\TableRegistry;
  21. use Cake\TestSuite\TestCase;
  22. use Cake\Utility\ClassRegistry;
  23. use Cake\Utility\Inflector;
  24. /**
  25. * ModelTaskTest class
  26. */
  27. class ModelTaskTest extends TestCase {
  28. /**
  29. * fixtures
  30. *
  31. * @var array
  32. */
  33. public $fixtures = array(
  34. 'core.bake_article', 'core.bake_comment', 'core.bake_articles_bake_tag',
  35. 'core.bake_tag', 'core.user', 'core.category_thread', 'core.number_tree',
  36. 'core.counter_cache_user'
  37. );
  38. /**
  39. * setUp method
  40. *
  41. * @return void
  42. */
  43. public function setUp() {
  44. parent::setUp();
  45. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  46. $this->Task = $this->getMock('Cake\Console\Command\Task\ModelTask',
  47. array('in', 'err', 'createFile', '_stop', '_checkUnitTest'),
  48. array($io)
  49. );
  50. $this->Task->connection = 'test';
  51. $this->_setupOtherMocks();
  52. TableRegistry::clear();
  53. }
  54. /**
  55. * Setup a mock that has out mocked. Normally this is not used as it makes $this->at() really tricky.
  56. *
  57. * @return void
  58. */
  59. protected function _useMockedOut() {
  60. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  61. $this->Task = $this->getMock('Cake\Console\Command\Task\ModelTask',
  62. array('in', 'out', 'err', 'hr', 'createFile', '_stop', '_checkUnitTest'),
  63. array($io)
  64. );
  65. $this->_setupOtherMocks();
  66. }
  67. /**
  68. * sets up the rest of the dependencies for Model Task
  69. *
  70. * @return void
  71. */
  72. protected function _setupOtherMocks() {
  73. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  74. $this->Task->Fixture = $this->getMock('Cake\Console\Command\Task\FixtureTask', [], [$io]);
  75. $this->Task->Test = $this->getMock('Cake\Console\Command\Task\FixtureTask', [], [$io]);
  76. $this->Task->Template = new TemplateTask($io);
  77. $this->Task->Template->interactive = false;
  78. $this->Task->name = 'Model';
  79. }
  80. /**
  81. * tearDown method
  82. *
  83. * @return void
  84. */
  85. public function tearDown() {
  86. parent::tearDown();
  87. unset($this->Task);
  88. $this->fixtureManager->shutDown();
  89. }
  90. /**
  91. * Test that listAll uses the connection property
  92. *
  93. * @return void
  94. */
  95. public function testListAllConnection() {
  96. $this->_useMockedOut();
  97. $this->Task->connection = 'test';
  98. $result = $this->Task->listAll();
  99. $this->assertContains('bake_articles', $result);
  100. $this->assertContains('bake_articles_bake_tags', $result);
  101. $this->assertContains('bake_tags', $result);
  102. $this->assertContains('bake_comments', $result);
  103. $this->assertContains('category_threads', $result);
  104. }
  105. /**
  106. * Test getName() method.
  107. *
  108. * @return void
  109. */
  110. public function testGetTable() {
  111. $result = $this->Task->getTable('BakeArticle');
  112. $this->assertEquals('bake_articles', $result);
  113. $result = $this->Task->getTable('BakeArticles');
  114. $this->assertEquals('bake_articles', $result);
  115. $this->Task->params['table'] = 'bake_articles';
  116. $result = $this->Task->getTable('Article');
  117. $this->assertEquals('bake_articles', $result);
  118. }
  119. /**
  120. * Test getting the a table class.
  121. *
  122. * @return void
  123. */
  124. public function testGetTableObject() {
  125. $result = $this->Task->getTableObject('Article', 'bake_articles');
  126. $this->assertInstanceOf('Cake\ORM\Table', $result);
  127. $this->assertEquals('bake_articles', $result->table());
  128. $this->assertEquals('Article', $result->alias());
  129. }
  130. /**
  131. * Test getAssociations with off flag.
  132. *
  133. * @return void
  134. */
  135. public function testGetAssociationsNoFlag() {
  136. $this->Task->params['no-associations'] = true;
  137. $articles = TableRegistry::get('BakeArticle');
  138. $this->assertEquals([], $this->Task->getAssociations($articles));
  139. }
  140. /**
  141. * Test getAssociations
  142. *
  143. * @return void
  144. */
  145. public function testGetAssociations() {
  146. $articles = TableRegistry::get('BakeArticles');
  147. $result = $this->Task->getAssociations($articles);
  148. $expected = [
  149. 'belongsTo' => [
  150. [
  151. 'alias' => 'BakeUsers',
  152. 'foreignKey' => 'bake_user_id',
  153. ],
  154. ],
  155. 'hasMany' => [
  156. [
  157. 'alias' => 'BakeComments',
  158. 'foreignKey' => 'bake_article_id',
  159. ],
  160. ],
  161. 'belongsToMany' => [
  162. [
  163. 'alias' => 'BakeTags',
  164. 'foreignKey' => 'bake_article_id',
  165. 'joinTable' => 'bake_articles_bake_tags',
  166. 'targetForeignKey' => 'bake_tag_id',
  167. ],
  168. ],
  169. ];
  170. $this->assertEquals($expected, $result);
  171. }
  172. /**
  173. * test that belongsTo generation works.
  174. *
  175. * @return void
  176. */
  177. public function testBelongsToGeneration() {
  178. $model = TableRegistry::get('BakeComments');
  179. $result = $this->Task->findBelongsTo($model, []);
  180. $expected = [
  181. 'belongsTo' => [
  182. [
  183. 'alias' => 'BakeArticles',
  184. 'foreignKey' => 'bake_article_id',
  185. ],
  186. [
  187. 'alias' => 'BakeUsers',
  188. 'foreignKey' => 'bake_user_id',
  189. ],
  190. ]
  191. ];
  192. $this->assertEquals($expected, $result);
  193. $model = TableRegistry::get('CategoryThreads');
  194. $result = $this->Task->findBelongsTo($model, array());
  195. $expected = [
  196. 'belongsTo' => [
  197. [
  198. 'alias' => 'ParentCategoryThreads',
  199. 'foreignKey' => 'parent_id',
  200. ],
  201. ]
  202. ];
  203. $this->assertEquals($expected, $result);
  204. }
  205. /**
  206. * test that hasOne and/or hasMany relations are generated properly.
  207. *
  208. * @return void
  209. */
  210. public function testHasManyGeneration() {
  211. $this->Task->connection = 'test';
  212. $model = TableRegistry::get('BakeArticles');
  213. $result = $this->Task->findHasMany($model, []);
  214. $expected = [
  215. 'hasMany' => [
  216. [
  217. 'alias' => 'BakeComments',
  218. 'foreignKey' => 'bake_article_id',
  219. ],
  220. ],
  221. ];
  222. $this->assertEquals($expected, $result);
  223. $model = TableRegistry::get('CategoryThreads');
  224. $result = $this->Task->findHasMany($model, []);
  225. $expected = [
  226. 'hasMany' => [
  227. [
  228. 'alias' => 'ChildCategoryThreads',
  229. 'foreignKey' => 'parent_id',
  230. ],
  231. ]
  232. ];
  233. $this->assertEquals($expected, $result);
  234. }
  235. /**
  236. * Test that HABTM generation works
  237. *
  238. * @return void
  239. */
  240. public function testHasAndBelongsToManyGeneration() {
  241. $this->Task->connection = 'test';
  242. $model = TableRegistry::get('BakeArticles');
  243. $result = $this->Task->findBelongsToMany($model, []);
  244. $expected = [
  245. 'belongsToMany' => [
  246. [
  247. 'alias' => 'BakeTags',
  248. 'foreignKey' => 'bake_article_id',
  249. 'joinTable' => 'bake_articles_bake_tags',
  250. 'targetForeignKey' => 'bake_tag_id',
  251. ],
  252. ],
  253. ];
  254. $this->assertEquals($expected, $result);
  255. }
  256. /**
  257. * Test getting accessible fields.
  258. *
  259. * @return void
  260. */
  261. public function testGetFields() {
  262. $model = TableRegistry::get('BakeArticles');
  263. $result = $this->Task->getFields($model);
  264. $expected = [
  265. 'bake_user_id',
  266. 'title',
  267. 'body',
  268. 'published',
  269. ];
  270. $this->assertEquals($expected, $result);
  271. }
  272. /**
  273. * Test getting accessible fields includes associations.
  274. *
  275. * @return void
  276. */
  277. public function testGetFieldsAssociations() {
  278. $model = TableRegistry::get('BakeArticles');
  279. $model->belongsToMany('BakeTags');
  280. $model->belongsTo('BakeAuthors');
  281. $model->hasMany('BakeComments');
  282. $result = $this->Task->getFields($model);
  283. $this->assertContains('bake_tags', $result);
  284. $this->assertContains('bake_comments', $result);
  285. $this->assertContains('bake_author', $result);
  286. }
  287. /**
  288. * Test getting field with the no- option
  289. *
  290. * @return void
  291. */
  292. public function testGetFieldsDisabled() {
  293. $model = TableRegistry::get('BakeArticles');
  294. $this->Task->params['no-fields'] = true;
  295. $result = $this->Task->getFields($model);
  296. $this->assertEquals([], $result);
  297. }
  298. /**
  299. * Test getting field with a whitelist
  300. *
  301. * @return void
  302. */
  303. public function testGetFieldsWhiteList() {
  304. $model = TableRegistry::get('BakeArticles');
  305. $this->Task->params['fields'] = 'id, title , , body , created';
  306. $result = $this->Task->getFields($model);
  307. $expected = [
  308. 'id',
  309. 'title',
  310. 'body',
  311. 'created',
  312. ];
  313. $this->assertEquals($expected, $result);
  314. }
  315. /**
  316. * Test getting hidden fields.
  317. *
  318. * @return void
  319. */
  320. public function testGetHiddenFields() {
  321. $model = TableRegistry::get('Users');
  322. $result = $this->Task->getHiddenFields($model);
  323. $expected = [
  324. 'password',
  325. ];
  326. $this->assertEquals($expected, $result);
  327. }
  328. /**
  329. * Test getting hidden field with the no- option
  330. *
  331. * @return void
  332. */
  333. public function testGetHiddenFieldsDisabled() {
  334. $model = TableRegistry::get('Users');
  335. $this->Task->params['no-hidden'] = true;
  336. $result = $this->Task->getHiddenFields($model);
  337. $this->assertEquals([], $result);
  338. }
  339. /**
  340. * Test getting hidden field with a whitelist
  341. *
  342. * @return void
  343. */
  344. public function testGetHiddenFieldsWhiteList() {
  345. $model = TableRegistry::get('Users');
  346. $this->Task->params['hidden'] = 'id, title , , body , created';
  347. $result = $this->Task->getHiddenFields($model);
  348. $expected = [
  349. 'id',
  350. 'title',
  351. 'body',
  352. 'created',
  353. ];
  354. $this->assertEquals($expected, $result);
  355. }
  356. /**
  357. * Test getting primary key
  358. *
  359. * @return void
  360. */
  361. public function testGetPrimaryKey() {
  362. $model = TableRegistry::get('BakeArticles');
  363. $result = $this->Task->getPrimaryKey($model);
  364. $expected = ['id'];
  365. $this->assertEquals($expected, $result);
  366. $this->Task->params['primary-key'] = 'id, , account_id';
  367. $result = $this->Task->getPrimaryKey($model);
  368. $expected = ['id', 'account_id'];
  369. $this->assertEquals($expected, $result);
  370. }
  371. /**
  372. * test getting validation rules with the no-validation rule.
  373. *
  374. * @return void
  375. */
  376. public function testGetValidationDisabled() {
  377. $model = TableRegistry::get('BakeArticles');
  378. $this->Task->params['no-validation'] = true;
  379. $result = $this->Task->getValidation($model);
  380. $this->assertEquals([], $result);
  381. }
  382. /**
  383. * test getting validation rules.
  384. *
  385. * @return void
  386. */
  387. public function testGetValidation() {
  388. $model = TableRegistry::get('BakeArticles');
  389. $result = $this->Task->getValidation($model);
  390. $expected = [
  391. 'bake_user_id' => ['rule' => 'numeric', 'allowEmpty' => false],
  392. 'title' => ['rule' => false, 'allowEmpty' => false],
  393. 'body' => ['rule' => false, 'allowEmpty' => true],
  394. 'published' => ['rule' => 'boolean', 'allowEmpty' => true],
  395. 'id' => ['rule' => 'numeric', 'allowEmpty' => 'create']
  396. ];
  397. $this->assertEquals($expected, $result);
  398. $model = TableRegistry::get('BakeComments');
  399. $result = $this->Task->getValidation($model);
  400. $expected = [
  401. 'bake_article_id' => ['rule' => 'numeric', 'allowEmpty' => false],
  402. 'bake_user_id' => ['rule' => 'numeric', 'allowEmpty' => false],
  403. 'comment' => ['rule' => false, 'allowEmpty' => true],
  404. 'published' => ['rule' => false, 'allowEmpty' => true],
  405. 'otherid' => ['rule' => 'numeric', 'allowEmpty' => 'create']
  406. ];
  407. $this->assertEquals($expected, $result);
  408. }
  409. /**
  410. * test non interactive doActsAs
  411. *
  412. * @return void
  413. */
  414. public function testGetBehaviors() {
  415. $model = TableRegistry::get('NumberTrees');
  416. $result = $this->Task->getBehaviors($model);
  417. $this->assertEquals(['Tree' => []], $result);
  418. $model = TableRegistry::get('BakeArticles');
  419. $result = $this->Task->getBehaviors($model);
  420. $this->assertEquals(['Timestamp' => []], $result);
  421. $model = TableRegistry::get('CounterCacheUsers');
  422. $result = $this->Task->getBehaviors($model);
  423. $expected = [
  424. 'CounterCache' => ["'Posts' => ['post_count']"]
  425. ];
  426. $this->assertEquals($expected, $result);
  427. }
  428. /**
  429. * Test getDisplayField() method.
  430. *
  431. * @return void
  432. */
  433. public function testGetDisplayField() {
  434. $model = TableRegistry::get('BakeArticles');
  435. $result = $this->Task->getDisplayField($model);
  436. $this->assertEquals('title', $result);
  437. $this->Task->params['display-field'] = 'custom';
  438. $result = $this->Task->getDisplayField($model);
  439. $this->assertEquals('custom', $result);
  440. }
  441. /**
  442. * Ensure that the fixture object is correctly called.
  443. *
  444. * @return void
  445. */
  446. public function testBakeFixture() {
  447. $this->Task->plugin = 'TestPlugin';
  448. $this->Task->Fixture->expects($this->at(0))
  449. ->method('bake')
  450. ->with('BakeArticle', 'bake_articles');
  451. $this->Task->bakeFixture('BakeArticle', 'bake_articles');
  452. $this->assertEquals($this->Task->plugin, $this->Task->Fixture->plugin);
  453. $this->assertEquals($this->Task->connection, $this->Task->Fixture->connection);
  454. $this->assertEquals($this->Task->interactive, $this->Task->Fixture->interactive);
  455. }
  456. /**
  457. * Ensure that the fixture baking can be disabled
  458. *
  459. * @return void
  460. */
  461. public function testBakeFixtureDisabled() {
  462. $this->Task->params['no-fixture'] = true;
  463. $this->Task->plugin = 'TestPlugin';
  464. $this->Task->Fixture->expects($this->never())
  465. ->method('bake');
  466. $this->Task->bakeFixture('BakeArticle', 'bake_articles');
  467. }
  468. /**
  469. * Ensure that the test object is correctly called.
  470. *
  471. * @return void
  472. */
  473. public function testBakeTest() {
  474. $this->Task->plugin = 'TestPlugin';
  475. $this->Task->Test->expects($this->at(0))
  476. ->method('bake')
  477. ->with('Table', 'BakeArticle');
  478. $this->Task->bakeTest('BakeArticle');
  479. $this->assertEquals($this->Task->plugin, $this->Task->Test->plugin);
  480. $this->assertEquals($this->Task->connection, $this->Task->Test->connection);
  481. $this->assertEquals($this->Task->interactive, $this->Task->Test->interactive);
  482. }
  483. /**
  484. * Ensure that test baking can be disabled.
  485. *
  486. * @return void
  487. */
  488. public function testBakeTestDisabled() {
  489. $this->Task->params['no-test'] = true;
  490. $this->Task->plugin = 'TestPlugin';
  491. $this->Task->Test->expects($this->never())
  492. ->method('bake');
  493. $this->Task->bakeTest('BakeArticle');
  494. }
  495. /**
  496. * test baking validation
  497. *
  498. * @return void
  499. */
  500. public function testBakeTableValidation() {
  501. $validation = array(
  502. 'id' => array(
  503. 'allowEmpty' => 'create',
  504. 'rule' => 'numeric',
  505. ),
  506. 'name' => array(
  507. 'allowEmpty' => false,
  508. 'rule' => false,
  509. ),
  510. 'email' => array(
  511. 'allowEmpty' => true,
  512. 'rule' => 'email',
  513. ),
  514. );
  515. $model = TableRegistry::get('BakeArticles');
  516. $result = $this->Task->bakeTable($model, compact('validation'));
  517. $this->assertContains('namespace App\Model\Table;', $result);
  518. $this->assertContains('use Cake\ORM\Table;', $result);
  519. $this->assertContains('use Cake\Validation\Validator;', $result);
  520. $this->assertContains('class BakeArticlesTable extends Table {', $result);
  521. $this->assertContains('public function validationDefault(Validator $validator) {', $result);
  522. $this->assertContains("->add('id', 'valid', ['rule' => 'numeric'])", $result);
  523. $this->assertContains("->add('email', 'valid', ['rule' => 'email'])", $result);
  524. $this->assertContains("->allowEmpty('id', 'create')", $result);
  525. $this->assertContains("->allowEmpty('email')", $result);
  526. $this->assertContains("->validatePresence('name', 'create')", $result);
  527. }
  528. /**
  529. * test baking
  530. *
  531. * @return void
  532. */
  533. public function testBakeTableConfig() {
  534. $config = [
  535. 'table' => 'articles',
  536. 'primaryKey' => ['id'],
  537. 'displayField' => 'title',
  538. 'behaviors' => ['Timestamp' => ''],
  539. ];
  540. $model = TableRegistry::get('BakeArticles');
  541. $result = $this->Task->bakeTable($model, $config);
  542. $this->assertContains('public function initialize(array $config) {', $result);
  543. $this->assertContains("this->primaryKey(['id']);\n", $result);
  544. $this->assertContains("this->displayField('title');\n", $result);
  545. $this->assertContains("this->addBehavior('Timestamp');\n", $result);
  546. $this->assertContains("this->table('articles');\n", $result);
  547. $this->assertContains('use Cake\Validation\Validator;', $result);
  548. }
  549. /**
  550. * test baking relations
  551. *
  552. * @return void
  553. */
  554. public function testBakeTableRelations() {
  555. $associations = [
  556. 'belongsTo' => [
  557. [
  558. 'alias' => 'SomethingElse',
  559. 'foreignKey' => 'something_else_id',
  560. ],
  561. [
  562. 'alias' => 'BakeUser',
  563. 'foreignKey' => 'bake_user_id',
  564. ],
  565. ],
  566. 'hasMany' => [
  567. [
  568. 'alias' => 'BakeComment',
  569. 'foreignKey' => 'parent_id',
  570. ],
  571. ],
  572. 'belongsToMany' => [
  573. [
  574. 'alias' => 'BakeTag',
  575. 'foreignKey' => 'bake_article_id',
  576. 'joinTable' => 'bake_articles_bake_tags',
  577. 'targetForeignKey' => 'bake_tag_id',
  578. ],
  579. ]
  580. ];
  581. $model = TableRegistry::get('BakeArticles');
  582. $result = $this->Task->bakeTable($model, compact('associations'));
  583. $this->assertContains("this->hasMany('BakeComment', [", $result);
  584. $this->assertContains("this->belongsTo('SomethingElse', [", $result);
  585. $this->assertContains("this->belongsTo('BakeUser', [", $result);
  586. $this->assertContains("this->belongsToMany('BakeTag', [", $result);
  587. $this->assertContains("'joinTable' => 'bake_articles_bake_tags',", $result);
  588. }
  589. /**
  590. * test baking an entity class
  591. *
  592. * @return void
  593. */
  594. public function testBakeEntity() {
  595. $config = [
  596. 'fields' => []
  597. ];
  598. $model = TableRegistry::get('BakeArticles');
  599. $result = $this->Task->bakeEntity($model, $config);
  600. $this->assertContains('namespace App\Model\Entity;', $result);
  601. $this->assertContains('use Cake\ORM\Entity;', $result);
  602. $this->assertContains('class BakeArticle extends Entity {', $result);
  603. $this->assertNotContains('$_accessible', $result);
  604. }
  605. /**
  606. * test baking an entity class
  607. *
  608. * @return void
  609. */
  610. public function testBakeEntityFields() {
  611. $config = [
  612. 'fields' => ['title', 'body', 'published']
  613. ];
  614. $model = TableRegistry::get('BakeArticles');
  615. $result = $this->Task->bakeEntity($model, $config);
  616. $this->assertContains("protected \$_accessible = [", $result);
  617. $this->assertContains("'title' => true,", $result);
  618. $this->assertContains("'body' => true,", $result);
  619. $this->assertContains("'published' => true", $result);
  620. $this->assertNotContains("protected \$_hidden", $result);
  621. }
  622. /**
  623. * test baking an entity class sets hidden fields.
  624. *
  625. * @return void
  626. */
  627. public function testBakeEntityHidden() {
  628. $model = TableRegistry::get('BakeUsers');
  629. $config = [
  630. 'hidden' => ['password'],
  631. ];
  632. $result = $this->Task->bakeEntity($model, $config);
  633. $this->assertContains("protected \$_hidden = [", $result);
  634. $this->assertContains("'password'", $result);
  635. $this->assertNotContains("protected \$_accessible", $result);
  636. }
  637. /**
  638. * test bake() with a -plugin param
  639. *
  640. * @return void
  641. */
  642. public function testBakeTableWithPlugin() {
  643. $this->Task->plugin = 'ControllerTest';
  644. // fake plugin path
  645. Plugin::load('ControllerTest', array('path' => APP . 'Plugin' . DS . 'ControllerTest' . DS));
  646. $path = $this->_normalizePath(APP . 'Plugin/ControllerTest/Model/Table/BakeArticlesTable.php');
  647. $this->Task->expects($this->once())->method('createFile')
  648. ->with($path, $this->logicalAnd(
  649. $this->stringContains('namespace ControllerTest\\Model\\Table;'),
  650. $this->stringContains('use Cake\\ORM\\Table;'),
  651. $this->stringContains('class BakeArticlesTable extends Table {')
  652. ));
  653. $model = TableRegistry::get('BakeArticles');
  654. $this->Task->bakeTable($model);
  655. }
  656. /**
  657. * test bake() with a -plugin param
  658. *
  659. * @return void
  660. */
  661. public function testBakeEntityWithPlugin() {
  662. $this->Task->plugin = 'ControllerTest';
  663. // fake plugin path
  664. Plugin::load('ControllerTest', array('path' => APP . 'Plugin' . DS . 'ControllerTest' . DS));
  665. $path = APP . 'Plugin' . DS . 'ControllerTest' . DS . 'Model' . DS . 'Entity' . DS . 'BakeArticle.php';
  666. $path = $this->_normalizePath($path);
  667. $this->Task->expects($this->once())->method('createFile')
  668. ->with($path, $this->logicalAnd(
  669. $this->stringContains('namespace ControllerTest\\Model\\Entity;'),
  670. $this->stringContains('use Cake\\ORM\\Entity;'),
  671. $this->stringContains('class BakeArticle extends Entity {')
  672. ));
  673. $model = TableRegistry::get('BakeArticles');
  674. $this->Task->bakeEntity($model);
  675. }
  676. /**
  677. * test that execute with no args
  678. *
  679. * @return void
  680. */
  681. public function testMainNoArgs() {
  682. $this->_useMockedOut();
  683. $this->Task->connection = 'test';
  684. $this->Task->path = '/my/path/';
  685. $this->Task->expects($this->at(0))
  686. ->method('out')
  687. ->with($this->stringContains('Choose a model to bake from the following:'));
  688. $this->Task->main();
  689. }
  690. /**
  691. * test that execute passes runs bake depending with named model.
  692. *
  693. * @return void
  694. */
  695. public function testMainWithNamedModel() {
  696. $this->Task->connection = 'test';
  697. $tableFile = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
  698. $this->Task->expects($this->at(0))
  699. ->method('createFile')
  700. ->with($tableFile, $this->stringContains('class BakeArticlesTable extends Table'));
  701. $entityFile = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
  702. $this->Task->expects($this->at(1))
  703. ->method('createFile')
  704. ->with($entityFile, $this->stringContains('class BakeArticle extends Entity'));
  705. $this->Task->main('BakeArticles');
  706. }
  707. /**
  708. * data provider for testMainWithNamedModelVariations
  709. *
  710. * @return void
  711. */
  712. public static function nameVariations() {
  713. return array(
  714. array('BakeArticles'), array('BakeArticle'), array('bake_article'), array('bake_articles')
  715. );
  716. }
  717. /**
  718. * test that execute passes with different inflections of the same name.
  719. *
  720. * @dataProvider nameVariations
  721. * @return void
  722. */
  723. public function testMainWithNamedModelVariations($name) {
  724. $this->Task->connection = 'test';
  725. $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
  726. $this->Task->expects($this->at(0))
  727. ->method('createFile')
  728. ->with($filename, $this->stringContains('class BakeArticlesTable extends Table {'));
  729. $this->Task->main($name);
  730. }
  731. /**
  732. * test that execute runs all() when args[0] = all
  733. *
  734. * @return void
  735. */
  736. public function testMainIntoAll() {
  737. $count = count($this->Task->listAll());
  738. if ($count != count($this->fixtures)) {
  739. $this->markTestSkipped('Additional tables detected.');
  740. }
  741. $this->Task->connection = 'test';
  742. $this->Task->Fixture->expects($this->exactly($count))
  743. ->method('bake');
  744. $this->Task->Test->expects($this->exactly($count))
  745. ->method('bake');
  746. $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
  747. $this->Task->expects($this->at(0))
  748. ->method('createFile')
  749. ->with($filename, $this->stringContains('class BakeArticlesTable extends'));
  750. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
  751. $this->Task->expects($this->at(1))
  752. ->method('createFile')
  753. ->with($filename, $this->stringContains('class BakeArticle extends'));
  754. $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesBakeTagsTable.php');
  755. $this->Task->expects($this->at(2))
  756. ->method('createFile')
  757. ->with($filename, $this->stringContains('class BakeArticlesBakeTagsTable extends'));
  758. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticlesBakeTag.php');
  759. $this->Task->expects($this->at(3))
  760. ->method('createFile')
  761. ->with($filename, $this->stringContains('class BakeArticlesBakeTag extends'));
  762. $filename = $this->_normalizePath(APP . 'Model/Table/BakeCommentsTable.php');
  763. $this->Task->expects($this->at(4))
  764. ->method('createFile')
  765. ->with($filename, $this->stringContains('class BakeCommentsTable extends'));
  766. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeComment.php');
  767. $this->Task->expects($this->at(5))
  768. ->method('createFile')
  769. ->with($filename, $this->stringContains('class BakeComment extends'));
  770. $filename = $this->_normalizePath(APP . 'Model/Table/BakeTagsTable.php');
  771. $this->Task->expects($this->at(6))
  772. ->method('createFile')
  773. ->with($filename, $this->stringContains('class BakeTagsTable extends'));
  774. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeTag.php');
  775. $this->Task->expects($this->at(7))
  776. ->method('createFile')
  777. ->with($filename, $this->stringContains('class BakeTag extends'));
  778. $filename = $this->_normalizePath(APP . 'Model/Table/CategoryThreadsTable.php');
  779. $this->Task->expects($this->at(8))
  780. ->method('createFile')
  781. ->with($filename, $this->stringContains('class CategoryThreadsTable extends'));
  782. $filename = $this->_normalizePath(APP . 'Model/Entity/CategoryThread.php');
  783. $this->Task->expects($this->at(9))
  784. ->method('createFile')
  785. ->with($filename, $this->stringContains('class CategoryThread extends'));
  786. $this->Task->all();
  787. }
  788. /**
  789. * test that skipTables changes how all() works.
  790. *
  791. * @return void
  792. */
  793. public function testSkipTablesAndAll() {
  794. $count = count($this->Task->listAll('test'));
  795. if ($count != count($this->fixtures)) {
  796. $this->markTestSkipped('Additional tables detected.');
  797. }
  798. $this->Task->connection = 'test';
  799. $this->Task->skipTables = ['bake_tags'];
  800. $this->Task->Fixture->expects($this->exactly(7))
  801. ->method('bake');
  802. $this->Task->Test->expects($this->exactly(7))
  803. ->method('bake');
  804. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
  805. $this->Task->expects($this->at(1))
  806. ->method('createFile')
  807. ->with($filename);
  808. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticlesBakeTag.php');
  809. $this->Task->expects($this->at(3))
  810. ->method('createFile')
  811. ->with($filename);
  812. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeComment.php');
  813. $this->Task->expects($this->at(5))
  814. ->method('createFile')
  815. ->with($filename);
  816. $filename = $this->_normalizePath(APP . 'Model/Entity/CategoryThread.php');
  817. $this->Task->expects($this->at(7))
  818. ->method('createFile')
  819. ->with($filename);
  820. $filename = $this->_normalizePath(APP . 'Model/Entity/CounterCacheUser.php');
  821. $this->Task->expects($this->at(9))
  822. ->method('createFile')
  823. ->with($filename);
  824. $filename = $this->_normalizePath(APP . 'Model/Entity/NumberTree.php');
  825. $this->Task->expects($this->at(11))
  826. ->method('createFile')
  827. ->with($filename);
  828. $this->Task->all();
  829. }
  830. }